I really need some help...basically, I just wanted to read a text file in Java using Eclipse. Here's my code:
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
//checks where the path is going ?????????????????????
System.out.println(System.getProperty("user.dir"));
//
FileInputStream fstream = new FileInputStream(System.getProperty("user.dir") + "\\sample.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
} catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
} // end of
} //end of FileRead class
I am using Java 1.6; after clicking 'Run as Application', here's the error I get:
----------------------------------------------------------------------------------
C:\Program Files\Eclipse SDK 3.4\workspace\SampleRun
Error: C:\Program Files\Eclipse SDK 3.4\workspace\SampleRun\sample.txt (The system cannot find the file specified)
----------------------------------------------------------------------------------
Here's how my files are organized:
sample.txt file is located at C:\Program Files\Eclipse SDK 3.4\workspace\SampleRun\sample.txt
SampleRun project contains
src (with FileRead.java in it)
bin (with compiled java files)
sample.txt (I moved this under src but still no luck)
PLEASE, PLEASE help...I appreciate it a lot in advance.
Dor
This post has been edited by esmm1969: 05 February 2009 - 06:07 PM

New Topic/Question
Reply




MultiQuote





|