I am trying to read some lines from a file in Java. I have 4 lines in the file, but the problem is that it reads only 2 of the lines. Here's the code:
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while (flux_in.readLine() != null)
{
line = flux_in.readLine();
System.out.println(line);
}
It is because you are calling readLine twice as often as you should.
Your first call inside the the while condition just threw the line away.
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while ((line = flux_in.readLine()) != null)
{
System.out.println(line);
}
It does read all of them, although not quite in the way you'd like.
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while (flux_in.readLine()!=null) //one line is read here
{
line = flux_in.readLine(); //the next one here
System.out.println(line);
}
In your code, the call in the loop test consumes a line, and then the call n the loop body consumes another line. So it would only print every other line.
String s = null;
while ((s = flux_in.readLine()) != null)
{
System.out.println(s);
}
BufferedReader is stateful and remembers what has been read from the file already. Every call of readLine() moves the cursor to the next line. You are calling readLine() twice per line: in the while loop and in the line assignment. Try this instead:
String line = flux_in.readLine();
while (line != null) {
System.out.println(line);
line = flux_in.readLine();
}
You're reading the line twice (in the while condition and in the while loop, so it's displaying one and then skipping the next. Change your code to this:
try {
BufferedReader flux_in = new BufferedReader(new FileReader("abc.txt"));
String line;
while ((line = flux_in.readLine()) != null)
System.out.println(line);
} catch(Exception e) { System.out.println("Error"); }
Related
I am writing a file IO method and have a while loop within a try/catch clause. Thus works though my txt file that I am reading must have a new blank line at the end in order for it to work corrctly. If the txt file does not have this blank line then it runs but also produces my catch exception error msg at the end.
Any ideas on how to implement a NoSuchElementException to fix this.
Thanks
Change the whileloop to:
while((line = reader.readLine()) != null){
Sysout...
}
And it will work.
Problem with your code is, that you read a line and than enter the whileloop again.
Scanner in = new Scanner(filename);
File fileName = new File(filename);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch(Exception e) {
System.out.println("ERROR FILE NOT FOUND");
in.close(); // close scanner if file not found
}
replace
String line = reader.readLine();
while(line.length() > 0) {
System.out.println(line);
line = reader.readLine();
}
with
String s;
while((s = reader.readLine()) != null) {
System.out.println(s);
}
i have to count the lines on a file but later in the code I also have to print what's in that file, but I can't use the reader twice it just says null. How can I work this out without creating a bunch of bufferedreader objects?
thanks
Print and count at the same time?
Move the lines to an array then print them?
Make sure you've closed the file before reopening again?
Try closing the buffer, and then re-opening it again.
BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/java/com/william/sandbox/stackoverflow/samples20160306/Demo.java"));
String line = bufferedReader.readLine();
int lineCount = 0;
while(line != null){
lineCount += 1;
line = bufferedReader.readLine();
}
System.out.println("Line count is: " + lineCount);
bufferedReader.close();
bufferedReader = new BufferedReader(new FileReader("src/main/java/com/william/sandbox/stackoverflow/samples20160306/Demo.java"));
line = bufferedReader.readLine();
while(line != null){
System.out.println(line);
line = bufferedReader.readLine();
}
}
You can use BufferedReader's mark() and reset() methods to jump back to a specific position.
try (BufferedReader r = new BufferedReader(new FileReader("somefile.txt"))) {
// marks this position for the next 10 characters read
// after that the mark is lost
r.mark(10);
// do some reading
// jump back to the mark
r.reset();
}
Note that, BufferedReader supports marking but not all Readers do. You can use markSupported() to check.
I have a text file. I want to read it line by line and turn it into an 2-dimensional array. I have written something as follows:
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line = br.readLine();
while( line != null) {
System.out.printf(line);
}
This turns into an infinite loop. I want to move on to the next line after I'm done with reading and printing a line. But I don't know how to do that.
You only read the first line. The line variable didn't change in the while loop, leading to the infinite loop.
Read the next line in the while condition, so each iteration reads a line, changing the variable.
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line;
while( (line = br.readLine() ) != null) {
System.out.printf(line);
}
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line = br.readLine();
while( line != null) {
System.out.printf(line);
// read the next line
line = br.readLine();
}
... or read the line in the while condition (as rgettman pointed out):
String line;
while( (line = br.readLine()) != null) {
System.out.printf(line);
}
Every time I do something like
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
It won't outprint line by line. Instead it will lag for 2-3 seconds then show it all at once. I've tried putting sleep methods etc. How can I make it so it takes its time and goes through each one rather than just lagging and spitting it all out at once?
Try flushing the output.
System.out.flush();
After each System.out.println
This is probably because your file doesn't have carriage return characters at the end of each line. So, it's considering the entire file as one line.
Try sending the lines to an array list. See if the arraylist contains each line. Then try iterating the array list to the website chunk by chunk.
BufferedReader br = new BufferedReader(new FileReader(f));
ArrayList<String> list = new ArrayList<String>();
String line;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
for(String one_line: list){
//send lines to website:
//sendLine(one_line);
}
I'm trying to read a csv file from my java code. using the following piece of code:
public void readFile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
lines = new ArrayList<String>();
String newLine;
while ((newLine = br.readLine()) != null) {
newLine = br.readLine();
System.out.println(newLine);
lines.add(newLine);
}
br.close();
}
The output I get from the above piece of code is every alternative line [2nd, 4th, 6th lines] is read and returned by the readLine() method. I'm not sure why this behavior exists. Please correct me if I am missing something while reading the csv file.
The first time you're reading the line without processing it in the while loop, then you're reading it again but this time you're processing it. readLine() method reads a line and displaces the reader-pointer to the next line in the file. Hence, every time you use this method, the pointer will be incremented by one pointing to the next line.
This:
while ((newLine = br.readLine()) != null) {
newLine = br.readLine();
System.out.println(newLine);
lines.add(newLine);
}
Should be changed to this:
while ((newLine = br.readLine()) != null) {
System.out.println(newLine);
lines.add(newLine);
}
Hence reading a line and processing it, without reading another line and then processing.
You need to remove the first line in a loop body
newLine = br.readLine();
In java 8, we can easily achieve it
InputStream is = new ByteArrayInputStream(byteArr);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
List<List<String>> dataList = br.lines()
.map(k -> Arrays.asList(k.split(",")))
.collect(Collectors.toCollection(LinkedList::new));
outer list will have rows and inner list will have corresponding column values