I am trying to import a list from a txt file, but the reader skips the first line and also adds the last "null" line that the file writer adds.

I fixed the null part but I can't seem to be able to add the first line in the list.

BufferedReader bf = new BufferedReader( new FileReader(importpath+"\\additional_info\\"+name+".txt"));
            //System.out.println(importpath+"\\"+name+".txt");
            String line = bf.readLine();
            listModel.removeAllElements();
            while (line != null) {
                line = bf.readLine();
                if(line == null){

                }else{
                    listModel.addElement(line);
                }
            }
            bf.close();
            System.out.println(listModel);
            clipsLineList.setModel(listModel);

Recommended Answers

All 4 Replies

ok it was stupidly easy and I am still hitting my head on the wall, inside the while, the first line is line=bf.readLine() right? well with this it skips the first line so I simply needed to add this after the else and everything works

Just FYI the “standard” way to code this (saving duplicated reads and tests) is simply

while ((String line = br.readLine()) != null) {
   // process line
}

I know but for some reason it didn't seem to work as it should, I will try to refactor later, but first I want it to work.
Probably a netbeans freak out cause it sees it as a false code or something.

My mistake (typing while watching to), you need to declare String line before the while loop.

commented: Well either way I need to fix things up make it work as should and will refactor after that :P +0
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.