I have the following problem: I need to input a file with 12 lines. Each line consist of 8 characters. I have to output it in a file with 8 lines and 12 characters. I have to read the input line by line and output each line at the same time. So I'm not allowed to read my input first and after i read it just cut in in 8 lines with 12 characters. I'm using BufferedReader to read my file and BufferedWriter to write to my file. So by example:
Input:
12345678
qwertyui
asdfghjk
Output:
12345678qwer
tyuiasdfghjk
Edit: It's an homework assignment indeed.
BufferedWriter bufferedWriter = null;
FileReader fr;
try {
fr = new FileReader(new File(directory to file));
bufferedWriter = new BufferedWriter(new FileWriter(directory to file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
bufferedWriter.write(output);
bufferedWriter.newLine();
line = br.readLine();
}
br.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
This is how i read my inputfile and write to an outputfile, and it's the code I have at the moment.