-2

i want to create a binary file that contain a check of directory files like this one i tried reading this file using this code

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ZeroFileDecryptor {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String file_name = "0";
        try {
            
            FileInputStream fos = new FileInputStream(file_name);
            DataInputStream os = new DataInputStream(fos);
            
            System.out.println("os read");
            
            // Header
            System.out.println(os.readShort());
            System.out.println(os.readInt());
            
            // 0 file size
            System.out.println(os.readInt());
            
            // separator
            System.out.println(os.readShort());
            
            // 1 folder path - custom description
            System.out.println(os.readUTF());
            
            os.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

it contain a header, the file size, a separator (not sure), the path to the file containing the files, a checksum crc32(not sure) and a loop throw the files filename - file extension - filename - file checksum crc32(not sure) when it come to read the filename using os.readUTF() i got a java.io.UTFDataFormatException. is that the right way to parse this file ? how can i get the missing value ? how can i read the filename without the error ?

Community
  • 1
  • 1
Grafikdev
  • 41
  • 4
  • See if this link helps :http://www.devmanuals.com/tutorials/java/corejava/files/java-read-large-file-efficiently.html – Ravindra babu Oct 12 '15 at 16:57
  • @ravindra The OP is trying to parse the data from the file not simply write it into another stream. – Titus Oct 12 '15 at 16:59
  • @ravindra thanks but the link is to copy the data as it is to an output file what i want is to understand the logic (with parsing the existing file) and be able to generate a file like that – Grafikdev Oct 12 '15 at 17:06
  • If you're not sure, find out. Guessing your way through the data file is going to cause you trouble, even in the cases where it "seems to work." Then read about [the methods of DataInput](http://docs.oracle.com/javase/8/docs/api/java/io/DataInput.html). `readUTF()` expects specific byte sequences, which your file does not adhere to. – VGR Oct 12 '15 at 18:46
  • @VGR the file structure is not two complicated when i say not sure is i am not sure 100% just 80%. if i can't use readUTF() do you know another way to parse the file correctly ? thanks – Grafikdev Oct 12 '15 at 18:55

1 Answers1

0

Don't you need to know the length of the filename to read it accurately?

Anyway, I would suggest looking at this question regarding reading binary files. Basically: read parts of the file - or the entire thing - into a byte array and then you can use it to translate the bytes into a more sensible datatype. So you could do this for the filename:

byte[] bytes = new bytes[filenameLength];
try {
    fos.read(bytes);
} catch(IOException e) {
}
String filename = new String(bytes);
Community
  • 1
  • 1
Fjotten
  • 347
  • 3
  • 13