Why initialize this byte array to 1024

I am relatively new to Java and I am trying to write a simple Android application. I have a large text file with 3500 lines in the folder with the resources of my applications, and I need to read it in a line. I found a good example on how to do this, but I have a question why the byte array is initialized to 1024. Do I want to initialize it to the length of my text file? Also, do I want to use char , not byte ? Here is the code:

 private void populateArray(){ AssetManager assetManager = getAssets(); InputStream inputStream = null; try { inputStream = assetManager.open("3500LineTextFile.txt"); } catch (IOException e) { Log.e("IOException populateArray", e.getMessage()); } String s = readTextFile(inputStream); // Add more code here to populate array from string } private String readTextFile(InputStream inputStream) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); inputStream.length byte buf[] = new byte[1024]; int len; try { while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } outputStream.close(); inputStream.close(); } catch (IOException e) { Log.e("IOException readTextFile", e.getMessage()); } return outputStream.toString(); } 

EDIT: Based on your suggestions, I tried this approach. It is better? Thanks.

 private void populateArray(){ AssetManager assetManager = getAssets(); InputStream inputStream = null; Reader iStreamReader = null; try { inputStream = assetManager.open("List.txt"); iStreamReader = new InputStreamReader(inputStream, "UTF-8"); } catch (IOException e) { Log.e("IOException populateArray", e.getMessage()); } String String = readTextFile(iStreamReader); // more code here } private String readTextFile(InputStreamReader inputStreamReader) { StringBuilder sb = new StringBuilder(); char buf[] = new char[2048]; int read; try { do { read = inputStreamReader.read(buf, 0, buf.length); if (read>0) { sb.append(buf, 0, read); } } while (read>=0); } catch (IOException e) { Log.e("IOException readTextFile", e.getMessage()); } return sb.toString(); } 
+6
source share
4 answers

This example is not very good. It is full of bad practices (hides exceptions, and does not close threads in finally blocks, does not indicate explicit encoding, etc.). It uses a 1024 byte buffer because it has no way of knowing the length of the input stream.

Read the Java IO tutorial to learn how to read text from a file.

+3
source

You read the file into a buffer of 1024 bytes. These 1024 bytes are then written to outputStream. This process repeats until the entire file is read in outputStream. As JB Nizet noted, the example is filled with bad practices.

+1
source

To read from a file, I use a scanner and StringBuilder.

  Scanner scan = new Scanner(new BufferedInputStream(new FileInputStream(filename)), "UTF-8"); StringBuilder sb = new StringBuilder(); while (scan.hasNextLine()) { sb.append(scan.nextLine()); sb.append("\n"); } scan.close return sb.toString(); 

Try throwing your exceptions instead of catching them. The caller must know that there is a problem reading the file.

Edit: Also note that using a BufferedInputStream is important. Otherwise, it will try to read bytes bytes, which may be slow.

0
source

Do I want to initialize it to the length of my text file? Also, would I not want to use char, not bytes?

Yes, and yes ... and, as the other answers said, you chose an example with a number of errors.

However, there is a theoretical problem that does both; that is, set the buffer length to the file length and use the character buffer, not the byte buffer. The problem is that the file size is measured in bytes, but the buffer size must be measured in characters. This is usually normal, but it is theoretically possible that you will need more characters than the file size in bytes; for example, if the input file used a 6-bit character set and packed 4 characters into 3 bytes.

0
source

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


All Articles