Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting to and reading from file - Java - Not reading what I wrote into file
    primarykey
    data
    text
    <p>I'm executing an encryption algorithm and I need your help regarding writing to and reading from a <code>.xtt</code> file in Java. As part of the encryption, I basically need to write Base64 encoded bytes into a <code>.txt</code> file and read these exact bytes, decode them and use them to execute the decryption process. </p> <p>I seem to be reading something different compared to what I'm writing into the <code>.txt</code> file. Basically when I check the bytearray I'm writing into the file it is reads as <code>[B@56e5b723</code> but when I read it of my file it produces <code>[B@35a8767</code>. </p> <p>Here's the outcome as printed in my Java console:</p> <pre><code>***Numbs converted to ByteArray is as follows: [B@56e5b723 Size of NumbsByteArray is: 10 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS STARTED -x-x-x-x-x 6,7,8,9,10 has been received as a byte array in WriteByteArrayToFile(byte[] encoded): [B@56e5b723 6,7,8,9,10 IS TO BE WRITTEN TO THE FILE: /Users/anmonari/Desktop/textfiletwo.txt bs.write(encoded); HAS BEEN CALLED -x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS ENDED -x-x-x-x-x ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -x-x-x-x-x ReadByteArray() HAS STARTED -x-x-x-x-x fileData read as bytes is: [B@35a8767 Size of fileData is: 10 fileDataString when converted to a string using String object is���������� fileDataString when converted to a string using fileDataStringTwo.toString()[B@35a8767 fileDataString.getBytes(); is: [B@2c6f7ce9 -x-x-x-x-x ReadByteArray() HAS ENDED -x-x-x-x-x*** </code></pre> <p>Below is my code:</p> <pre><code>package com.writeandreadfromfile; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class WriteAndRead { public static void main(String j[]) { String Numbs = "6,7,8,9,10"; byte[] NumbsByteArray = Numbs.getBytes(); System.out.println("Numbs converted to ByteArray is as follows: " + NumbsByteArray); System.out.println("Size of NumbsByteArray is: " + NumbsByteArray.length); System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); WriteByteArrayToFile(NumbsByteArray); System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); try { ReadByteArrayFromFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Write ByteArray To File public static void WriteByteArrayToFile(byte[] NumbsByteArray) { System.out.println("\n-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS STARTED -x-x-x-x-x"); System.out.println("6,7,8,9,10 has been received as a byte array in WriteByteArrayToFile(byte[] encoded): " + NumbsByteArray); String fileName = "/Users/anmonari/Desktop/textfiletwo.txt"; System.out.println("6,7,8,9,10 IS TO BE WRITTEN TO THE FILE: " + fileName); BufferedOutputStream bs = null; try { FileOutputStream fs = new FileOutputStream(new File(fileName)); bs = new BufferedOutputStream(fs); bs.write(NumbsByteArray); System.out.println("bs.write(encoded); HAS BEEN CALLED"); bs.close(); bs = null; } catch (Exception e) { e.printStackTrace(); } if (bs != null) try { bs.close(); } catch (Exception e) {} System.out.println("-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS ENDED -x-x-x-x-x"); } // Read ByteArray To File public static void ReadByteArrayFromFile() throws IOException { // Create FileInputStream and feed it the file name System.out.println("-x-x-x-x-x ReadByteArray() HAS STARTED -x-x-x-x-x"); File file; try { file = new File("/Users/anmonari/Desktop/textfiletwo.txt"); // Create the object of DataInputStream DataInputStream in = new DataInputStream((new FileInputStream(file))); byte[] fileData = new byte[(int)file.length()]; System.out.println("fileData read as bytes is from file: " + fileData); System.out.println("Size of fileData is: " + fileData.length); //String fileDataString = in.readLine(); String fileDataString = new String(fileData); System.out.println("fileDataString when converted to a string using String object is" + fileDataString); String fileDataStringTwo = fileData.toString(); System.out.println("fileDataString when converted to a string using fileDataStringTwo.toString()" + fileDataStringTwo); fileDataString.getBytes(); System.out.println("fileDataString.getBytes(); is: " + fileDataString.getBytes()); //Close the input stream in.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("-x-x-x-x-x ReadByteArray() HAS ENDED -x-x-x-x-x"); } } </code></pre> <p>Any assistance regarding how to read from a file the exact byte array you wrote onto a file is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload