一个实现文件拷贝的程序:
源程序如下:
import java.io.*;
class Txtcopy
{    public static void main(String[] args)
     {  byte[] b1=new byte[255];
        byte[] b2=new byte[255];
        byte[] b3=new byte[2056];
        byte[] b4=new byte[2056];
        try
          {  System.out.println("请输入源文件名称:\n");
             System.in.read(b1,0,255);
             System.out.println("\n请输入目的文件名称:\n");
             System.in.read(b2,0,255);
             String sourceName=new String(b1,0);
             String desName=new String(b2,0);
             FileInputStream fileInput=new FileInputStream(sourceName);
             int bytes1=fileInput.read(b3,0,2056);
             String sourceFile=new String(b3,0,0,bytes1);
             FileOutputStream fileOutput=new FileOutputStream(desName);
             fileOutput.write(b3,0,bytes1);
             fileInput=new FileInputStream(desName);
             int bytes2=fileInput.read(b4,0,2056);
             String desFile=new String(b4,0,0,bytes2);
             System.out.println("\n源文件内容为:");
             System.out.println(sourceFile);
             System.out.println("\n目的文件内容为:");
             System.out.println(desFile);          }
          catch(Exception e)
          {  System.out.println(e.toString());  }
     }
}
在输入源文件名称时总是提示FileNotFoundException,但文件确实存在。请教原因!

解决方案 »

  1.   

    import java.io.*;
    class Txtcopy
    {    public static void main(String[] args)
         {  byte[] b1=new byte[255];
            byte[] b2=new byte[255];
            byte[] b3=new byte[2056];
            byte[] b4=new byte[2056];
            try
              {  System.out.println("请输入源文件名称:\n");
                 System.in.read(b1,0,255);
                 System.out.println("\n请输入目的文件名称:\n");
                 System.in.read(b2,0,255);
                 String sourceName=new String(b1,0);
                 String desName=new String(b2,0);
                 FileInputStream fileInput=new FileInputStream("Txtcopy.java");
                 int bytes1=fileInput.read(b3,0,2056);
                 String sourceFile=new String(b3,0,0,bytes1);
                 File f1 = new File(sourceFile);
                 FileOutputStream fileOutput=new FileOutputStream(f1);
                 fileOutput.write(b3,0,bytes1);
                 File f2 = new File(desName);
                 fileInput=new FileInputStream(f2);
                 int bytes2=fileInput.read(b4,0,2056);
                 String desFile=new String(b4,0,0,bytes2);
                 System.out.println("\n源文件内容为:");
                 System.out.println(sourceFile);
                 System.out.println("\n目的文件内容为:");
                 System.out.println(desFile);          }
              catch(Exception e)
              {  System.out.println(e.toString());  }
         }
    }
      

  2.   

    平台问题,不同的平台的目录表示形式不一样,java 不能自动识别是哪个平台的目录,所以就出现了FileNotFoundException