0

I have a string and want to persist it into a file and be able to retrieve it again into a String.

Something is wrong with my code because It's supposing that I must write something binary non readable but when i Open the file I can read this:

Original string:

[{"name":"asdasd","etName":"111","members":[]}]

Stored string in binary file:

[ { " n a m e " : " a s d a s d " , " e t N a m e " : " 1 1 1 " , " m e m b e r s " : [ ] } ]

I detect two problems:

  1. Is not stored in binary! I can read it. It's supposed to be a confused binary text unreadable but I can read it.
  2. When i retrieve it it's being retrieved with that strange space between the characters. So it doesn't works.

This is my code for storing the string:

public static void storeStringInBinary(String string, String path) {
    DataOutputStream os = null;
    try {
        os = new DataOutputStream(new FileOutputStream(path));        
        os.writeChars(string);
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }        
}

And this is my code for reading it from binary to a String:

public static String retrieveStringFromBinary(String file) {
    String string = null;
    BufferedReader reader = null;
    try {           
        reader = new BufferedReader(new FileReader (file));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();      
        while((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        return stringBuilder.toString();
    } catch (Exception e){
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {               
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return string;
}   
halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

2 Answers2

2

Firstly, there isn't really a distinction between a text file and a binary file. A text file is just a file who's content falls in the range of byte values that correspond to characters.

If you want to encrypt the content of the file so it is unreadable just by catting the file then you will need to choose an appropriate encryption method.

Secondly Mixing Readers/Writers and Streams in Java is never a good idea, pick one style and stick to it.

The problem with your function that saves the string to a file is that you are using the writeChars() method, which from the doc does the following:

Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Since your string is made up of single byte characters this is leading to the padding of your string with null bytes, which are being converted to spaces when read back in. If you change this to writeBytes() then you should get output without the extra null byte.

The null byte will also stop your read function working as the readLine() function will return null on it's first call due to the leading 0x00 in the file.

hardillb
  • 54,545
  • 11
  • 67
  • 105
0

Try this out:

public static void storeStringInBinary(String string, String path) {
    try(ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(path))) {
        os.writeObject(string);
    } catch (IOException e) {
        e.printStackTrace();
    }    
}

public static String retrieveStringFromBinary(String file) {
    String string = null;
    try (ObjectInputStream reader = new ObjectInputStream(new FileInputStream(file))){           
        string = (String) reader.readObject();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
    return string;
} 
Tarun
  • 685
  • 3
  • 16