美文网首页
文件的复制通过字符流(writer)

文件的复制通过字符流(writer)

作者: Joy_yang17 | 来源:发表于2018-05-23 19:10 被阅读3次
    public class CopyReaderAndWriter {
        public static void main(String[] args) {
          try {
            FileInputStream fileInputStream = new FileInputStream("learn.txt");
            //把字节流转化成字符流
            InputStreamReader isr = new InputStreamReader(fileInputStream);
            //public FileOutputStream(String name, boolean append)代表append可接字符串
            FileOutputStream fileOutputStream = new FileOutputStream("learn2.txt",true);
            //把字节流转化成字符流
            OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream);
            
            char c [] = new char[1024];
            int l = 0;
            while ((l = isr.read(c)) != -1) {
                osw.write(new String(c,0,l));
            }
            isr.close();
            fileInputStream.close();
            osw.close();
            fileInputStream.close();
            System.out.println("done");
          
          } catch (Exception e) {
            e.printStackTrace();
            
        }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:文件的复制通过字符流(writer)

          本文链接:https://www.haomeiwen.com/subject/lvldjftx.html