java中如何用stream将一个文档里的内容写到新的文档中
3个回答
展开全部
File filein = new File("D://","Example1.txt");
File fileout = new File("D://","Example1-1.txt");
FileInputStream fis;
try{
if(!filein.exists()){
filein.createNewFile();
}
if(!fileout.exists()){
fileout.createNewFile();
}
fis = new FileInputStream(filein);
FileOutputStream fos = new FileOutputStream(fileout,true);
InputStreamReader in = new InputStreamReader(fis,"GB2312");
OutputStreamWriter out = new OutputStreamWriter(fos,"GB2312");
int is;
while ((is=in.read()) != -1){
out.write(is);
}
in.close();
out.close();
}catch (IOException e){
e.printStackTrace();
}
欢迎追问
展开全部
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class TestCopy {
public static void main(String[] args) throws IOException {
//原始文档路径
String old = "D:/b.txt";
//新文档路径
String newPath = "D:/a.txt";
//将原始文档通过输入流读入内存
FileInputStream fis = new FileInputStream(new File(old));
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr);
String temp;
//通过输出流将内存中的数据写出到新的文档
FileOutputStream fos = new FileOutputStream(new File(newPath),false);//设置为false表示每次执行不累加内容到a.txt,true表示累加内容到a.txt
BufferedOutputStream bw = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bw);
while((temp= br.readLine())!=null){
pw.println(temp);
}
if(pw!=null){
pw.close();
}
}
}
这个是将b.txt的内容写到a.txt文件里,不知你想要写的文件是什么格式的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这相当于一个复制文件的过程,可以按照如下的代码进行编写:
package com.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try{
// 源文件路径
in = new FileInputStream("D:\\text.txt");
// 结果文件路径
out = new FileOutputStream("E:\\copytext.txt");
while((b = in.read()) != -1){
out.write(b);
}
in.close();
out.close();
}catch(FileNotFoundException e1){
System.out.println("file is not found!");
System.exit(-1);
}catch(IOException e2){
System.out.println("文件复制错误!!");
System.exit(-1);
}
System.out.println("文件复制成功了~~!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询