read a file

how to get the file to be read

Page 1 of 1

13 Replies - 1160 Views - Last Post: 10 March 2009 - 08:15 AM Rate Topic: -----

#1 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

read a file

Posted 09 March 2009 - 06:45 PM

 BufferedReader finput = 
		new BufferedReader(new FileReader("ad.txt")); 


I want this to read a my documents file. How can I do that?
Is This A Good Question/Topic? 0
  • +

Replies To: read a file

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 26,999
  • Joined: 10-May 07

Re: read a file

Posted 09 March 2009 - 06:51 PM

Something like the following?
		File in = new File(
				"/Documents and Settings/user/ad.txt");
		BufferedReader reader = new BufferedReader(new FileReader(in));
		String line = "";
		while ((line = reader.readLine()) != null)
		{
			...
		}


Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: read a file

Posted 09 March 2009 - 06:55 PM

@no2pencil, pbl is not gonna like that while line, since you assigned a variable a value in it. :)

Just warnin' ya. ;)

This post has been edited by Locke: 09 March 2009 - 06:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: read a file

Posted 09 March 2009 - 07:04 PM

Use Scanner then you can hasNext() ;)
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 26,999
  • Joined: 10-May 07

Re: read a file

Posted 09 March 2009 - 07:05 PM

View PostLocke, on 9 Mar, 2009 - 07:55 PM, said:

Just warnin' ya. ;)

It's all good. I'm not that great with Java, I pulled some example file reading code.

I'd rather know how to do it correctly.
Was This Post Helpful? 0
  • +
  • -

#6 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: read a file

Posted 09 March 2009 - 07:12 PM

any way to call it straight through this
BufferedReader finput =
new BufferedReader(new FileReader("ad.txt"));


I dont want any extra coding or anything
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: read a file

Posted 09 March 2009 - 07:19 PM

View PostPublic Designs, on 9 Mar, 2009 - 06:12 PM, said:

any way to call it straight through this
BufferedReader finput =
new BufferedReader(new FileReader("ad.txt"));


I dont want any extra coding or anything

If you don't want extra coding use a Scanner
Scanner in = new Scanner(new File("ad.txt"));
while(in.hastNext()) {
   String line = in.nextLine();
}


This post has been edited by pbl: 09 March 2009 - 07:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: read a file

Posted 09 March 2009 - 07:21 PM

If i were not to use a scanner then where would i need to place the file? in the same folder as the java class?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: read a file

Posted 09 March 2009 - 07:25 PM

View Postno2pencil, on 9 Mar, 2009 - 06:05 PM, said:

View PostLocke, on 9 Mar, 2009 - 07:55 PM, said:

Just warnin' ya. ;)

It's all good. I'm not that great with Java, I pulled some example file reading code.

I'd rather know how to do it correctly.

long story no2pencil :)
Locke has good memory

Despite the fact your code is syntaxly correct.. and woul be seen often in C/C++ coding Java avoids to assign a value inside a if/while statement
Locke is refering on an article on Sun site where Sun Engineering thought once at simply forbidding variable assignment inside if/while
so your corrected code (1 more line of code) would be

String line = reader.readLine();				
while ((line  != null)	{
   ...				
   line = reader.readLine();				
} 



At least Locke learned and remember it :D :^:
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: read a file

Posted 09 March 2009 - 08:19 PM

View PostPublic Designs, on 9 Mar, 2009 - 06:12 PM, said:

any way to call it straight through this
BufferedReader finput =
new BufferedReader(new FileReader("ad.txt"));


I dont want any extra coding or anything

Use the full path for the file:
// assuming windows
BufferedReader finput = 
		new BufferedReader(new FileReader("C:\\Documents and Settings\\Owner\\My Documents\\ad.txt"));


Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: read a file

Posted 09 March 2009 - 08:21 PM

View Postpbl, on 9 Mar, 2009 - 08:25 PM, said:

At least Locke learned and remember it :D :^:


I just remember that you don't like that. I do it "no2pencil"'s way, since that was the first few examples that I learned. I could do it your way, but it's more lines. (granted, only by 2) ;) :rolleyes:

This post has been edited by Locke: 09 March 2009 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: read a file

Posted 09 March 2009 - 08:25 PM

View PostPublic Designs, on 9 Mar, 2009 - 06:21 PM, said:

If i were not to use a scanner then where would i need to place the file? in the same folder as the java class?

Question is irrelevant... you will have the same problem if you were using a BufferedReader

Scanner in = new Scanner(new File("ad.txt"));

assumes ad.txt is in the same directory

Scanner in = new Scanner(new File("/data/ad.txt"));

in the subdir data

Scanner in = new Scanner(new File("../../ad.txt"));

2 directories up


[quote name='n8wxs' date='9 Mar, 2009 - 07:19 PM' post='566993']

View PostPublic Designs, on 9 Mar, 2009 - 06:12 PM, said:

any way to call it straight through this
BufferedReader finput =
new BufferedReader(new FileReader("ad.txt"));


I dont want any extra coding or anything

Test for null and all the try and catch
Was This Post Helpful? 1
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: read a file

Posted 09 March 2009 - 09:19 PM

View PostPublic Designs, on 9 Mar, 2009 - 06:21 PM, said:

If i were not to use a scanner then where would i need to place the file? in the same folder as the java class?


If you know how, then you could also use a JFileChooser and yea there are more steps but that is one method to find a file fromt he entire computer not a specific folder
Was This Post Helpful? 0
  • +
  • -

#14 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: read a file

Posted 10 March 2009 - 08:15 AM

I got it solved. thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1