Java 复制文件怎么写

解决方案 »

  1.   

    这个问题你google一下,很多解决方法
      

  2.   

    package testio;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;public class FileInputStreamAndFileOutpuStream { public static void main(String[] args) {
    String outpath = "D://out.txt";
    String inpath = "D://in.txt";
    OutputStream out = null;
    InputStream in = null;
    try {
    out = new BufferedOutputStream(new FileOutputStream(outpath), 1024);
    in = new BufferedInputStream(new FileInputStream(inpath), 1024);
    byte[] a = new byte[1024];
    int i;
    while ((i = in.read(a)) != -1) {
    out.write(a, 0, i);
    }
    out.flush();
    } catch (FileNotFoundException e) {
    System.out.println("File Not Found Exception");
    } catch (IOException e) {
    System.out.println("IO Exception");
    } finally {
    if (out != null)
    try {
    out.close();
    } catch (IOException e) {
    System.out.println("IO Exception");
    } if (in != null)
    try {
    in.close();
    } catch (IOException e) {
    System.out.println("IO Exception");
    }
    }
    }
    }
      

  3.   

    import java.io.*;
    /**
    *
    *copy a file
    */
    public class MainClass5
    {
    public static void main(String[] args)throws Exception
    { File file=new File("MainClass1_1.txt");
    BufferedReader bin=new BufferedReader(new FileReader(file));
                    BufferedWriter bout=new BufferedWriter(new FileWriter("MainClass1_2.txt"));
                    
    char[] buffer=new char[1024];
    int charRead;
    while((charRead=bin.read(buffer))!=-1)
    {
    bout.write(buffer,0,charRead);
    }
    bin.close();
    bout.close();
    }
    }多练练IO部分。
      

  4.   

    import java.io.*;
    /**
    *
    *copy a file
    */
    public class MainClass5
    {
    public static void main(String[] args)throws Exception
    { File file=new File("MainClass1_1.txt");
    BufferedReader bin=new BufferedReader(new FileReader(file));
                    BufferedWriter bout=new BufferedWriter(new FileWriter("MainClass1_2.txt"));
                    
    char[] buffer=new char[1024];
    int charRead;
    while((charRead=bin.read(buffer))!=-1)
    {
    bout.write(buffer,0,charRead);
    }
    bin.close();
    bout.close();
    }
    }
    多练练IO部分
      

  5.   

    import java.io.*;
    public class FileCopy {
      public static void main(String[] args) {
      int b = 0;
      FileReader in = null;
      FileWriter out = null;
      try {
        in = new FileReader(String   Source file directory);//参数为源文件的绝对路径
        out = new FileWriter(String   destination file directory);//参数为目地文件的绝对路径
        while((b=in.read())!=-1){//关键部分
          out.write(b);
        }//文件读取是否到了结尾
        out.close();
        in.close(); 
        
      } catch (FileNotFoundException e2) {
        System.out.println("找不到指定文件"); System.exit(-1);
      } catch (IOException e1) {
        System.out.println("文件复制错误"); System.exit(-1);
      }
      System.out.println("文件已复制");
      }
    }