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();
}
Related
So I have a text file with very simple text. Each line is simply make,model,vin#. I have about 3 or 4 lines to test. When I print out these lines, only the lines with even indexes get printed. If I don't include the else statement then it gives an out of bounds exception. So for example, with text file input as shown
Hi guys. I have a text file that is only a few lines long. On each line, it is formatted as such:make,model,number. When I run my program, it prints the lines normally until it gets to the third line of the text file(there's only 5 lines). This third line is where I get the index out of bounds exception.
public CarDealershipSystem(File carFile, File associateFile) {
try (BufferedReader br = new BufferedReader(new FileReader(carFile))) {
String line;
for(;;) {
line = br.readLine();
String[] lineArray = line.split(",");
System.out.println(lineArray[0]);
System.out.println(lineArray[1]);
System.out.println(lineArray[2]);
}
}catch(IOException e) {
e.getLocalizedMessage();
e.printStackTrace();
}
You have "line = br.readLine()" in two places in "while" cycle and in "if" block that causes two calls to readLine per cycle. Besides this block is pointless because the "while" condition already handles it.
tldr: remove
if((line = br.readLine()) == null) {
break;
}
you need a break when you reach the end of the file.
String line;
while((line = br.readLine()) != null) { //stop loop when line == null
line = br.readLine();
}
you need to check your input, before split
String[] lineArray = line.split(",");
if (lineArray != null && lineArray.length == 3) { //will help you avoid the ArrayIndexOutOfBoundsException exception
System.out.println(lineArray[0]);
System.out.println(lineArray[1]);
System.out.println(lineArray[2]);
}
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;
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());
I am trying to use the LineNumberReader to get the number of empty lines in a file. However I cannot manage to get such information. the following is the code that I am using
LineNumberReader reader = new LineNumberReader(new FileReader(this.file));
int cnt = 0;
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {
if(lineRead.length == 0){
cnt++;
}
}
reader.close();
System.out.println(cnt);
Does anyone know of how to be able to get such information ?
Try with
if(lineRead.isEmpty()){
or
if(lineRead.trim().isEmpty()){
if you consider empty a line that contains only spaces or tabs
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);
}
}