4

Hi Deployed the following code in eclipse

//import cs1.Keyboard;
import java.util.*;
import java.io.*;
public class Parser
{
    public static void main (String[] args) throws IOException
    {
        String [][] addyArray = new String[50][4];
        for (int j=0; j<50; j++)
        {
            for (int k=0; k<4; k++)
            {
                addyArray[j][k] = "\n";
            }
        }
        FileReader inFile = new FileReader ("sample.txt");
        BufferedReader in = new BufferedReader (inFile);
        String line = "";
        int i = 0, a = 0;
        while(in.ready())
        {
            line = in.readLine();
            while (line != null && line != "\n")
            {
                addyArray[i][a] = line;
                line = in.readLine();
                a++;
                if (line == null) line = "\n";
            }
            i++;
            a = 0;
        }
        for(int j=0; j<3; j++)
        {
            for(int k=0;k<4;k++)
            {
                System.out.println((j+1) + "-" + (k+1) + " " + addyArray[j] [k]);
            }
        }
    }
}

I am getting the following error at this line FileReader inFile = new FileReader ("sample.txt");

at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Parser.main(Parser.java:19)

I placed sample.txt file in the same package folder where the above source code file was placed. I am not sure why I am getting this error. Can you please help me out. Thank you

1
  • What's the actual exception that you're getting? Commented Feb 6, 2012 at 17:41

3 Answers 3

4

When you run a program in Eclipse, the current working directory, by default, is the root directory of your project. You probably have a sub-directory for your source code, so if you put "sample.txt" in it, it won't be found.

Either open the file as "<sub-directory>/sample.txt", or (preferably) move the file to the root of your Eclipse project.

Putting a file in your source code is only appropriate if it's a "resource"; that is, some information that doesn't need to be modified at runtime, but isn't convenient to express as Java source code. For example, localized text and images for a UI is a resource, while a user-specified configuration for window sizes and positions is not. If a file is treated as a resource, you can load it with the getResourceAsStream() method of Class.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. getResourceAsStream() is my preferred method of grabbing resource-type files.
2
new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("sample.txt")));

try this code, to read file, that located in the same directory

Comments

1

It should be placed in resources folder

src/main/java  ... same package
src/main/resource ... same package

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.