Java reading text file using two loops - java

I'm away from my computer right now but I had an idea and I really wanna know if it'll work.
Would this rough code work for getting groups of lines out of a text file (using BufferedReader br):
String line;
BufferedReader br = ....;
List<String> list = new ArrayList<String>();
while(line = br.readline() != null){
if(line.equals("Group1"){
while(line = br.readline() != "}"){
list.add(line);
}
}
}
Here would be the text file:
Group1
one
two
three
}
Group2
....
}

Try to use single loop like this:
boolean isGroup=false;
while(line = br.readline() != null){
if(line.equals("Group1"){
isGroup=true;
}
if(line.equals("}") && isGroup)
isGroup=false;
if(isGroup){
//read line and check whether it is null or not
list.add(line);
}
}

Related

How to increment a while loop to read the next line on a file

I am writing code that
first) asks the user for a file name
second) reads the file and puts each line into an ArrayList
third) prints out the ArrayList
My code is reading the file with a BufferedReader, but it is only printing out the first line 25 times instead of printing out the 25 different lines.
This is what my while loop looks like. I don't know how to increment it though
ArrayList<String> stringArray = new ArrayList<String>();
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while(reader.readLine() != null){
stringArray.add(line);
}
return stringArray;
Any thoughts?
You are not reading in the line to the variable on each run, you need to read it in the while loop.
String line = reader.readLine();
while(line != null){
stringArray.add(line);
line = reader.readLine(); // read the next line
}
return stringArray;
This is not the preferred solution. Just showing that it can be done a different way
Or you can use a do...while instead of while...do.
String line;
do {
line = reader.readLine();
if (line != null) {
stringArray.add(line);
}
} while (line != null);
You can see why this isn't the preferred solution. You are doing 2 null checks where you can get away with 1.
while (true) {
String line = reader.readLine();
if (line == null) break;
stringArray.add(line);
}
return stringArray;

How do I use BufferedReader to read lines from a txt file into an array

I know how to read in lines with Scanner, but how do I use a BufferedReader? I want to be able to read lines into an array. I am able to use the hasNext() function with a Scanner but not a BufferedReader, that is the only thing I don't know how to do. How do I check when the end of the file text has been reached?
BufferedReader reader = new BufferedReader(new FileReader("weblog.txt"));
String[] fileRead = new String[2990];
int count = 0;
while (fileRead[count] != null) {
fileRead[count] = reader.readLine();
count++;
}
readLine() returns null after reaching EOF.
Just
do {
fileRead[count] = reader.readLine();
count++;
} while (fileRead[count-1]) != null);
Of course this piece of code is not the recommended way of reading the file, but shows how it might be done if you want to do it exactly the way you attempted to ( some predefined size array, counter etc. )
The documentation states that readLine() returns null if the end of the stream is reached.
The usual idiom is to update the variable that holds the current line in the while condition and check if it's not null:
String currentLine;
while((currentLine = reader.readLine()) != null) {
//do something with line
}
As an aside, you might not know in advance the number of lines you will read, so I suggest you use a list instead of an array.
If you plan to read all the file's content, you can use Files.readAllLines instead:
//or whatever the file is encoded with
List<String> list = Files.readAllLines(Paths.get("weblog.txt"), StandardCharsets.UTF_8);
using readLine(), try-with-resources and Vector
try (BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\weblog.txt")))
{
String line;
Vector<String> fileRead = new Vector<String>();
while ((line = bufferedReader.readLine()) != null) {
fileRead.add(line);
}
} catch (IOException exception) {
exception.printStackTrace();
}

Looping every single lines with readLine();

I am currently stuck with my current code of printing my file out with only 1 line of code in it. I am trying to loop through every single line with readLine() but uanble to achieve it. Stuck with looping either the first row or last row of line in the file.
The purpose of this is to print out exactly the same file with this program but printing it out as a folder with other different files.
try
{
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testsample.csv"));
if((sCurrentLine = br.readLine()) != null)
{
String info = br.readLine();
resultString += sCurrentLine.toString();
}
this.WriteToFile(resultString);
}
String info = br.readLine();
is not helping you. And you need while, not if
while((sCurrentLine = br.readLine()) != null)
{
resultString += sCurrentLine.toString();
}
You are reading only two lines. To read all lines you need while loop.
while((sCurrentLine = br.readLine()) != null)
{
//String info = br.readLine();- > Remove this line.
resultString += sCurrentLine.toString();
}

Java BufferedReader to String Array

I was looking through a lot of diffrent subjects here on stackoverflow but couldn't find anything helpful so far :/
So this is my problem. I am writing a filecopier. The problem occurs already at reading the file. My test docoument got 3 lines of random text. All those 3 lines should get written in a string array. The problem is that only the 2nd line of the textdocument gets written in the array and I can't figure out why. Already debugged it, but didn't get me any further.
I know there are diffrent solutions for a filecopier with diffrent classes etc. But I would really like to get it running with the classes I used here.
String[] array = new String[5];
String datei = "test.txt";
public String[] readfile() throws FileNotFoundException {
FileReader fr = new FileReader(datei);
BufferedReader bf = new BufferedReader(fr);
try {
int i=0;
//String Zeile = bf.readLine();
while(bf.readLine() != null){
array[i] = bf.readLine();
// System.out.println(array[i]); This line is for testing
i++;
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
return array;
You're calling readLine() twice for each iteration of the loop, thereby discarding every other line. You need to capture the value returned by every call to readLine(), because each readLine() call advances the reader's position in the file.
Here's the idiomatic solution:
String line;
while((line = bf.readLine()) != null){
array[i] = line;
i++;
}
Here you read 2 lines:
while(bf.readLine() != null){
array[i] = bf.readLine();
// System.out.println(array[i]); This line is for testing
i++;
}
You have to change your Code to:
String line = null;
while((line =bf.readLine()) != null){
array[i] = line;
// System.out.println(array[i]); This line is for testing
i++;
}
The problem is here :
while(bf.readLine() != null)
readLine() reads a line and returns the same at the same time it moves to the next line.
So instead of just checking if the returned value was null also store it.
String txt = null;
while((txt = bf.readLine()) != null)
array[i++] = txt;
I think its because you are calling readLine() twice. First time in the loop, and then second time when you put it in the array. So, it reads a line at the beginning of the loop (line 1), then first line of code inside the loop (line 2 that you see)
I am use Stream.
Not a. This form only applies to reading text files.
BufferedReader bf = new BufferedReader(fr);
// ...
List<String> lines = bf.lines().collect(Collectors.toList());

Java - Reading From FIle

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"); }

Categories

Resources