【每日日报】第三十三天---文件操作(缓冲流)

1 今天继续看书

缓冲流读文件

复制代码
 1 package File;
 2 import java.io.BufferedReader;
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 
 7 public class BufferedReaderDemo {
 8     public static void main(String[] args)throws IOException{
 9         read();
10     }
11     public static void read() throws IOException{
12         BufferedReader read=new BufferedReader (new InputStreamReader(new FileInputStream("D:/Hello.txt")));
13         String line=null;
14         while((line=read.readLine())!=null){
15             System.out.println(line);
16         }
17         read.close();
18     }
19 
20 }
复制代码

缓冲流写文件

复制代码
 1 package File;
 2 import java.io.BufferedWriter;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.OutputStreamWriter;
 6 
 7 public class BufferedWriterDemo {
 8     public static void main(String[] args)throws IOException{
 9         write();
10     }
11     public static void write()throws IOException{
12         BufferedWriter w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/Hello.txt")));
13         w.write("Hello");
14         w.write("Java");
15         w.close();
16     }
17 
18 }
复制代码

使用BufferedInputStream和BufferedOutputStream读写图片

复制代码
 1 package File;
 2 import java.io.BufferedInputStream;
 3 import java.io.BufferedOutputStream;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 public class BufferedInputOutput {
 9     public static void main(String[] args)throws IOException{
10         outIn();
11     }
12     public static void outIn()throws IOException{
13         BufferedInputStream in=new BufferedInputStream(new FileInputStream("C:\\Users\\linmob\\Pictures\\ps\\a.png"));
14         BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("C:\\Users\\linmob\\Pictures\\ps\\a2.png"));
15         byte[] buf=new byte[1024];
16         int len=-1;
17         while((len=in.read(buf))!=-1){
18             out.write(buf,0,len);
19         }
20         out.close();
21         in.close();
22     }
23 }
复制代码

 

2 没有其他问题 

3 明天继续看书

posted @   我试试这个昵称好使不  阅读(72)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示