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 :)

Recommended Answers

All 4 Replies

You can use Scanner or a BufferedReader and String.split().

im quite new to java, so my next question is: what is that and how do i use it ? :p

http://java.sun.com/javase/6/docs/api/java/util/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/api/java/io/BufferedReader.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/api/java/lang/String.html#split(java.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.

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.