如何在java程序中打开.dat文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3373155/
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
How to open a .dat file in java program
提问by flyingfromchina
I was handed some data in a file with an .dat extension. I need to read this data in a java program and build the data into some objects we defined. I tried the following, but it did not work
我收到了一个扩展名为 .dat 的文件中的一些数据。我需要在java程序中读取这些数据并将数据构建到我们定义的一些对象中。我尝试了以下,但没有奏效
FileInputStream fstream = new FileInputStream("news.dat");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
Could someone tell me how to do this in java?
有人能告诉我如何在java中做到这一点吗?
回答by BalusC
What kind of file is it? Is it a binary file which contains serialized Java objects? If so, then you rather need ObjectInputStream
instead of DataInputStream
to read it.
它是什么类型的文件?它是包含序列化 Java 对象的二进制文件吗?如果是这样,那么您宁愿需要ObjectInputStream
而不是DataInputStream
阅读它。
FileInputStream fis = new FileInputStream("news.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
// ...
(don't forget to properly handle resources using close()
in finally
, but that's beyond the scope of this question)
(不要忘记使用close()
in正确处理资源finally
,但这超出了本问题的范围)
See also:
也可以看看:
回答by Andrzej Doyle
A .dat
file is usually a binary file, without any specific associated format. You can read the raw bytes of the file in a manner similar to what you posted - but you will need to interpret these bytes according to the underlying format. In particular, when you say "open" the file, what exactlydo you want to happen in Java? What kind of objects do you want to be created? How should the stream of bytes map to these objects?
一个.dat
文件通常是一个二进制文件,没有任何具体的相关格式。您可以以类似于您发布的方式读取文件的原始字节 - 但您需要根据底层格式解释这些字节。特别是,当您说“打开”文件时,您到底想在 Java 中发生什么?你想创建什么样的对象?字节流应该如何映射到这些对象?
Once you know this, you can either write this layer yourself or use an existing API (assuming it's a standard format).
一旦你知道了这一点,你可以自己编写这一层,也可以使用现有的 API(假设它是标准格式)。
For reference, your example doesn't work because it assumes that the binary format is a character representation in the platform's default charset (as per the InputStreamReader
constructor). And as you say it's binary, this will fail to convert the binary to a stream of characters (since, after all, it's not).
作为参考,您的示例不起作用,因为它假定二进制格式是平台默认字符集中的字符表示(根据InputStreamReader
构造函数)。正如您所说的那样,它是二进制的,这将无法将二进制转换为字符流(因为毕竟不是)。
// BufferedInputStream not strictly needed, but much more efficient than reading
// one byte at a time
BufferedInputStream in = new BufferedInputStream (new FileInputStream("news.dat"));
This will give you a buffered stream which will return the raw bytes of the file; you can now either read and process them yourself, or pass this input stream to some library API that will create appropriate objects for you (if such a library exists).
这将为您提供一个缓冲流,它将返回文件的原始字节;您现在可以自己读取和处理它们,或者将此输入流传递给某个库 API,该 API 将为您创建适当的对象(如果存在这样的库)。
回答by Scott
That entirely depends on what sort of file the .dat is. Unfortunately, .dat is often used as a generic extension for a data file. It could be binary, in which case you could use FileInputStream fstream = new FileInputStream(new File("news.dat"));
and call read() to get bytes from the file, or text, in which case you could use BufferedReader buff = new BufferedInputReader(new FileInputStream(new File("news.dat")));
and call readLine() to get each line of text. [edit]Or it could be Java objects in which case what BalusC said.[/edit]
这完全取决于 .dat 是什么类型的文件。不幸的是,.dat 通常用作数据文件的通用扩展名。它可以是二进制的,在这种情况下,您可以使用FileInputStream fstream = new FileInputStream(new File("news.dat"));
并调用 read() 从文件或文本中获取字节,在这种情况下,您可以使用BufferedReader buff = new BufferedInputReader(new FileInputStream(new File("news.dat")));
并调用 readLine() 来获取每一行文本。[编辑]或者它可能是 Java 对象,在这种情况下 BalusC 所说的。[/编辑]
In both cases, you'd then need to know what format the file was in to divide things up and get meaning from it, although this would be much easier if it was text as it could be done by inspection.
在这两种情况下,您都需要知道文件采用什么格式来划分内容并从中获取意义,尽管如果它是文本会容易得多,因为它可以通过检查来完成。
回答by Vinda
Please try the below code:
请尝试以下代码:
FileReader file = new FileReader(new File("File.dat"));
BufferedReader br = new BufferedReader(file);
String temp = br.readLine();
while (temp != null) {
temp = br.readLine();
System.out.println(temp);
}
回答by skmangalam
A better way would be to use try-with-resources so that you would not have to worry about closing the resources.
更好的方法是使用 try-with-resources,这样您就不必担心关闭资源。
Here is the code.
这是代码。
FileInputStream fis = new FileInputStream("news.dat");
try(ObjectInputStream objectstream = new ObjectInputStream(fis)){
objectstream.readObject();
}
catch(IOException e){
//
}