flyingfromchina
flyingfromchina

Reputation: 9691

How to open a .dat file in java program

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

FileInputStream fstream = new FileInputStream("news.dat");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

Could someone tell me how to do this in java?

Upvotes: 4

Views: 43078

Answers (5)

skmangalam
skmangalam

Reputation: 369

A better way would be to use try-with-resources so that you would not have to worry about closing the resources.

Here is the code.

    FileInputStream fis = new FileInputStream("news.dat");
    try(ObjectInputStream objectstream = new ObjectInputStream(fis)){

          objectstream.readObject();
    }
    catch(IOException e){
         //
    }

Upvotes: 0

Vinda
Vinda

Reputation: 223

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

Upvotes: 1

Andrzej Doyle
Andrzej Doyle

Reputation: 103787

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 exactly do 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?

Once you know this, you can either write this layer yourself or use an existing API (assuming it's a standard format).

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).

// 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).

Upvotes: 8

Scott
Scott

Reputation: 1929

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]

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.

Upvotes: 3

BalusC
BalusC

Reputation: 1108692

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.

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)

See also:

Upvotes: 8

Related Questions