反向读取和写入流Java文件[逐字节]
我需要从这个文本文件source.txt
中读取内容,并在这个文本文件destination.txt
中反向写入内容。读取和写入必须使用字节来完成
我用BufferedReader
&BufferedWriter
这会让你把整行作为一个字符串,然后反转它非常简单
但我不知道如何使用字节对字节的反向顺序写入! 谢谢你的帮助
source.txt
有这样一段文字:“操作系统”
在{
以下是代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("source.txt");
out = new FileOutputStream("destination.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
# 1 楼答案
您可以使用RandomAccesFile读取: