简体   繁体   English

WriteInt-RandomAccessFile - java

[英]WriteInt-RandomAccessFile - java

i am trying to write a program in java (on linux) using RandomAccessFile class for writing to files.我正在尝试使用 RandomAccessFile class 在 java(在 Linux 上)编写一个程序来写入文件。

for some really weird reason it is not working.由于某些非常奇怪的原因,它无法正常工作。 the most simplest code does not work.最简单的代码不起作用。 when try to use:尝试使用时:

RandomAccessFile file = new RandomAccessFile("a.txt", "rw");
file.writeInt(3);
file.close();

it ether leaves the file blank or fills it with gibrish以太将文件留空或用乱码填充

i assume it has to do with some encoding issue i am not familiar with.我认为这与我不熟悉的一些编码问题有关。

any one have any thoughts about it?有人对此有任何想法吗?

thank you谢谢你

It simply writes a 32-bit integer to the file (in your case it is the bytes sequence 00 00 00 03).它只是将一个 32 位 integer 写入文件(在您的情况下,它是字节序列 00 00 00 03)。 If you want to write it as a string, you need to如果你想把它写成一个字符串,你需要

    RandomAccessFile file = new RandomAccessFile("a.txt", "rw");
    file.writeBytes(Integer.toString(3));
    file.close();

You would be best served going thru a tutorial like this one to learn how to use random access files你最好通过像这样的教程来学习如何使用随机访问文件

http://www.java-tips.org/java-se-tips/java.io/how-to-use-random-access-file.html http://www.java-tips.org/java-se-tips/java.io/how-to-use-random-access-file.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM