Import the Java. IO. *; Public class Ex10_4 { Public static void main (String args []) { Int n=1; Byte [] a=new byte [100]. Try {File f=new File (" Ex10_4. Java ");//create a file (source) InputStream in=new FileInputStream (f);//create a point to file input stream While ((n=in. Read (a, 0100)).=1) {//not read bytes will return 1 /* loop reads bytes, call int read (byte [], int off, int len), Read the len bytes in a byte array, starting off to specify from which position */ String s=new String (a, 0, n); System. The out. Print (s); } In the close ();//off flow } The catch (Exception e) { System. The out. Println (" the File read Error "+ e); } } }
Declaration of the variable n=1 don't understand, the while loop and the back of the String (a, 0, n) method is a what O bosses solve
CodePudding user response:
InputStream of the read () method will stream of bytes read into a byte array, the return value is read to the number of bytes, if return 1, that is not read data, A, new String (0, n) is the call to a construction method of the String class, Find a JDK API documentation, see will know that the String class constructor has several, But if they don't contact me, I have 1.6, 1.7, 1.8 documents, including 1.6 is all in Chinese, 1.7 is all in English, 1.8 half, including the Chinese translation software translation, looking at a little bit difficult
CodePudding user response:
N=1 is useless, because behind n=in. Read (a, 0100) to give n assignment;
A, String (0, n) is the function of the byte array into a String, but the array is not all need to be converted to a String, only need 0 - n bytes,