我正在尝试用java (在linux上)编写一个程序,使用RandomAccessFile类来写入文件。
由于一些非常奇怪的原因,它不能工作。最简单的代码也不能工作。当尝试使用时:
RandomAccessFile file = new RandomAccessFile("a.txt", "rw");
file.writeInt(3);
file.close();它会将文件留空或用gibrish填充
我想这与一些我不熟悉的编码问题有关。
有人对此有什么想法吗?
谢谢
发布于 2012-04-04 04:11:35
它只是将一个32位整数写入文件(在本例中是字节序列00 00 00 03)。如果你想把它写成一个字符串,你需要
RandomAccessFile file = new RandomAccessFile("a.txt", "rw");
file.writeBytes(Integer.toString(3));
file.close();发布于 2012-04-04 03:55:44
你最好通过像这样的教程来学习如何使用随机存取文件。
http://www.java-tips.org/java-se-tips/java.io/how-to-use-random-access-file.html
https://stackoverflow.com/questions/10000296
复制相似问题