-1

I have a list of objects that has some simple String properties. I want to be able to save those strings to binary so that when you open the file outside the program, you only see 1's and 0's.

I have managed to use FileOutputStreamand saved the strings, however, I can't manage to get it to write to binary. The file reads as clean readable text. I have tried getBytes().

What would be the best approach for this? Keep in mind that I want to be able to read the file later and construct back the objects. Would it be better to use Serializable and save a list of objects?

Here is my FileWriter:

NB: The toString() is custom and returns a String with linebreaks for every property.

public class FileWriter {

    public void write(String fileName, Savable objectToSave ) throws IOException {

        File fileToSave = new File(fileName);

        String stringToSave = objectToSave.toString();

        byte[] bytesToSave = stringToSave.getBytes(StandardCharsets.UTF_8) ;

        try (
                OutputStream outputStream = new FileOutputStream(fileToSave);
                ) {

            outputStream.write(bytesToSave);

        } catch (IOException e) {
            throw new IOException("error");
        }
    }

}
Sam
  • 350
  • 5
  • 18
Mats
  • 223
  • 1
  • 5
  • 15
  • 4
    "save those strings to binary so that when you open the file outside the program, you only see 1's and 0's" FYI, that's not what "binary file" usually means. You either want to encrypt your strings or, at least, encode them. – Federico klez Culloca May 12 '20 at 09:40
  • what does it mean then? – Mats May 12 '20 at 09:44
  • It simply means "not text". – Federico klez Culloca May 12 '20 at 09:53
  • 1
    [Here](http://www.linfo.org/binary_file.html) is a more extensive definition, if you prefer it. – Federico klez Culloca May 12 '20 at 10:01
  • By any chance, did you check https://stackoverflow.com/questions/5632658/how-to-encrypt-or-decrypt-a-file-in-java? – Arvind Kumar Avinash May 12 '20 at 10:06
  • @ArvindKumarAvinash No, I did not. I wasn't really looking to encrypt anything – Mats May 12 '20 at 10:20
  • A binary file just means one where control characters like carriage returns and linefeeds are not treated specially, so whether a file is "text" or "binary" is all in how a program handles it. There's no difference in the file itself. If you want someone to see "1"s and "0"s then write those characters to the file. If you write other characters to the file, anyone who opens it can see them. – David Conrad May 12 '20 at 14:38

1 Answers1

1

If your goal is simply serializing, implementing Serializable and writing them would work, but your string is still going to be readable. You can encrypt the stream, but anyone decompiling your code can still devise a way to read the values.

Martheen
  • 5,198
  • 4
  • 32
  • 55
  • So, if I have understood correctly, serializing an object and writing it to a file is essentially writing a binary file? I don't need it to be secure. – Mats May 12 '20 at 09:52
  • Yes, the default action is using the binary serialization. – Martheen May 12 '20 at 10:50