用 Java 将 long(原始类型)写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7409953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:50:32  来源:igfitidea点击:

Writing a long (primitive type) to a file in Java

javafilelong-integer

提问by Jary

Hi all,

大家好,

I have an array of long that I would like to write into a .txt file that I can later open in gedit (one number per line). I get those values by using a subtraction of two instances of System.currentTimeMillis().

我有一个 long 数组,我想将它写入一个 .txt 文件中,稍后我可以在 gedit 中打开该文件(每行一个数字)。我通过使用 System.currentTimeMillis() 的两个实例相减得到这些值。

I use the following code:

我使用以下代码:

BufferedWriter out = new BufferedWriter(new FileWriter("latency.txt"));
for (int i = 0; i < USER_LIMIT; ++i) {
    out.write(latency[i] + "\n");
}
out.close();

When looking at the file, I do see:

查看文件时,我确实看到:

0
1
1
0

I believe the string concatenation converted the long into an integer. If I use the DataOutputStream, then I cannot read it back with gedit or any notepad/text editor, it just looks like garbage (I believe it's writing bytes).

我相信字符串连接将 long 转换为整数。如果我使用 DataOutputStream,则无法使用 gedit 或任何记事本/文本编辑器读回它,它看起来就像垃圾(我相信它正在写入字节)。

Would anyone please let me know how I can fix my problem please?

请有人告诉我如何解决我的问题吗?

Thank you very much!

非常感谢你!

回答by Brian Roach

There is nothing wrong with your code. What you think is in latency... isn't.

您的代码没有任何问题。你认为在latency......不是。

public static void main(String[] args) throws IOException
{
    long[] latency = { 123456789000L, 234567890000L, 345678901000L };

    BufferedWriter out = new BufferedWriter(new FileWriter("latency.txt"));
    for (int i = 0; i < 3; ++i) {
        out.write(latency[i] + "\n");
    }
    out.close();
}

produces:

产生:

$ more latency.txt 
123456789000
234567890000
345678901000

When you're having a problem with code like this, it's often beneficial to write a small test case to narrow down the problem.

当您遇到这样的代码问题时,编写一个小测试用例来缩小问题范围通常是有益的。

回答by Ian Dallas

Cast to a Long and use toString:

转换为 Long 并使用 toString:

out.write(((Long)latency[i]).toString() + "\n");

Or just use the static toString:

或者只使用静态 toString:

out.write(Long.toString(latency[i]) + "\n");

回答by Sarel Botha

When you use DataOutputStream to write a long it is using a different encoding of your data than gedit understands.

当您使用 DataOutputStream 写入 long 时,它使用的数据编码与 gedit 理解的不同。

One of the most common encodings used for text files is ASCII. With this each byte represents a character in a table. This table has 128 characters. When you write a string to a file using Java the way you're doing it this is what is happening and gedit understands that encoding. If you convert a long to a string the maximum size a long can be would look like: 9223372036854775807. That would take up 19 bytes.

用于文本文件的最常见的编码之一是 ASCII。有了这个,每个字节代表表中的一个字符。该表有 128 个字符。当您使用 Java 将字符串写入文件时,您这样做的方式就是正在发生的事情,并且 gedit 理解该编码。如果将 long 转换为字符串,则 long 的最大大小将如下所示:9223372036854775807。这将占用 19 个字节。

A long in Java is 64 bits or 8 bytes. When you use DataOutputStream a long gets written to the file as 8 bytes. A text editor like gedit does not understand this encoding.

Java 中的 long 是 64 位或 8 个字节。当您使用 DataOutputStream 时,long 将作为 8 个字节写入文件。像 gedit 这样的文本编辑器无法理解这种编码。

What further complicates things is encodings such as UTF-8.

更复杂的是诸如 UTF-8 之类的编码。

回答by Nate W.

You don't need to append the newline to the number, so you can break them apart and avoid this problem. Try this:

您不需要将换行符附加到数字上,因此您可以将它们分开并避免此问题。试试这个:

for ( int i = 0; i < USER_LIMIT; ++i ) {
    out.write( String.valueOf(latency[i]) ); // Or Long.toString(long)
    out.write( '\n' ); // Or simply out.newLine() 
}