File类和stream流

File类操作文件和目录、stream流操作文件内容

File类:  

     InputStream   输入流(写文件)抽象类,用ctrl+h找子类
        FileInputStream file1=new FileInputStream(文件路径/文件名) ;

 
    OutputStream  输出流(读文件)
        FileOutputStream file2=new FileOutputStream(文件路径/文件名) ;
        

/*复制文件:FileUtils.copyFile(旧的文件路径/文件名,新的文件路径/文件名);(需要导包)    

 

stream流
    
字节流读:以字节为单位来读文件;(不推荐)    

FileInputStream fis=new FileInputStream("文件路径/文件名");
InputStreamReader isr=new InputStreamReader(fis);
isr.read()// 逐字节读取 


字节流写:以字节为单位来写文件;(不推荐)    

FileOutputStream fos=new FileOutputStream("文件路径/文件名");
fos.write(byte[] arr) //将字符数组顺序写入文件中(可以与String str.getBytes()搭配使用) 
          (int a)       //将一个整型的ASCII码重新写入文件中

 

字符流读:以字符为单位来读一行文件;(推荐)    

FileReader fr=new FileReader("文件路径/文件名")
BufferedReader br=new BufferedReader(fr);
br.readLine()  //读取文件内容,返回String,若读完返回null;


    
字符流写:以字符为单位来写一行文件;(推荐)    

FileWriter fw = new FileWriter(文件路径/文件名);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(String str)或.write(String str)   //把数据流全部重新写出


    
序列化:
    实现接口:implements Serializable
    生成序列化(利用idea提示自动生成):
        private static final long serialVersionUID = -2823867087228590138L;


版权声明:本文为ZHOU_0522原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。