2 Replies - 49577 Views - Last Post: 05 February 2009 - 06:45 PM Rate Topic: -----

#1 esmm1969  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 09

Read a Text File in Java using Eclipse

Posted 05 February 2009 - 05:43 PM

Hi all gurus and experts,

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


Is This A Good Question/Topic? 0
  • +

Replies To: Read a Text File in Java using Eclipse

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Read a Text File in Java using Eclipse

Posted 05 February 2009 - 06:21 PM

I think it's most likely an incorrect filename coz I tried the code on my computer with another filename and it worked.

This post has been edited by Gloin: 05 February 2009 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 esmm1969  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 09

Re: Read a Text File in Java using Eclipse

Posted 05 February 2009 - 06:45 PM

View PostGloin, on 5 Feb, 2009 - 05:21 PM, said:

I think it's most likely an incorrect filename coz I tried the code on my computer with another filename and it worked.


Thanks for the quick reply...I changed 'sample.txt' to 'MyReadFile' and it worked....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1