1

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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user1242750
  • 13
  • 1
  • 1
  • 4
  • 2
    and your effort so far? we're not going to do it for you if that's what you are expecting... – Nim Mar 01 '12 at 13:50
  • http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html, http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedWriter.html – weekens Mar 01 '12 at 13:50
  • 1
    If this is `[homework]` you should tag it as such. I suggest you ask a question, unless you expect people to do your homework for you. ;) – Peter Lawrey Mar 01 '12 at 13:51

2 Answers2

6

Use the read method of Reader class. (FileReader is a descendant of Reader). I'm not going to implement the whole logic but here is a skeleton to work on.

FileReader inputStream = null;
FileWriter outputStream = null;

try {
    inputStream =
        new FileReader("inputfile.txt");
    outputStream =
        new FileWriter("outputfile.txt");

    int c;
    int counter = 1;
    while ((c = inputStream.read()) != -1) {
        //keep a counter that will cycle for 12 characters
        //check if c represents a alphabet or number, write it to file else skip 
        //when counter is 12 write a newline
        outputStream.write(c);
    }
} finally {
    if (inputStream != null) {
        inputStream.close();
    }
    if (outputStream != null) {
        outputStream.close();
    }
}
bluish
  • 26,356
  • 27
  • 122
  • 180
John Eipe
  • 10,922
  • 24
  • 72
  • 114
0

The read method allows you to control how many characters to read:

See BufferedReader#read.

Same with write

Allan
  • 2,889
  • 2
  • 27
  • 38