I was studying on a project where I'd like to read a text file and put the content into the program.
For this, I needed to read it, obviously and I tried. But it only read second and fourth and so lines. So, I thought it would be better to work on a different project to figure the problem.
So, here is my code:
Attached you can find the text file I'm working on for this test project. Since my OS language is not English I thought the problem might be it.import java.io.*; public class SolveHere { private File f; private BufferedReader r; private FileReader fr; public static void main(String[] args) { SolveHere m = new SolveHere(); m.fileRead(); } public void fileRead(){ f = new File("D:\\a.txt"); try { fr = new FileReader(f); } catch (FileNotFoundException e) { e.printStackTrace(); } r = new BufferedReader(fr); String w; try { while((r.readLine())!=null){ w = r.readLine(); System.out.println(w); } } catch (IOException e) { e.printStackTrace(); } } }
This is what I expect:
This is what I get:This is first line.
This is second line.
This is third line.
This is fourth line.
This is fifth line.
This is sixth line.
This is seventh file.
I am so annoyed right now as you'd expect Can you guys explain me what is wrong with this code?This is second line.
This is fourth line.
This is sixth line.
null