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