Read a specific line from a text file - how to ?

Thread Solved
Reply

Join Date: Nov 2007
Posts: 9
Reputation: Krimi is an unknown quantity at this point 
Solved Threads: 0
Krimi Krimi is offline Offline
Newbie Poster

Read a specific line from a text file - how to ?

 
0
  #1
Nov 19th, 2007
hi there!

how do i read a specific line from a text file?

maybe im asking the wrong question, so i will try to explain what im trying to do

i have a file that looks a bit like this(just with 1000 lines):
1 dudea addressa 1000 10000000 maila 2007-11-19
2 dudeb addressb 2000 20000000 mailb 2007-11-19
3 dudec addressc 3000 30000000 mailc 2007-11-19
4 duded addressd 4000 40000000 maild 2007-11-19
5 dudee addresse 5000 50000000 maile 2007-11-19

as you can probably see its:
ID name address zipcode phonenumber mail date

now i need to get this stuff into a 2d array.. i know its something with tokenizer or something like that, but i spent like 3 million hours on this annoying little issue and i just cant figure it out!
so please! someone help me
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,355
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 500
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Read a specific line from a text file - how to ?

 
0
  #2
Nov 19th, 2007
You can use Scanner or a BufferedReader and String.split().
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: Krimi is an unknown quantity at this point 
Solved Threads: 0
Krimi Krimi is offline Offline
Newbie Poster

Re: Read a specific line from a text file - how to ?

 
0
  #3
Nov 19th, 2007
im quite new to java, so my next question is: what is that and how do i use it ? :p
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,355
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 500
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Read a specific line from a text file - how to ?

 
0
  #4
Nov 19th, 2007
http://java.sun.com/javase/6/docs/ap...l/Scanner.html can be used for scanning and processing many kinds of text input from a variety of sources.

http://java.sun.com/javase/6/docs/ap...redReader.html allows for buffered reading from an input stream and has a readLine() method that will read a single line from a stream (file in your case).

String.split() http://java.sun.com/javase/6/docs/ap...va.lang.String) splits a string by the supplied expression and returns the pieces as an array of strings.

The following code fragment opens a BufferedReader against a text file and reads each line.
  1. try {
  2. BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
  3.  
  4. String line = null;
  5. while ((line = reader.readLine()) != null){
  6. System.out.println(line);
  7. }
  8. } catch (Exception e){
  9. e.printStackTrace();
  10. }
With that as a starting point, see if you can get the elements into your array and post back if you run into problems.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: Krimi is an unknown quantity at this point 
Solved Threads: 0
Krimi Krimi is offline Offline
Newbie Poster

Re: Read a specific line from a text file - how to ?

 
0
  #5
Nov 19th, 2007
thanks! i think that bit of code solved half of my problem.. i will read those link and try to figure it out..

great forum btw.. definitely worth a bookmark
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC