Using a byte array

The following code gives a compile error storing char in byte . How to save bytes read by fin.read() function into byte array and print it?

 import java.io.FileInputStream; class IO { public static void main(String args[]) { int i; int j = 0; FileInputStream fin = new FileInputStream("file.txt"); byte[] b = new byte[100]; do { i = fin.read(); j++; b[j] = (char) i; } while (i != -1); System.out.println(b); } } 

Output:

 possible loss of precision found : char required: byte b[j] =(char) i; ^ 1 error 

How do I make it work? Read the file into an array of bytes and then display?

+4
source share
5 answers

Read the file and print it:

  File file = new File("C:\\MyFile.txt"); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; try { fis = new FileInputStream(file); // Here BufferedInputStream is added for fast reading. bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); // dis.available() returns 0 if the file does not have more lines. while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. System.out.println(dis.readLine()); } // dispose all the resources after using them, you need to move this to the finally block and check for null!! fis.close(); bis.close(); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 
+1
source

You can read directly into the byte array using InputStream.read (byte [])

+2
source

Change the following line b[j] = (char) i; on b[j] = (byte) i; .

This will give you another compiler error. But the first problem will be solved.

The first one does not work because Java uses Unicode for type char , therefore it has two bytes. You throw the int value into char , but then try to assign it to the byte variable, and for this you need to explicitly point to byte , it is no longer C or C ++.

You can also consider using the simpler fin.read(b) method, which will read up to 100 bytes in your case and return -1 when EOF is reached. Thus, you do not need to explicitly point i to a byte .

For example, for example:

 import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; class IO { public static void main(String args[]) { byte[] b = new byte[100]; try (FileInputStream fin = new FileInputStream("file.txt");) { while (true) { int i = fin.read(b); if (i < 0) { break; } if (i < b.length) { b = Arrays.copyOf(b, i); } System.out.println(Arrays.toString(b)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 
+1
source

Try using the FileUtils.readFileToByteArray(file) method found in Apache Commons IO . This is easier than writing code manually.

+1
source

For your question, the answer is simple. You convert int to char, but want to save it as a byte. Therefore, you need to convert it to byte during the conversation. i.e

 import java.io.*; class IO{ public static void main(String args[]){ int i; int j=0; FileInputStream fin = new FileInputStream("file.txt"); byte[] b = new byte[100]; do{ i= fin.read(); j++; b[j] =(byte) i; }while( i != -1); System.out.println(b); } } 

Also you do not need to read it as an int. directly read as char or as a byte.

+1
source

Source: https://habr.com/ru/post/1445253/


All Articles