A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

1、到底使用谁
     如果在写数据的时候需要另起一行或者读数据的时候一行一行读就用字符缓冲流:BufferedReader & BufferedWriter
     如果操作的是文本就用字符流,因为操作比较方便。否则就用字节流
     如果根本不知道用哪个,就用字节流,因为字节流很强大

2、复制文本文件
     (1)复制文本文件的几种方式
          字节流:
                      4 种
                             基本流一次读写一个字节
                             基本流一次读写一个字节数组
                             高效流一次读写一个字节
                             高效流一次读写一个字节数组
                  字符流:
                      5 种
                             基本流一次读写一个字符
                             基本流一次读写一个字符数组
                             高效流一次读写一个字符
                             高效流一次读写一个字符数组
                             高效流一次读写一个字符串

     (2)推荐
          既然是文本文件那么用字符串缓冲流读写是最好的方式

     (3)推荐代码:(掌握)

      public static voidmain(String[] args) {

              BufferedReader reader = null;

              BufferedWriter write = null;



              try {

                     reader = new BufferedReader(newFileReader("o.txt"));

                     write = new BufferedWriter(newFileWriter("copy.txt"));

                     String line = null;



                     while( (line=reader.readLine())  != null){

                            write.write(line);

                            write.newLine();

                            write.flush();

                     }



              } catch(IOException e) {

                     e.printStackTrace();

              }finally {

                     if(reader != null) {

                            try {

                                   reader.close();

                            } catch (Exception e) {

                                   e.printStackTrace();

                            }

                     }



                     if (write != null) {

                            try {

                                   write.close();

                            } catch (Exception e) {

                                   e.printStackTrace();

                            }

                     }

              }

       }


3、复制二进制数据(非文本文件,音乐,视频,应用软件等..)

      (1)赋值二进制文件的几种方式
                  基本流一次读写一个字节
                  基本流一次读写一个字节数组
                  高效流一次读写一个字节
                  高效流一次读写一个字节数组


      (2)推荐
          使用缓冲流一次读写一个字节数组

      (3)推荐代码
       public static voidmain(String[] args) throws Exception {

              // 定义字节缓冲输入流

              BufferedInputStream bis = null;

              // 定义字节缓冲输出流

              BufferedOutputStream bos = null;



              try {

                     bis = new BufferedInputStream(newFileInputStream("d://readme.txt"));

                     bos = new BufferedOutputStream(newFileOutputStream("d://readme.txt.byte"));

                     byte[] bys = new byte[1024];

                     int len = -1;



                     while ((len = bis.read(bys)) != -1) {

                            bos.write(bys, 0, len);

                            bos.flush();

                     }

              } catch(Exception e) {

                     e.printStackTrace();

              } finally {



                     if (bis != null) {

                            try {

                                   bis.close();

                            } catch (Exception e) {

                                   e.printStackTrace();

                            }

                     }



                     if (bos != null) {

                            try {

                                   bos.close();

                            } catch (Exception e) {

                                   e.printStackTrace();

                            }

                     }



              }

       }

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马