2

I am writing data with types as int, char, double, byte, and boolean to and from files.

I have the methods that write my data to the files.

For the reading method, I am having the content of the files put into an ArrayList and then transferring them into a plain array. However in order to do that I have to know what the data type of the file's contents are.

So my question here is:

  • How do I check to see what the data type of the contents of a random file is?
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
Heiland
  • 21
  • 2
  • Hi, post some code that will be more clear! – BilalDja Sep 29 '14 at 20:23
  • 4
    You can't. You have to remember to read them in the order that you wrote them. Once they're in a file, they're just bytes. – Blorgbeard Sep 29 '14 at 20:26
  • 1
    see this http://stackoverflow.com/questions/17293991/how-to-write-and-read-java-serialized-objects-into-a-file – Richard Chambers Sep 29 '14 at 20:34
  • you can store it like `type length value type length value` – Logan Murphy Sep 29 '14 at 20:34
  • http://stackoverflow.com/questions/447898/what-is-object-serialization as well as http://stackoverflow.com/questions/15788182/java-serialization – Richard Chambers Sep 29 '14 at 20:39
  • My guess this is some sort of homework assignment, but just in case. You might take a look at [Apache Tika](http://tika.apache.org/). It has the ability to crack open many different file types and extract metadata. – hooknc Sep 29 '14 at 21:58

2 Answers2

0

You access files via Streams. What you read out of a file depends on the stream-class you use to access the file.

There are two main groups in Java (up to Java 7): 1. The "...Reader" classes. Here the content of the file is read as sequence of chars. 2. The "...Stream" classes. Here the content of the file is read as a sequence of bytes.

However, you can write and read Java Objects to and from a file "directly" via the ObjectOutputStream and the ObjectInputStream classes. With them you ca read/write serialized Java-objects and primitive datatypes. With this you could check what you want in the following way:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("filename"));
Object o = ois.readObject();
if (o instanceof)
//... checked

(This only works if the content of the file are serialized java objects.)

0

There's is no simple generic way to do this automatically.

If you want to retain additional information as the type, you will have to store it along with the data.

Here are some examples of how you can store the type info.

  • Using annotation. Always store the type explicitly with the related data, e.g. if the first token on a line is equal to "type3" then the following data represents a floating point number (for example).

    type1 herecomesthedata1
    type2 11011010111011010
    type3 55.67
    

    For more complex data with trees of variables use a well known data annotation standard as JSON or XML.

  • Using structure. Always have the variables written in the same order, e.g. the first token on a line is always an integer and the next is always floating point etc. Use this information when reading the data.

    123 43.11
    456 78.90
    
  • Use Java's built in serialization utilites as ObjectOutputStream and ObjectInputStream (only works on primitive types and objects that implement java.io.Serializable).

    Example (using structure to determine type):

    int i = 5;
    double d = 7.3;
    
    try (ObjectOutputStream out = new ObjectOutputStream(
                         new FileOutputStream("test.dat"))) {
        out.writeInt(i);
        out.writeDouble(d);
    } catch (IOException e) {
        System.err.println("An error occured writing to file");
    }
    
    i = 0;
    d = 0.0;
    
    try (ObjectInputStream in = new ObjectInputStream(
                       new FileInputStream("test.dat"))) {
        i = in.readInt();
        d = in.readDouble();
    } catch (IOException e) {
        System.err.println("An error occured reading from file");
    }
    
    System.out.println("i = " + i + " and d = " + d); // 5 and 7.3
    
Felix Glas
  • 15,065
  • 7
  • 53
  • 82