2

I am having this code in Java to read from a file and pass the values to a vector but it seems not to consider the last line.

int lines = 0;
Vector<String> vc = new Vector<String>();
BufferedReader in = new BufferedReader(new FileReader(FILE_NAME));      
while(in.readLine() != null){
    if(lines >= 1){
        vc.addElement(in.readLine());       
    }

    lines++;
}

System.out.println(vc.size());

for(int i = 0; i< vc.size(); i++){          
    System.out.println(vc.get(i));
}

The elements of a file are

4
fdgdfs
sdfg
sdfg
sfdg
sdfg

for example and I want to read from the second line and add them in the vector. The results printing are

3
sdfg
sfdg
null

What seems to be the problem?

Anas
  • 177
  • 1
  • 11
  • 2
    Eating code may be hazardous to health. Jokes apart, `lines>=1` is going to skip the first line since `lines` is initialized to 0. – Chetan Kinger Apr 26 '15 at 12:28
  • 3
    You're calling in.readLine() twice per iteration. Call it once. Also, stop using Vector. It's obsolete for 15 years. And consider using http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29 – JB Nizet Apr 26 '15 at 12:28
  • 2
    Also.. do you really need a `Vector` instead of `ArrayList`? – Maroun Apr 26 '15 at 12:28
  • 1
    Don't use `Vector`. Use `ArrayList` – MadConan Apr 26 '15 at 12:30
  • 1
    @MadConan I wouldn't say "*don't*", recall that `Vector`s are synchronized, sometimes you do want them. – Maroun Apr 26 '15 at 12:32
  • Why `Vector` in 2015? Why `File` in 2015? Java 7+ has `Files.readAllLines()` – fge Apr 26 '15 at 12:32
  • @MarounMaroun when you need a synchronized list, there is `Collections.synchronizedList()`. – JB Nizet Apr 26 '15 at 12:33
  • @JBNizet Indeed, it's more efficient. I still hate to say "don't". – Maroun Apr 26 '15 at 12:34
  • @MarounMaroun There really isn't any good reason to use `Vector` any more. Lots of good info out there. Here's a very popular one: http://stackoverflow.com/a/1386288/2919971 – MadConan Apr 26 '15 at 13:17

2 Answers2

2

Change:

while(in.readLine()!=null)

to:

 while((line = in.readLine())!=null)

Where line is a String you should define before the while loop.

Then use line variable as follows to add element,

vc.addElement(line);

As suggested in comments, you can use arraylist instead of vector, read this it explains why you should do it.

Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
0

Use this code:

package stackoverflow;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class FileReadingLastLine {

    private static final String FILE_NAME = "test";

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        try {
            BufferedReader in = new BufferedReader(new FileReader(FILE_NAME));
            String line = "";
            int lines = 0;
            while ((line = in.readLine()) != null) {
                if (lines >= 1) {
                    list.add(line);
                }
                lines++;
            }

            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Size: " + list.size() + "\n");

        for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();
            System.out.println(string);
        }
    }
}

The output is:

Size: 5

fdgdfs
sdfg
sdfg
sfdg
sdfg

The content of the file named test is:

4
fdgdfs
sdfg
sdfg
sfdg
sdfg
A.B.
  • 460
  • 5
  • 18