简体   繁体   English

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

[英]Writing a long (primitive type) to a file in Java

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). 我有一个很长的数组,我想写入一个.txt文件,以后可以在gedit中打开(每行一个数字)。 I get those values by using a subtraction of two instances of System.currentTimeMillis(). 我通过减去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. 我相信字符串串联将long转换为整数。 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). 如果使用DataOutputStream,则无法使用gedit或任何记事本/文本编辑器将其读回,它看起来就像垃圾(我相信它在写字节)。

Would anyone please let me know how I can fix my problem please? 有人能让我知道如何解决我的问题吗?

Thank you very much! 非常感谢你!

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. 当您遇到这样的代码问题时,编写一个小的测试用例来缩小问题范围通常是有益的。

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");

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. 文本文件最常用的编码之一是ASCII。 With this each byte represents a character in a table. 每个字节代表一个表中的字符。 This table has 128 characters. 该表有128个字符。 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. 当您使用Java方式将字符串写入文件时,这就是正在发生的事情,而gedit可以理解这种编码。 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. 如果将long转换为字符串,则long的最大大小可能如下所示:9223372036854775807。这将占用19个字节。

A long in Java is 64 bits or 8 bytes. Java中的long是64位或8个字节。 When you use DataOutputStream a long gets written to the file as 8 bytes. 使用DataOutputStream时,很长一段时间会以8个字节的形式写入文件。 A text editor like gedit does not understand this encoding. 像gedit这样的文本编辑器无法理解这种编码。

What further complicates things is encodings such as UTF-8. 使事情更加复杂的是诸如UTF-8的编码。

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() 
}

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

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