Binary file not being read properly in Java -


I am trying to read a binary file in Java using bufferedreader. I have written that the binary file using "UTF-8" encoding. Writing code in a binary file:

  byte [] inMsgBin = null; Try {inMsgBin = String.valueOf (cybertext) .getBytes ("UTF-8"); // SYSTEM.out.println (CIPHER text: complete: write binary: "+ inboxbun);} hold (unsupported encoding exception preceding) {Logger.getLogger (EncDecApp.class.getName ()) .log (Level.SEVERE, null , ex);} (File Optical Stream Out = New FileApplyput Stream (filename + string.valuef (New SimpleDetermate ("YYMDhamdham"). Format (New Date ()) + ".encmsg")) {out.write (inMsgBin) ; out.close ();} hold (IOException pre) {Logger.getLogger (EncDecApp.class.getName ()) .log (Level.SEVERE, null, ex);} System.out.println ("cybertext charCount =" + cypherteext.length ());   

Here 'cybertext' is a string with some content, the total number of characters written in the file is given as 1st. After writing, when I open the binary file in Notepad +++++++++++++++ The selection of all the contents of the file is counted as 19 characters in the total.

Now when I read the same file using BufferedReader using the following lines of code:

  try {DecMessage obj2 = new DecMessage (); Stringbuilder ciphermog = new stingbuilder (); Try (BufferedReader = New BufferedReader (New FileReader (filePath)) (String Templin = ";; FileSelect True =; while ((tempLine = in.readLine ()) = Faucet {cipherMsg.append (tempLine);}} .out.println ("file from: charCount =" + cipherMsg.length ());   

The total number of characters here (stored in 'charcount') is 17 instead of 19.

How can I read all the characters in the file?

Specify the same charset while reading the file.

  try (final BufferedReader br = Files.newbufferedReader (new file (filePath) .toPath (), StandardCharsets.UTF_8))   

UPDATE

Thanks for the file I have now received your problem.

Again: Your file is still readable for a text reader like Notepad (since your characters include extended and control letters, but you are looking at those non-readable characters, but it is still in ASCII.)

Now back to your problem, you have two problems in your code.

  1. You should specify the correct charset in reading the file. Readers are character readers - while reading the bytes will be converted to letters. If you specify the charsets, it will use it to use the default system charset. Then you should try BufferedReader as follows

    (Last BufferedReader br = Files.newBufferedReader (new file (filePath) .toPath (), StandardCharsets.UTF_8)

  2. The second issue, you have letters that contain control letters, while reading the file line by line, the default bufferedReader uses the system's default EOL characters and leave those characters . That's why you're getting 17 instead of 19 (because you have 2 characters in the CR). To avoid this issue, you should read the letters.

    int ch; While ((ch = br.read ()) gt; -1) {buffer.appand (four) f); }

    Overall the method below will return the appropriate text.

      Read String StringKiber Text () {StringBinder Buffer = New StringBuilder (); Try (Last BufferedReader br = Files.newBufferedReader (new file ("C: \\ projects \\ test2201404221017.txt"). ToPath (), StandardCharsets.UTF_8)) {int ch; While ((ch = br.read ()) gt; -1) {buffer.appand (four) f); } Return buffer. Tosting (); } Hold (IOException e) {e.printStackTrace (); Return tap; }}   

    and you can check by

      string s = readCyberText (); Println (s.length ()); Println (s);   

    and as output

      19 ia @ mà © à ?? 6o «& lt; A «9K () IL   

    Note: The length of the string is 19, although when it is displayed it displays only 17 characters. Because the console is considered as AOF and displayed in a different line. But all 19 characters in the string are correct.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -