Java, Eclipse: Target Files’s Position To Be Read

Java

eclipse file hierarchy
-Correct position to read a text file-

###TODAY’S TIP###
Put the text file directly under the project directory,
not the same level the class that has the method to read the file belongs to.
#################

I faced the trouble that the text file did not be read,
learning topics about input and output stream on Java.

I usually write and run my code on Eclipse.
I tried to run the same code on Windows’ command prompt.
It works as intended.
So the code seems to be correct.
Maybe Eclipse setting is causing the problem.

I got related information at Stack Overflow, searching for any solution.
I moved the target text file to the position, directly under the project directory.
Then my code worked as planned.

I think I should get deeper understanding about Eclipse, too.

////Sample Code///
package input_test;
import java.io.FileInputStream;
import java.io.InputStream;

class Main {

public static void main(String args[]) {

byte[] array = new byte[100];

 

try {

InputStream input = new FileInputStream(“input.txt”);
input.read(array);
String data = new String(array);
System.out.println(data);
input.close();

} catch (Exception e) {

e.getStackTrace();

}

}

}

タイトルとURLをコピーしました