将file1.txt复制到file2.txt中,源文件名和目标文件名作为命令行参数输入
请问怎么实现?

解决方案 »

  1.   

    public static void main(String[] args) throws Exception {
     String from_file = args[0];
     String to_file = args[1];
     BufferedInputStream in = new BufferedInputStream(new FileInputStream(from_file));
     BufferedOutputStream out = new BufferedOutputStream(new OutputStream(to_file));
     int n = in.read();
     while(n != -1) {
      out.write(n);
      n = in.read();
     }
     in.close();
     out.close();
    }
      

  2.   

    import java.io.*;public class FileTest
    {
    public static void main(String[] args)
    {
    Inter am=new Inter();
    if(args.length==0)
    {
    System.out.println("请输入文件名!");
    return;
    }
    try{

    am.show(args[0],args[1]);
    }catch(Exception e)
    {
    System.out.println(e.getMessage());

    }
    }
    }
    class Inter
    {
    String line="";
    public void show(String filename1,String filename2)throws Exception
    {

    FileReader f=new FileReader(filename1);
    BufferedReader m=new BufferedReader(f);

    FileWriter f1=new FileWriter(filename2);
    BufferedWriter m1=new BufferedWriter(f1);

    do
    {
    line=m.readLine();
    m1.write(line);
    m1.newLine();
    }while(line!=null);
    //关闭文件
    m.close();
    f.close();
    m1.close();
    f1.close();
    System.out.println("文件复制完成!");

    }

    }
    怎么最后显示是null?请问我这个有什么问题?