黑马程序员_java_IO流总结(中)

来源:互联网 发布:脸部比例测试软件 编辑:程序博客网 时间:2024/06/03 21:46
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------




四、字节流、字符流对比
(1)字节流指IO流中针对字节输入输出提供的一系列的流,统称为字节流。
字符流的指IO流中针对字符输入输出提供的一系列的流,统称为字符流。


(2)到底使用谁?
用记事本打开能读懂的,就用字符流。
否则,用字节流。
如果你根本就不知道,用字节流。




(3)复制文本文件(了解):
9种方式:
字节流:
4种
基本流一次读写一个字节
基本流一次读写一个字节数组
高效流一次读写一个字节
高效流一次读写一个字节数组
字符流:
5种
基本流一次读写一个字符
基本流一次读写一个字符数组
高效流一次读写一个字符
高效流一次读写一个字符数组
高效流一次读写一个字符串


一般来说,明明知道是文本文件,那么,肯定不用字节流。
那么,如果是使用字符流,有5种方式,选择哪一种呢?
一般都选择高效流读写一个字符串的方式。




代码体现:
数据源:c:\\a.txt
目的地:d:\\b.txt


BufferedReader br = new BufferedReader(new FileReader("c:\\a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\b.txt"));


String line = null;
while((line=br.readLine())!=null)
{
bw.write(line);
bw.newLine();
bw.flush();
}


bw.close();
br.close();


(4)复制二进制流数据:(图片,视频,音频等)
字节流:
4种
基本流一次读写一个字节
基本流一次读写一个字节数组
高效流一次读写一个字节
高效流一次读写一个字节数组


一般来说,我们选择使用高效流一次读写一个字节数组


代码体现:
数据源:c:\\a.jpg
目的地:d:\\b.jpg


BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\a.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\b.jpg"));


byte[] bys = new byte[1024];
int len = 0;
while((len=bis.read(bys))!=-1)
{
bos.write(bys,0,len);
}


bos.close();
bis.close();




五、FileWriter


1、使用步骤
/**


 * FileWriter类的使用


 * 该类是写入字符文件的快捷类


 */


public classFileWriterDemo {


       public static voidmain(String[] args)throws Exception {


              //第一步:创建字符流输出对象


              FileWriter fw = new FileWriter("readme.txt");


             


              //第二步:调用写入数据方法


              fw.write("注意事项"+System.getProperty("line.separator"));


              fw.write("1、上课不能玩手机"+System.getProperty("line.separator"));


              fw.write("2、不能迟到早退"+System.getProperty("line.separator"));


             


              //第三步:调用刷新缓冲区功能,该功能会将缓冲区的数据写入到文件,并且清空缓冲区中的内容


              fw.flush();


             


              //第四步:关闭流,释放资源


              fw.close();


       }


}






2、flush 方法和close方法的区别
     flush只刷新缓冲区,不释放流资源,流对象还可以继续使用
     close刷新并且释放流资源,流对象不可以继续使用




3、如何实现数据换行
     window:\r\n
     linux:\n
     mac:\r
     通用解决方案:String line = System.getProperty("line.separator");//根据当前系统返回相应的换行符




4、怎么实现追加数据
     调用FileWriter(String fileName, true)构造方法创建对象 
六、FileReader


1、使用步骤
importjava.io.FileReader;


public classFileReaderDemo {


       public static voidmain(String[] args)throws Exception {


              //第一步:创建字符流输入对象


              FileReader fr = new FileReader("readme.txt");


            


              //第二步:调用读取数据方法,并显示


              /*


               *方式1::一次读取一个字符,效率低 ,不推荐


               */


              int ch = 0;


              while((ch=fr.read()) != -1){


                     System.out.print((char)ch);


              }


             


             


              /*


               * 方式2:一次读取一个字符数组,效率高推荐


               */


              fr = newFileReader("readme.txt");//必须重新赋值,或者注释掉方式1中的代码,因为fr对象已经读取到末尾了,必须重新开始读取


              char[] chs = new char[1024];


              int len = 0;


              while((len = fr.read(chs)) != -1){


                     System.out.println(newString(chs,0,len));


              }


             




              //第三步:关闭流,释放资源,该对象无法继续使用


              fr.close();


       }


      


}


 










5、综合练习(掌握-必须练到闭着眼睛敲下来)


1、复制文本文件 , 从D 盘中复制一个 haha.txt文件( 里面字节写上 ”我爱学Java, 我爱学编程 ”)到E 盘下, 起名为 copy.txt




try异常的代码




import java.io.FileReader;


import java.io.FileWriter;


import java.io.IOException;


 


 


public class CopyFileDemo {


 


       public static void main(String[] args) throws Exception {


              boolean flag = copy(new FileReader("D://haha.txt"), new FileWriter("E://haha.txt"));


              if(flag){


                     System.out.println("拷贝成功");


              }else{


                     System.out.println("拷贝失败");


              }


       }


       /**


        * 拷贝文件


        * 思路:读取文件内容的同时,将读取到的内容写入到输出流


        */


       public static boolean copy(FileReader fileReader, FileWriter fileWriter) {


              //标识符,拷贝成功变为true并返回,拷贝失败返回false


              boolean flag = false;


             


              //创建字符数组,将读取到的内容存进去


              char[] chs = new char[1024];


              int len = -1;


             


             


              try {


                     //读取文件,将读取到的内容存到chs字符数组


                     while((len = fileReader.read(chs)) != -1){


                            //将字符数组中的内容写入到输出流


                            fileWriter.write(chs, 0, len);


                            fileWriter.flush();


                     }


                    


                     //拷贝成功,修改标识符的值为true


                     flag = true;


              } catch (IOException e) {


                     e.printStackTrace();


              }finally{


                     try {


                            fileWriter.close();


                     } catch (IOException e) {


                            e.printStackTrace();


                     }


                     try {


                            fileReader.close();


                     } catch (IOException e) {


                            e.printStackTrace();


                     }


              }


             


             


              return flag;


       }


}


 






















抛异常的代码




import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;




public class FileUtil {
         /**
          * 拷贝文本文件
          * 从src 拷贝到 dest
          * @param src
          * 源文件
          * @param dest
          * 目标文件
          */
         public static void copy(File src, File dest) throws IOException {
                   FileReader fr =new FileReader(src);
                   FileWriter fw = new FileWriter(dest);




                   
                   char[] chs = new char[1024];
                   int len = -1;
                   while ((len = fr.read(chs)) != -1) {
                            fw.write(chs, 0, len);
                   }
                   
                   fr.close();
                   fw.close();




         }
}
0 0
原创粉丝点击