4

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?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
flyingfromchina
  • 9,571
  • 12
  • 35
  • 38

5 Answers5

8

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    +1 for mentioning `finally { in.close() }`, because a question at this level may quite possibly overlook it otherwise. – Andrzej Doyle Jul 30 '10 at 16:23
8

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

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
3

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.

Scott
  • 1,869
  • 3
  • 20
  • 25
1

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);
}
Vinda
  • 213
  • 3
  • 7
  • 24
0

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){
         //
    }
skmangalam
  • 359
  • 2
  • 8
  • I think this is a better fit as a comment since it refines the existing answer instead of adding a new solution. – suvartheec Jun 12 '18 at 06:16
  • yeah buddy.. even I thought of doing so, but since I am not allowed to comment yet, had to do it that way. cheers.. – skmangalam Jun 12 '18 at 06:26