简体   繁体   中英

Reading from one java file and writing to another one

I want content of one .java file to the another .java file. I am doing this with FileInput/FileOutput Stream classes in eclipse IDE . I have put one file named FileToFile.java inside Nisarg/src/FiliIO(package) .

And I am getting FileNotFoundException at line 12. I want to know why this exception raised?

This is what I actually got at runtime..

Exception in thread "main" java.io.FileNotFoundException: FileToFile.java (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at FileIO.FileToFile.main(FileToFile.java:12)

This is a piece of code:

 package FileIO;

 import java.io.*;
 public class FileToFile {

    /**
     * @param args
     */
    public static void main(String[] args)throws IOException {
        // TODO Auto-generated method stub

            FileInputStream i=new FileInputStream("FileToFile.java");// Current file content is wanted to be written...
            FileOutputStream o=new FileOutputStream("M.java"); // Destination file which is also in same place.(Nisarg/src/FileIO(package)...
            int a=0;
            while((a=i.read())!=-1)
            {
                o.write((byte)a);
            }
            o.close();
            i.close();
            System.out.print("Done");

    }
}

What should be done to achieve my requirements? I have searched but I was unable to where to put the file. Thank you in advance..!!

Java cant find your input file FileToFile.java, that's what is basically mean. You either can specify a absolute file path or find out what folder your main class is located at and place your file FileToFile.java there.

Find the current directory of your main class, use System.getProperty("user.dir")

您必须进行更改“ src \\ FileIO \\ FileToFile.java”

package fileIO;

import java.io.*;

public class FileToFile {


   public static void main(String[] args)throws IOException {


 FileInputStream i=new FileInputStream("src\\fileIO\\FileToFile.java");

           FileOutputStream o=new FileOutputStream("F:\\M.java"); 

           int a=0;
           while((a=i.read())!=-1)
           {
               o.write((byte)a);
           }
           o.close();
           i.close();
           System.out.print("Done");

   }
}

You should have to use 1)the absolute path for your file 2)Find the current directory using System.getProperty("user.dir") and place your file there. 3)You can change your current working directory for your application by Go to run configuration >> Arguments >> Other radio button. Then enter an absolute path name as the working directory for the launched application.Place your file in the specified directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM