• Post Reply Bookmark Topic Watch Topic
  • New Topic

bufferedreader  RSS feed

 
Natasha Basu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with this program. The sample1.txt has the data given in the comments above but when i run the code only the last line prints out in sample4.txt.

No matter how i change the code all the lines does not display.Can somebody help me with this please.

package com.Multivision1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*sample1.txt has the following data
* Lynn;25;12341334;Female
* Jackie;45;23443434;Female
*
*
*/


public class IoDemo {

static String line1 = null;
static String[] array;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
static String name;

public void readFromFile() {

try {

// Construct the BufferedReader object
bufferedReader = new BufferedReader(
new FileReader("sample1.txt"));

String line = null;
while ((line = bufferedReader.readLine()) != null) {
// Print out the data
line1 = line;
splitDetails();

}

} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();

} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {

if (bufferedReader != null)
bufferedReader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

}

public void splitDetails() {
array = line1.split(";");
//System.out.println("Name: " + array[0] + "\nAge: " + array[1]
//+ " years" + "\nPhone: " + array[2] + "\nM/F: " + array[3]);
writetoFile();
}

public void writetoFile(){

try {
bufferedWriter = new BufferedWriter(
new FileWriter("sample4.txt"));

bufferedWriter.write("Name\t\tAge");
bufferedWriter.write("\n" + array[0] + "\t\t" + array[1]);

// Close the output stream
bufferedWriter.close();

} catch (IOException ioe) {
ioe.printStackTrace();
}

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
IoDemo iod = new IoDemo();
iod.readFromFile();

}

}
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should by now know to use the code button.

And? You are reading multiple lines, splitting them into arrays, and look where you are storing the arrays. See how many arrays are available when you start writing.
 
Natasha Basu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry about the code button. But is there any other way i can store the lines other than arrays.

Thank you.
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... it looks like you are 'opening, writing, closing' the file in a loop. Whenever you write something to a file it does not appends the new lines/contents, it removes the existing file contents and adds the new contents. That's reason the last line is getting stored in the file.

I would suggest doing as below,

Read the contents from file1 and store it in ArrayList.
Open file2
Loop through the ArrayList and write it to file2
Close the file2

Also its good to flush the output
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Natasha,

You can edit your original post and add the code tags to it so it displays nicely.

Your program seems unnecessarily complicated for what you are attempting to do. If you try streamlining it a bit it may become clearer what is going wrong. But to expand on the point Campbell was making, take a look at your writetoFile() method. How often is it being called (e.g., once per file, once per input line, etc.)? A good technique for debugging any program with a logic bug like this is to manually work through a few iterations with pencil and paper and see what it is really doing.
 
Natasha Basu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what i am doing is complicated but that is what was given to me.


 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natasha Basu wrote:I know what i am doing is complicated but that is what was given to me.

Natasha Basu wrote:No matter how i change the code all the lines does not display.

Well, if you're free to change the code any way you want, I would recommend rewriting it from scratch--it's badly written code. However, I noticed that some of your other posts have dealt with sample exam questions where you're given some code and have to explain what it's doing. If that's the case here (and the code is deliberately written in a confusing way), then the earlier replies should help you out.
 
Natasha Basu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. i will try that.
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rereading my earlier response, I see that it might have come across a little bit snarky, which I didn't really intend. It's just that often it's easier to rewrite a block of code from scratch than to try to patch up someone else's programming.

Reading in a file and writing it out to another file is a standard task that you can find a lot of online examples of--I'd recommend looking at the I/O trail at the Java Tutorials. The only extra thing your code needs to do is parsing the input string into its components, and that part is already working okay.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anbarasu Aladiyan wrote: . . . Also its good to flush the output
It is unnecessary to call flush() in that code. I trust you know why?
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is unnecessary to call flush() in that code. I trust you know why?
Thanks for pointing out . Calling close() will cause flush by default.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!