此代码正在尝试读取文件,然后将其反转为输出文件。当它写入(不反转)时,输出是相同的。但是当它被反转时,输出将全部写入输出文件的 ONE 行。
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.");
}
我正在使用BufferedInputStream
和BufferedOutputStream