diff --git a/notes/Java IO.md b/notes/Java IO.md index acb95b36..a2dd668b 100644 --- a/notes/Java IO.md +++ b/notes/Java IO.md @@ -69,16 +69,17 @@ public static void listAllFiles(File dir) { ```java public static void copyFile(String src, String dist) throws IOException { - FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dist); + byte[] buffer = new byte[20 * 1024]; + int cnt; // read() 最多读取 buffer.length 个字节 // 返回的是实际读取的个数 // 返回 -1 的时候表示读到 eof,即文件尾 - while (in.read(buffer, 0, buffer.length) != -1) { - out.write(buffer); + while ((cnt = in.read(buffer, 0, buffer.length)) != -1) { + out.write(buffer, 0, cnt); } in.close(); diff --git a/notes/Java 基础.md b/notes/Java 基础.md index b3aed5d6..9da25851 100644 --- a/notes/Java 基础.md +++ b/notes/Java 基础.md @@ -196,7 +196,7 @@ String 不可变性天生具备线程安全,可以在多个线程中安全地 ## String Pool -字符串常量池(String Pool)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定。不仅如此,还可以使用 String 的 intern() 方法在运行过程中将字符串添加到 String Poll 中。 +字符串常量池(String Pool)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定。不仅如此,还可以使用 String 的 intern() 方法在运行过程中将字符串添加到 String Pool 中。 当一个字符串调用 intern() 方法时,如果 String Pool 中已经存在一个字符串和该字符串值相等(使用 equals() 方法进行确定),那么就会返回 String Pool 中字符串的引用;否则,就会在 String Pool 中添加一个新的字符串,并返回这个新字符串的引用。