java读取输入流并保存成一个文件

//这是你的源文件,本身是存在的
File beforefile = new File("c://a.txt");

//这是你要保存之后的文件,是自定义的,本身不存在
File afterfile = new File("d://a.txt");

//定义文件输入流,用来读取beforefile文件
InputStream in = new FileInputStream(beforefile);

//定义文件输出流,用来把信息写入afterfile文件中
OutputStream out = new FileOutputStream(afterfile);

//文件缓存区
byte[] bytes= new byte[1024];
//将文件流信息读取文件缓存区,如果读取结果不为-1就代表文件没有读取完毕,反之已经读取完毕
while(in.read( bytes )!=-1){
//将缓存区中的内容写到afterfile文件中
out.write( bytes );
out.flush();

out.close();
}
posted @ 2020-11-15 04:50  风肖肖(肖遥)  阅读(2402)  评论(0编辑  收藏  举报