Nisarg
Nisarg

Reputation: 53

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..!!

Upvotes: 0

Views: 105

Answers (3)

Sudheep Vallipoyil
Sudheep Vallipoyil

Reputation: 99

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.

Upvotes: 0

sayee
sayee

Reputation: 46

you have to do changes "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");

   }
}

Upvotes: 0

Andreas Baaserud
Andreas Baaserud

Reputation: 149

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")

Upvotes: 1

Related Questions