0

I'm writing my byte array to a file:

PrintWriter pw = new PrintWriter(new FileOutputStream(fileOutput, true));
pw.write(new String(cryptogram, Charset.defaultCharset()));
pw.close();

Then, I am reading it from the file like this:

String cryptogramString = new String();
while (scPriv.hasNext()) {
    linePriv = scPriv.nextLine();
    cryptogramString += linePriv;
}

But I don't know how to make byte[] from cryptogramString. I'am trying this:

byte[] b = cryptogramString.getBytes(Charset.defaultCharset());
System.out.println(Arrays.toString(b));
System.out.println(Arrays.toString(cryptogram));

But it doesn't return the same values. Does anyone have an idea how to make this right?

5
  • I don't understand doesn't return me same values. What are you comparing? Commented Apr 26, 2016 at 18:21
  • 3
    This doesn't actually sound like something that should be interacting with strings, but rather should be just using InputStream and OutputStream and dealing strictly with bytes? Commented Apr 26, 2016 at 18:22
  • System.out.println(Arrays.toString(b)); is not the same as System.out.println(Arrays.toString(cryptogram)); Commented Apr 26, 2016 at 18:22
  • I have a file that contains xml and then plain text, so i cant read a file as a whole Commented Apr 26, 2016 at 18:23
  • hasNext checks for an available token, while nextLine reads until the next line separator, excluding the line separator characters from the return value. Just read the file into a byte[] directly as previously suggested. Commented Apr 26, 2016 at 18:25

1 Answer 1

2

You should decide whether you are writing text or binary.

Encrypted data is always binary which means you shouldn't be using Reader/Writer/String classes.

try (FileOutputstream out = new FileOutputStream(filename)) {
    out.write(bytes);
}

to read back in

byte[] bytes = new byte[(int) (new File(filename).length())];
try (FileInputstream in = new FileInputStream(filename)) {
    in.read(bytes);
}

I have a file that contains xml and then plain text, so i cant read a file as a whole

You also can't write binary into a text file. You can encode it using base64.

Storing base64 data in XML?

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

6 Comments

I have file structure as follows <xml> <tag> </tag> </xml> Cryptogram How could i read this Cryptogram with FileInputStream without reading xml? I cannot use base64 beacuse it extends the size.
You are mixing text (xml) and binary data in a single file? That is really not a good way to do it.
This are my requirements for this task and i have to do it this way. If it was up to me i would find different way.
@SzymonKaczorowski You can add it, but it won't be XML any more.
@SzymonKaczorowski you have to read it the same as above, but you have to skip the XML first, but no more. I suggest reading it using the binary mode as well to avoid reading too much.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.