zoukankan      html  css  js  c++  java
  • Java中的IO流(1)

    字节流:
    
    //一个字节一个字节的读写
    FileInputStream in=new FileInputStream("源文件");
    FileOutputStream out=new FileOutputStream("目标文件");
    ......
    in.close();
    out.close();
    
    首先要记住,一旦使用了IO流最后一定要记得关闭,这是常识。
    
    //利用缓冲区,高效的读取字节
    BufferedInputStream in=new BufferedInputStream(new FileInputStream("源文件");
    BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("目标文件");
    ......
    in.close();
    out.close();
    
    字符流:
    
    InputStreamReader isr=new InputStreamReader(new FileInputStream("源文件路径"),"设置编码");
    OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("目标文件路径"),"设置编码");
    ......
    osw.close();
    isr.close();
    
    也可以这样写:
    
    FileReader fr=new FileReader("源文件路径");
    FileWriter fw=new FileWriter("目标文件路径");
    ......
    fr.close();
    fw.close();
    
    //利用缓冲区,高效的读取字符
    BufferedReader br=new BufferedReader(new FileReade("源文件路径");
    PrintWriter pw=new PrintWriter("目标文件路径");
    ......
    br.close();
    pw.close();
    
    序列化和反序列化:
    
    //对象的序列化
    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
    
    这里的file指的是String file="文件在项目中的路径";
    
    //对象的反序列化
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
    

      

  • 相关阅读:
    Java后端WebSocket的Tomcat实现
    Shiro session和Spring session一样吗?
    HTTP请求类
    JSP页面中的时间显示问题
    Oracle在linux中相关设置操作
    关于BigDecimal类型在jsp页面中进行除法运算问题
    Spring与Redis的实现
    gson介绍
    busybox介绍
    vsftp中426 Failure writing network stream的错误解决
  • 原文地址:https://www.cnblogs.com/ipetergo/p/6443840.html
Copyright © 2011-2022 走看看