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?

0

5 Answers 5

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:

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for mentioning finally { in.close() }, because a question at this level may quite possibly overlook it otherwise.
What is the content-type in Post-Man for above file.
Depends on how you add the file to the request body and what the target server supports. For example, web services supporting file uploads from web pages usually only accept multipart/form-data flavor. This is in turn completely independent of the file itself.
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).

Comments

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.

Comments

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

Comments

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){
         //
    }

2 Comments

I think this is a better fit as a comment since it refines the existing answer instead of adding a new solution.
yeah buddy.. even I thought of doing so, but since I am not allowed to comment yet, had to do it that way. cheers..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.