简体   繁体   English

使用DataOutputStream将int写入文件

[英]Write int to file using DataOutputStream

I'm generating random ints and trying to write them down to a file. 我正在生成随机整数,并尝试将其写入文件。 The problem is when I open the file I've created I don't find my ints but a set of symbols like squares etc... Is it a problem of encoding ? 问题是,当我打开创建的文件时,找不到整数,而是找到一组符号,例如正方形等。这是编码问题吗?

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GenerateBigList {

    public static void main(String[] args) {
        //generate in memory big list of numbers in  [0, 100]
        List list = new ArrayList<Integer>(1000);
        for (int i = 0; i < 1000; i++) {
            Double randDouble = Math.random() * 100;
            int randInt = randDouble.intValue();
            list.add(randInt);
        }

        //write it down to disk
        File file = new File("tmpFileSort.txt");
        try {

            FileOutputStream fos = new FileOutputStream("C:/tmp/tmpFileSort.txt");
            DataOutputStream dos = new DataOutputStream(fos);
            writeListInteger(list, dos);
            dos.close();    

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void writeListInteger(List<Integer> list, DataOutputStream dos) throws IOException {
        for (Integer elt : list) {
            dos.writeInt(elt);
        }
    }

}

A partial copy paste from the created file: 从创建的文件中粘贴部分副本:

/   O   a   C   ?       6   N       

From the doc : 文档

 public final void writeInt(int v) throws IOException
    Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.

There is no encoding problem. 没有编码问题。 That is what you see when you open a binary file with a text editor. 这就是使用文本编辑器打开二进制文件时看到的。 Try to open with a hex editor. 尝试使用十六进制编辑器打开。

Those "symbols" are your ints. 这些“符号”就是您的观点。 That's what binary files look like if you open them in a text editor. 如果在文本编辑器中打开二进制文件,则二进制文件看起来就是这样。 Notice that the file is exactly 4000 bytes in size, and you wrote 1000 ints, at 4 bytes each. 请注意,该文件的大小恰好为4000字节,并且您写入了1000个int,每个4个字节。

If you read the file in with a DataInputStream you will get the original values back: 如果使用DataInputStream读取文件,则将获得原始值:

try (DataInputStream dis = new DataInputStream(
    new BufferedInputStream(new FileInputStream("C:/tmp/tmpFileSort.txt")))) {
    for (int i = 0; i < 1000; i++) {
        System.out.println(dis.readInt());
    }
} catch (IOException e) {
    throw new RuntimeException(e);
}

It writes binary, not text. 它写二进制而不是文本。 Your expectations are misplaced. 您的期望放错了地方。 If you want text, use a Writer. 如果需要文本,请使用Writer。

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

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