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