문제

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

올바른 솔루션이 없습니다

다른 팁

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top