DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Read a specific line from a text file - how to ? (http://www.daniweb.com/forums/thread97518.html)

Krimi Nov 19th, 2007 2:52 pm
Read a specific line from a text file - how to ?
 
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 :)

Ezzaral Nov 19th, 2007 2:59 pm
Re: Read a specific line from a text file - how to ?
 
You can use Scanner or a BufferedReader and String.split().

Krimi Nov 19th, 2007 3:01 pm
Re: Read a specific line from a text file - how to ?
 
im quite new to java, so my next question is: what is that and how do i use it ? :p

Ezzaral Nov 19th, 2007 3:45 pm
Re: Read a specific line from a text file - how to ?
 
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.
        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();
        }
With that as a starting point, see if you can get the elements into your array and post back if you run into problems.

Krimi Nov 19th, 2007 3:56 pm
Re: Read a specific line from a text file - how to ?
 
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 :)


All times are GMT -4. The time now is 2:16 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC