| | |
Read a specific line from a text file - how to ?
Thread Solved
![]() |
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
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
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
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. With that as a starting point, see if you can get the elements into your array and post back if you run into problems.
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.
Java Syntax (Toggle Plain Text)
try { BufferedReader reader = new BufferedReader(new FileReader("file.txt")); String line = null; while ((line = reader.readLine()) != null){ System.out.println(line); } } catch (Exception e){ e.printStackTrace(); }
![]() |
Similar Threads
- search in text file (C++)
- delete first line from a text file (C++)
- Reading specific line in a file. / Searching (C++)
- using a "for" loop to read a text file (VB.NET)
- how do i read the last line of a text file? (Python)
- 10 line text file (Java)
- Read and write to an ASCII Text file (Java)
Other Threads in the Java Forum
- Previous Thread: Creating a "Data" folder in my C: Drive using my Java program
- Next Thread: Fractals and the Chaos Game help.
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






