日期:2014-05-20  浏览次数:20856 次

怎么从一个文本中把内容读出来并写到另一个文本中
import java.io.*;
public class TestInputOutput 
{
public static void main(String[] args) //throws Excepton
{
File f1=new File("D:\\我的文档\\桌面\\cc.txt");
File f2=new File("D:\\我的文档\\桌面\\cc2.txt");
try
{
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
FileWriter fw =new FileWriter(f2,true);
PrintWriter pw=new PrintWriter(fw);
String s;
s=br.readLine();
while(!s.equals(""))
{
pw.println(s);
s=br.readLine();
}
br.close();
pw.close();
}
catch(NullPointerException ex)//开始时没写这句,运行时程序报java.lang.NullPonterException加了这句,但生成的cc2.txt里仍没有cc.txt中的内容。
{

}
catch(Exception e)
{
e.printStackTrace();
}

}

}
//以上程序是想把桌面上的cc.txt文本文档的内容读出并写入到桌面上生成的一个cc2.txt中,为什么程序执行后没有报错而且生成了cc2.txt但文档的内容却是空的呢,程序哪点错了??????
//本人菜鸟,请哪位大侠指点一下,最好把可以执行的程序和详解贴给我谢谢。

------解决方案--------------------
试试
while((s=br.readLine())!=null)
{
pw.println(s);


可以利用apache的FileUtis来拷贝文件
------解决方案--------------------
PrintWriter pw=new PrintWriter(fw); 
改为
PrintWriter pw=new PrintWriter(fw,true);
------解决方案--------------------
但你这个程序会抛空指针异常,并不是正常结束循环的,当没有行给读时,S的值会被赋为NULL而不是""
------解决方案--------------------
探讨
试试
while((s=br.readLine())!=null)
{
          pw.println(s);
}

可以利用apache的FileUtis来拷贝文件

------解决方案--------------------
同意一楼
------解决方案--------------------

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class FileCopy {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File f1=new File("C:\\Documents and Settings\\Administrator\\桌面\\cc.txt"); 
File f2=new File("C:\\Documents and Settings\\Administrator\\桌面\\cc1.txt"); 
try 

FileReader fr=new FileReader(f1); 
BufferedReader br=new BufferedReader(fr); 
FileWriter fw =new FileWriter(f2,true); 
PrintWriter pw=new PrintWriter(fw); 
String s;  

while((s=br.readLine()) != null) 

System.out.println(s);
pw.println(s); 


br.close(); 
pw.close(); 

catch(NullPointerException ex)//开始时没写这句,运行时程序报java.lang.NullPonterException加了这句,但生成的cc2.txt里仍没有cc.txt中的内容。 

ex.printStackTrace();

catch(Exception e) 

e.printStackTrace(); 

}

}
你while循环时出现错误。
原因可能是cc.txt里只有一行数据,那样while循环就不会被执行,就会有空点数异常

------解决方案--------------------
Java code
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileChannelCopy {

    public static void main(String[] args) throws IOException {
        long t0, t1;
        t0 = System.currentTimeMillis();
        long len = copy("d:/刘宝瑞 - 连升三级.mp3", "d:/刘宝瑞 - 连升三级.bak.mp3");
        t1 = System.currentTimeMillis();
        System.out.println("copy file length: " + len);
        System.out.println("time: " + (t1 - t0));
    }
    
    private static long copy(String srcFilename, String destFilename) throws IOException {
        return copy(new File(srcFilename), new File(destFilename));
    }
    
    private static long copy(File src, File dest) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            FileChannel srcChannel = new FileInputStream(src).getChannel();
            FileChannel destChannel = new FileOutputStream(dest).getChannel();
            return srcChannel.transferTo(0, src.length(), destChannel);
        } finally {
            closeIO(fis);
            closeIO(fos);
        }
    }
    
    private static void closeIO(Closeable io) {
        if(io != null) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}