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

来源:互联网 发布:js的join方法 编辑:程序博客网 时间:2024/05/20 21:20
  //这是你的源文件,本身是存在的
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();
}
阅读全文
0 0
原创粉丝点击