Question

This code is trying to read a file then reverse it to an output file. When it writes it (without reversing) the output is the same. But when it is reversed the output is written ALL on ONE line in the output file.

  int i;
    int x = 0;
    int[] ar = new int[9999];
    BufferedInputStream fin;
    BufferedOutputStream fout;
    try {

        File f1 = new File("C:/Users/NetBeansProjects/QuestionOne/input.txt");
        File f2 = new File("C:/Users/NetBeansProjects/QuestionOne/output.txt");
        fin = new BufferedInputStream(new FileInputStream(f1));
        fout = new BufferedOutputStream(new FileOutputStream(f2));


        while ((i = fin.read()) != -1) { //reads file into an array
            ar[x] = i;
            x++;
        }

        for(int y = (x-1); y >= 0; y--){ 
        //writes to a file from the end of the array
            fout.write(ar[y]);
        }

        System.out.println();
        fin.close();
        fout.close();
        } catch (FileNotFoundException e) {
        System.out.println("File is NOT found.");
    }

I'm using BufferedInputStream and BufferedOutputStream

No correct solution

OTHER TIPS

Probably you are reading \r\n and writing back \n\r.

You have to handle \r\n as a separate entity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top