我使用下面的程序复制一个txt文件C:/1.txt,里面的内容只是一个数字1,复制到文件2.txt后,在2.txt里,1后面有很多空格,即下面程序无法正确复制一个txt文件,这是我从Java范例大全里面抄写出来的程序段,我总是检查不出哪里错了,请帮忙。
import java.io.*;
import java.io.File;//引入类
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public void abcd () {
File fromFile = new File ("C:/1.txt");
File toFile = new File ("C:/2.txt");
copyFile(fromFile, toFile);
}    // 复制一个文件
    public void copyFile(File fromFile, File toFile) {// 复制文件
        createFile(toFile, true);// 创建文件
        System.out.println("复制文件" + fromFile.getAbsolutePath() + "到" + toFile.getAbsolutePath());
          try {
FileOutputStream fop = new FileOutputStream(toFile); // 创建FileOutputStream对象
            FileInputStream in = new FileInputStream(fromFile);
            int tempbyte;
  byte[] buffer = new byte[1024];
            while (in.read(buffer) != -1) { // 循环读取文件
                fop.write(buffer); // 通过流写数据
            }
            fop.close(); // 关闭输出流
            in.close(); // 关闭输入流
} catch (IOException e) {
                        e.printStackTrace();
                    }
    }     public void createFile(File file, boolean isFile) {// 创建文件
        if (!file.exists()) {// 如果文件不存在
            if (!file.getParentFile().exists()) {// 如果文件父目录不存在
                createFile(file.getParentFile(), false);
            } else {// 存在文件父目录
                if (isFile) {// 创建文件
                    try {
                        file.createNewFile();// 创建新文件
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    file.mkdirs();// 创建目录
                }
            }
        }
    }     public static void main(String[] args)  {
        long sttime = System.nanoTime();
        Copy sourceToDestiny = new Copy();
        sourceToDestiny.abcd();
        long endtime = System.nanoTime();
        System.out.println("Totaol time is: "+(endtime-sttime)/1000000000+"秒");
    }
}

解决方案 »

  1.   

    fop.write(buffer);把整个buffer都写进去了,但是其实buffer除了最前面放了个数字1,后面全是0,lz请用这个重载方法:void write(byte[] b, int off, int len)  
      

  2.   

    二楼说的没错,要精确复制就得到用到重载的方法,当然为何不用包装流来做呢,
    javaIO设计就是采用的装饰器模式,可以选择包装好的类来完成各种操作,BufferedReader,BufferedWriter
      

  3.   

    需要把已读的字符数也记下来,然后用write(byte[] b, int off, int len)这个函数
    import java.io.*;public class Copy {
    public void abcd () {
    File fromFile = new File ("c:\\1.txt");
    File toFile = new File ("c:\\2.txt");
    copyFile(fromFile, toFile);
    }

      // 复制一个文件
    public void copyFile(File fromFile, File toFile) {// 复制文件
    createFile(toFile, true);// 创建文件
    System.out.println("复制文件" + fromFile.getAbsolutePath() + "到" + toFile.getAbsolutePath());
    try {
    FileOutputStream fop = new FileOutputStream(toFile); // 创建FileOutputStream对象
    FileInputStream in = new FileInputStream(fromFile);
    int tempbyte;
    byte[] buffer = new byte[1024];
    int readNum; //这里添加一个变量用于记录已读的字符数
    while ((readNum = in.read(buffer)) != -1) { // 循环读取文件
    fop.write(buffer, 0, readNum); // 通过流写数据
    }
    fop.close(); // 关闭输出流
    in.close(); // 关闭输入流
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void createFile(File file, boolean isFile) {// 创建文件
    if (!file.exists()) {// 如果文件不存在
    if (!file.getParentFile().exists()) {// 如果文件父目录不存在
    createFile(file.getParentFile(), false);
    } else {// 存在文件父目录
    if (isFile) {// 创建文件
    try {
    file.createNewFile();// 创建新文件
    } catch (IOException e) {
    e.printStackTrace();
    }
    } else {
    file.mkdirs();// 创建目录
    }
    }
    }
    }

    public static void main(String[] args) {
    long sttime = System.nanoTime();
    Copy sourceToDestiny = new Copy();
    sourceToDestiny.abcd();
    long endtime = System.nanoTime();
    System.out.println("Totaol time is: "+(endtime-sttime)/1000000000+"秒");
    }
    }
      

  4.   

    直接用FileReader和FileWriter就行了,关键部分的while循环这样写就OK了如:int c=0;
    while ((c=in.read())!=-1){
        out.wirte(c);
    }
      

  5.   

    前面说的是用了字符流,一次只能读一个字符,因此不能用于文件中出现中文的情况,如果要读写中文,用字节流FileInputStream和FileOutputStream的read()和write()方法,一次读写一个字节
      

  6.   

    import java.io.*;
    public class TestFileInputStream {
    public static void main(String[] args) throws IOException {
        int b=0;
        int num=0;
        FileReader f = null; 
        FileWriter o = null;
        try {
     f = new FileReader("D:\\JavaWorkspace\\javaio\\src\\TestFileInputStream.java");
     o = new FileWriter("D:\\JavaWorkspace\\javaio\\TestFileOutputStream.java");
    } catch (FileNotFoundException e) {
    System.out.println("系统错误");
    System.exit(-1);
    }
    try{
    while((b=f.read())!=-1){
    num++;
    o.write(b);
    }
    System.out.println("总计"+num+"个字节");
    f.close();
    o.close();
    }catch(IOException e){
    System.out.println("读写错误");
    }

     }
    }//文件的读写操作的原理就是上面这样的  ,自己的代码要自己调试  
    //加油  嘿嘿
      

  7.   

    测试了一下,以下用FileReader能读到中文:import java.io.*;
    public class TestFileReader{
    public static void main(String[] args){
    FileReader fr=null;
    try{
    fr = new FileReader("d:/java/testData/1.txt");
    }catch(FileNotFoundException e){
    e.printStackTrace();
    }
    int c=0;
    try{
    while((c=fr.read())!=-1){
    System.out.print((char)c);
    }

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

    }
    }