From 2117800db205c2ea067de6d5fc98a46e1d6b95c2 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 21 Mar 2016 12:40:36 +0800 Subject: [PATCH 01/12] Creating a memory leak with Java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加Creating a memory leak with Java [closed] 文章的翻译,目前只是把问题翻译了一下 --- contents/creating-a-memory-leak-with-java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 contents/creating-a-memory-leak-with-java diff --git a/contents/creating-a-memory-leak-with-java b/contents/creating-a-memory-leak-with-java new file mode 100644 index 0000000..133c557 --- /dev/null +++ b/contents/creating-a-memory-leak-with-java @@ -0,0 +1,2 @@ +如何使用Java创建一个内存泄漏的程序 +问题:我在一个面试的过程中被问到如何使用Java创建一个内存泄漏的程序。毫无疑问地说,我当时哑口无言,根本不知道如何开始编码。 From e459c4ca2d964f5dca4372baedf64de1a65b3ee4 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 21 Mar 2016 15:07:11 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=9B=9E?= =?UTF-8?q?=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 回答第一版,可能有些地方并不准确,需要继续完善。 --- contents/creating-a-memory-leak-with-java | 28 +++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/contents/creating-a-memory-leak-with-java b/contents/creating-a-memory-leak-with-java index 133c557..8fc0a2b 100644 --- a/contents/creating-a-memory-leak-with-java +++ b/contents/creating-a-memory-leak-with-java @@ -1,2 +1,26 @@ -如何使用Java创建一个内存泄漏的程序 -问题:我在一个面试的过程中被问到如何使用Java创建一个内存泄漏的程序。毫无疑问地说,我当时哑口无言,根本不知道如何开始编码。 +# 如何使用Java创建一个内存泄漏的程序 + +## 问题: +我在一个面试的过程中被问到如何使用Java创建一个内存泄漏的程序。毫无疑问地说,我当时哑口无言,根本不知道如何开始编码。 + +## 解答 +在Java下有一个很好的方法来创建内存泄漏程序--通过使得对象不可访问但任然存储在内存中。 +1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。 +2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B +3. 在类B申请一块很大的连续内存(例如:new byte[1000000]), +并使用一个静态成员变量中存储该空间的一个强引用,之后在一个ThreadLocal中存储类B对象的引用。 +虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。 +4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。 +5. 重复上述步骤 + +上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用, +这样就可以保存一个指向该类(类B)的引用,而类B又保存了一个指向其ClassLoader的引用。 +而ClassLoader又保存了一个通过它加载的所有类的引用。 +这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。 + +这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。 +因为应用容器使用上述所说的线程,每次重新部署应用是,应用容器都会使用一个新的ClassLoader。 + +具体代码可以参考:https://gist.github.com/dpryden/b2bb29ee2d146901b4ae + +stackoverflow原址:http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java From 2dc09d98aff1a22efdd69ab51cc1959fbc702720 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 21 Mar 2016 15:15:39 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 给文件名添加了.md后缀,这样能显示mardown定义的格式。 --- ...-memory-leak-with-java => creating-a-memory-leak-with-java.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contents/{creating-a-memory-leak-with-java => creating-a-memory-leak-with-java.md} (100%) diff --git a/contents/creating-a-memory-leak-with-java b/contents/creating-a-memory-leak-with-java.md similarity index 100% rename from contents/creating-a-memory-leak-with-java rename to contents/creating-a-memory-leak-with-java.md From 2741d4a0dc8cb9b4f6f80d1d280134e4a544e9fe Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 21 Mar 2016 15:17:14 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在每一步后面添加了空白行,改进显示效果 --- contents/creating-a-memory-leak-with-java.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md index 8fc0a2b..f763f30 100644 --- a/contents/creating-a-memory-leak-with-java.md +++ b/contents/creating-a-memory-leak-with-java.md @@ -5,12 +5,17 @@ ## 解答 在Java下有一个很好的方法来创建内存泄漏程序--通过使得对象不可访问但任然存储在内存中。 + 1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。 + 2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B + 3. 在类B申请一块很大的连续内存(例如:new byte[1000000]), 并使用一个静态成员变量中存储该空间的一个强引用,之后在一个ThreadLocal中存储类B对象的引用。 虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。 + 4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。 + 5. 重复上述步骤 上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用, From ecc7b1e42c261fc5ea197d9efa6dce2f77215c5e Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 21 Mar 2016 15:54:25 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=8D=E5=BD=93?= =?UTF-8?q?=E7=9A=84=E8=AF=B4=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改了一些不正确的说法,并且添加了一些个人理解 --- contents/creating-a-memory-leak-with-java.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md index f763f30..1b5644d 100644 --- a/contents/creating-a-memory-leak-with-java.md +++ b/contents/creating-a-memory-leak-with-java.md @@ -8,10 +8,10 @@ 1. 应用程序创建一个长期运行的线程A 或者 使用一个线程池来加快泄漏的速度。 -2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B +2. 线程A使用ClassLoader(用户可以自定义)加载一个类 B。 3. 在类B申请一块很大的连续内存(例如:new byte[1000000]), -并使用一个静态成员变量中存储该空间的一个强引用,之后在一个ThreadLocal中存储类B对象的引用。 +并使用一个静态成员变量保存该空间的一个强引用,之后在一个ThreadLocal对象中存储类B对象的引用。 虽然泄漏这个类的一个实例就足够了,但是也可以通过申请多个实例的方法来加快内存泄漏的速度。 4. 线程A清理所有指向自定义类或者通过ClassLoadeer加载的引用。 @@ -19,13 +19,21 @@ 5. 重复上述步骤 上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用, -这样就可以保存一个指向该类(类B)的引用,而类B又保存了一个指向其ClassLoader的引用。 +而该对象又保存了一个指向其类的引用,这个类又保存了一个指向其ClassLoader的引用, 而ClassLoader又保存了一个通过它加载的所有类的引用。 这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。 +******************************下方为个人理解************************************ +通过一个简单的图来描述上述关系: +ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj +注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象 +******************************上方为个人理解************************************ + 这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。 因为应用容器使用上述所说的线程,每次重新部署应用是,应用容器都会使用一个新的ClassLoader。 具体代码可以参考:https://gist.github.com/dpryden/b2bb29ee2d146901b4ae +参考:http://frankkieviet.blogspot.com/2006/10/classloader-leaks-dreaded-permgen-space.html + stackoverflow原址:http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java From 4d6508b377492e678865929ff481f6bfc5aacd12 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 21 Mar 2016 15:55:21 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对于个人理解部分修改了下格式 --- contents/creating-a-memory-leak-with-java.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md index 1b5644d..91cfd5b 100644 --- a/contents/creating-a-memory-leak-with-java.md +++ b/contents/creating-a-memory-leak-with-java.md @@ -24,9 +24,11 @@ 这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。 ******************************下方为个人理解************************************ + 通过一个简单的图来描述上述关系: ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj 注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象 + ******************************上方为个人理解************************************ 这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。 From 712391ed5b8ddd644c3ec6377e45151bee33f9f4 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 21 Mar 2016 15:56:13 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 个人理解部分添加一个
标签 --- contents/creating-a-memory-leak-with-java.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md index 91cfd5b..2ef3ab6 100644 --- a/contents/creating-a-memory-leak-with-java.md +++ b/contents/creating-a-memory-leak-with-java.md @@ -26,7 +26,7 @@ ******************************下方为个人理解************************************ 通过一个简单的图来描述上述关系: -ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj +ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象 ******************************上方为个人理解************************************ From 5390916cba2de0a6e42965e5b0287088dacb5c70 Mon Sep 17 00:00:00 2001 From: AutumnLight Date: Mon, 21 Mar 2016 22:24:16 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改一些不合适的语句 --- contents/creating-a-memory-leak-with-java.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/creating-a-memory-leak-with-java.md b/contents/creating-a-memory-leak-with-java.md index 2ef3ab6..267a4db 100644 --- a/contents/creating-a-memory-leak-with-java.md +++ b/contents/creating-a-memory-leak-with-java.md @@ -21,18 +21,18 @@ 上述方式可以达到内存泄漏的目的,因为 ThreadLocal 存储了一个指向类B对象的引用, 而该对象又保存了一个指向其类的引用,这个类又保存了一个指向其ClassLoader的引用, 而ClassLoader又保存了一个通过它加载的所有类的引用。 -这种方法在许多的JVM的实现中表现很糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。 +这种方法在许多的JVM的实现中表现更糟糕,因为Classes和ClassLoader被直接存储在老年代(permgen)并且永远都不会被GC处理。 ******************************下方为个人理解************************************ -通过一个简单的图来描述上述关系: +通过一个简单的图来描述上述关系:
ThreadLocal.obj ---> B.obj ---> B.class <--> ClassLoader.obj
注:上图的\*.obj表示\*类的一个实例对象,B.class表示类B的Class对象 ******************************上方为个人理解************************************ -这个模式的一个变形:为什么应用容器(例如Tomcat)可以像筛子一样泄漏内存,如果你频繁的重新部署那些可能使用ThreadLocals的应用。 -因为应用容器使用上述所说的线程,每次重新部署应用是,应用容器都会使用一个新的ClassLoader。 +这个模式的一个变形:如果频繁的重新部署那些可能使用ThreadLocals的应用,应用容器(例如Tomcat)就会像筛子一样泄漏内存。 +因为应用容器使用上述所说的线程,每次重新部署应用时,应用容器都会使用一个新的ClassLoader。 具体代码可以参考:https://gist.github.com/dpryden/b2bb29ee2d146901b4ae From 796f78e8080f3fc592e7a7783bfd6f67abb11e01 Mon Sep 17 00:00:00 2001 From: AutumnLight Date: Tue, 22 Mar 2016 16:54:47 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E6=94=B9:=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8D=E7=AC=A6=E5=90=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 37 ++++++++++--------- ...ast-two-out-of-three-booleans-are-true.md} | 0 ...ay-t.md => create-arraylist-from-array.md} | 0 ...ay-in-Java.md => declare-array-in-java.md} | 0 ...=> does-finally-always-execute-in-java.md} | 0 ...-java-support-default-parameter-values.md} | 0 ...owing-the-progress-in-a-progressdialog.md} | 0 ...esign-patterns-in-javas-core-libraries.md} | 0 ...d => how-can-i-initialize-a-static-map.md} | 0 ...t-if-an-array-contains-a-certain-value.md} | 0 ...l-one-constructor-from-another-in-java.md} | 0 ...generate-a-random-alpha-numeric-string.md} | 0 ...rivate-methods-fields-or-inner-classes.md} | 0 ...rialversionuid-and-why-should-i-use-it.md} | 0 ...t-way-to-implement-a-singleton-in-java.md} | 0 ...hat-is-reflection-and-why-is-it-useful.md} | 0 ...the-simplest-way-to-print-a-java-array.md} | 0 ...ng.md => why-cant-i-switch-on-a-string.md} | 0 ...rred-over-string-for-passwords-in-java.md} | 0 ...ed-array-faster-than-an-unsorted-array.md} | 0 20 files changed, 19 insertions(+), 18 deletions(-) rename contents/{Check-if-at-least-two-out-of-three-booleans-are-true.md => check-if-at-least-two-out-of-three-booleans-are-true.md} (100%) rename contents/{create-arraylist-arraylistt-from-array-t.md => create-arraylist-from-array.md} (100%) rename contents/{Declare-array-in-Java.md => declare-array-in-java.md} (100%) rename contents/{Does-finally-always-execute-in-Java.md => does-finally-always-execute-in-java.md} (100%) rename contents/{Does-Java-support-default-parameter-values.md => does-java-support-default-parameter-values.md} (100%) rename contents/{Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md => download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md} (100%) rename contents/{Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md => examples-of-gof-design-patterns-in-javas-core-libraries.md} (100%) rename contents/{How-can-I-Initialize-a-static-Map.md => how-can-i-initialize-a-static-map.md} (100%) rename contents/{How-can-I-test-if-an-array-contains-a-certain-value.md => how-can-i-test-if-an-array-contains-a-certain-value.md} (100%) rename contents/{How-do-i-call-one-constructor-from-another-in-java.md => how-do-i-call-one-constructor-from-another-in-java.md} (100%) rename contents/{How_to_generate_a_random_alpha-numeric_string.md => how-to-generate-a-random-alpha-numeric-string.md} (100%) rename contents/{How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md => how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md} (100%) rename contents/{What_is_a_serialVersionUID_and_why_should_I_use_it.md => what-is-a-serialversionuid-and-why-should-i-use-it.md} (100%) rename contents/{What_is_an_efficient_way_to_implement_a_singleton_in_Java.md => what-is-an-efficient-way-to-implement-a-singleton-in-java.md} (100%) rename contents/{What-is-reflection-and-why-is-it-useful.md => what-is-reflection-and-why-is-it-useful.md} (100%) rename contents/{What's-the-simplest-way-to-print-a-Java-array.md => whats-the-simplest-way-to-print-a-java-array.md} (100%) rename contents/{Why-can't-I-switch-on-a-String.md => why-cant-i-switch-on-a-string.md} (100%) rename contents/{why-is-cha[]-preferred-over-String-for-passwords-in-java.md => why-is-char-preferred-over-string-for-passwords-in-java.md} (100%) rename contents/{Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md => why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md} (100%) diff --git a/README.md b/README.md index 35486fb..053f033 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,10 @@ stackoverflow-Java-top-qa * [Java += 操作符实质](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/java-operator.md) * [将InputStream转换为String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/read-convert-an-inputstream-to-a-string.md) -* [将数组转换为List](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/create-arraylist-arraylistt-from-array-t.md) +* [将数组转换为List](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/create-arraylist-from-array.md) * [如何遍历map对象](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/iterate-through-a-hashmap.md) * [public,protected,private,不加修饰符。有什么区别呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/in-java-whats-the-difference-between-public-default-protected-and-private.md) -* [如何测试一个数组是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-test-if-an-array-contains-a-certain-value.md) +* [如何测试一个数组是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-test-if-an-array-contains-a-certain-value.md) * [重写(Override)equlas和hashCode方法时应考虑的问题](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md) * [从一个多层嵌套循环中直接跳出](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/breaking-out-of-nested-loops-in-java.md) * [如何将String转换为Int](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/converting-string-to-int-in-java.md) @@ -40,33 +40,34 @@ stackoverflow-Java-top-qa * [`Map`基于Value值排序](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-sort-a-mapkey-value-on-the-values-in-java.md) * [`HashMap和Hashtable的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/differences-between-hashmap-and-hashtable.md) * [如何便捷地将两个数组合到一起](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-concatenate-two-arrays-in-java.md) -* [Java 是否支持默认的参数值](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-Java-support-default-parameter-values.md) +* [Java 是否支持默认的参数值](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/does-java-support-default-parameter-values.md) * [Java 产生指定范围的随机数](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/generating-random-integers-in-a-range-with-Java.md) * [JavaBean 到底是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-javabean-exactly.md) * [wait()和sleep()的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md) -* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md) -* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md) +* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-do-i-call-one-constructor-from-another-in-java.md) +* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/does-finally-always-execute-in-java.md) * [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md) -* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Declare-array-in-Java.md) -* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md) -* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md) +* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/declare-array-in-java.md) +* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-reflection-and-why-is-it-useful.md) +* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-cant-i-switch-on-a-string.md) * [比较java枚举成员使用equal还是==](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md) * [用java怎么创建一个文件并向该文件写文本内容](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-create-a-file-and-write-to-a-file-in-java.md) +* [serialVersionUID 有什么作用?该如何使用?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md) > 编程技巧 * [去掉烦人的“!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md) * [获取完整的堆栈信息](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/get-current-stack-trace-in-java.md) * [如何用一行代码初始化一个ArrayList](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/initialization-of-an-arraylist-in-one-line.md) -* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-Initialize-a-static-Map.md) -* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md) -* [输出 Java 数组最简单的方式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md) +* [初始化静态map](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-initialize-a-static-map.md) +* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/check-if-at-least-two-out-of-three-booleans-are-true.md) +* [输出 Java 数组最简单的方式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/whats-the-simplest-way-to-print-a-java-array.md) * [为什么以下用随机生成的文字会得出 “hello world”?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md) -* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md) +* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-char-preferred-over-string-for-passwords-in-java.md) * [如何避免在JSP文件中使用Java代码](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-avoid-java-code-in-jsp-files.md) -* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md) -* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md) -* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md) +* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/examples-of-gof-design-patterns-in-javas-core-libraries.md) +* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-generate-a-random-alpha-numeric-string.md) +* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-an-efficient-way-to-implement-a-singleton-in-java.md) * [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md) * [我应该用哪一个@NotNull注解](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/which-notnull-java-annotation-should-i-use.md) * [怎样将堆栈追踪信息转换为字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-can-i-convert-a-stack-trace-to-a-string.md) @@ -81,15 +82,15 @@ stackoverflow-Java-top-qa * [LinkedList、ArrayList各自的使用场景,如何确认应该用哪一个呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/when-to-use-linkedlist-over-arraylist.md) * [StringBuilder和StringBuffer有哪些区别呢](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/stringbuilder-and-stringbuffer.md) -* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md) +* [为什么处理排序的数组要比非排序的快](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md) > 测试 -* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md) +* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md) > Android -* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md) +* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md) * [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md) ### 待翻译问题链接(还剩x问题) diff --git a/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md b/contents/check-if-at-least-two-out-of-three-booleans-are-true.md similarity index 100% rename from contents/Check-if-at-least-two-out-of-three-booleans-are-true.md rename to contents/check-if-at-least-two-out-of-three-booleans-are-true.md diff --git a/contents/create-arraylist-arraylistt-from-array-t.md b/contents/create-arraylist-from-array.md similarity index 100% rename from contents/create-arraylist-arraylistt-from-array-t.md rename to contents/create-arraylist-from-array.md diff --git a/contents/Declare-array-in-Java.md b/contents/declare-array-in-java.md similarity index 100% rename from contents/Declare-array-in-Java.md rename to contents/declare-array-in-java.md diff --git a/contents/Does-finally-always-execute-in-Java.md b/contents/does-finally-always-execute-in-java.md similarity index 100% rename from contents/Does-finally-always-execute-in-Java.md rename to contents/does-finally-always-execute-in-java.md diff --git a/contents/Does-Java-support-default-parameter-values.md b/contents/does-java-support-default-parameter-values.md similarity index 100% rename from contents/Does-Java-support-default-parameter-values.md rename to contents/does-java-support-default-parameter-values.md diff --git a/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md b/contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md similarity index 100% rename from contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md rename to contents/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog.md diff --git a/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md b/contents/examples-of-gof-design-patterns-in-javas-core-libraries.md similarity index 100% rename from contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md rename to contents/examples-of-gof-design-patterns-in-javas-core-libraries.md diff --git a/contents/How-can-I-Initialize-a-static-Map.md b/contents/how-can-i-initialize-a-static-map.md similarity index 100% rename from contents/How-can-I-Initialize-a-static-Map.md rename to contents/how-can-i-initialize-a-static-map.md diff --git a/contents/How-can-I-test-if-an-array-contains-a-certain-value.md b/contents/how-can-i-test-if-an-array-contains-a-certain-value.md similarity index 100% rename from contents/How-can-I-test-if-an-array-contains-a-certain-value.md rename to contents/how-can-i-test-if-an-array-contains-a-certain-value.md diff --git a/contents/How-do-i-call-one-constructor-from-another-in-java.md b/contents/how-do-i-call-one-constructor-from-another-in-java.md similarity index 100% rename from contents/How-do-i-call-one-constructor-from-another-in-java.md rename to contents/how-do-i-call-one-constructor-from-another-in-java.md diff --git a/contents/How_to_generate_a_random_alpha-numeric_string.md b/contents/how-to-generate-a-random-alpha-numeric-string.md similarity index 100% rename from contents/How_to_generate_a_random_alpha-numeric_string.md rename to contents/how-to-generate-a-random-alpha-numeric-string.md diff --git a/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md b/contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md similarity index 100% rename from contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md rename to contents/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes.md diff --git a/contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md similarity index 100% rename from contents/What_is_a_serialVersionUID_and_why_should_I_use_it.md rename to contents/what-is-a-serialversionuid-and-why-should-i-use-it.md diff --git a/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md b/contents/what-is-an-efficient-way-to-implement-a-singleton-in-java.md similarity index 100% rename from contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md rename to contents/what-is-an-efficient-way-to-implement-a-singleton-in-java.md diff --git a/contents/What-is-reflection-and-why-is-it-useful.md b/contents/what-is-reflection-and-why-is-it-useful.md similarity index 100% rename from contents/What-is-reflection-and-why-is-it-useful.md rename to contents/what-is-reflection-and-why-is-it-useful.md diff --git a/contents/What's-the-simplest-way-to-print-a-Java-array.md b/contents/whats-the-simplest-way-to-print-a-java-array.md similarity index 100% rename from contents/What's-the-simplest-way-to-print-a-Java-array.md rename to contents/whats-the-simplest-way-to-print-a-java-array.md diff --git a/contents/Why-can't-I-switch-on-a-String.md b/contents/why-cant-i-switch-on-a-string.md similarity index 100% rename from contents/Why-can't-I-switch-on-a-String.md rename to contents/why-cant-i-switch-on-a-string.md diff --git a/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md b/contents/why-is-char-preferred-over-string-for-passwords-in-java.md similarity index 100% rename from contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md rename to contents/why-is-char-preferred-over-string-for-passwords-in-java.md diff --git a/contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md b/contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md similarity index 100% rename from contents/Why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md rename to contents/why-is-processing-a-sorted-array-faster-than-an-unsorted-array.md From 8c9a864d8835cb74122776d613e36b1930cefaca Mon Sep 17 00:00:00 2001 From: AutumnLight Date: Tue, 22 Mar 2016 16:56:42 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E6=94=B9:=20=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=92=8C=E7=AD=94=E6=A1=88=E4=B8=A4=E4=B8=AA=E4=BA=8C?= =?UTF-8?q?=E7=BA=A7=E6=A0=87=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../what-is-a-serialversionuid-and-why-should-i-use-it.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md index 192d129..645bd54 100644 --- a/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md +++ b/contents/what-is-a-serialversionuid-and-why-should-i-use-it.md @@ -1,7 +1,9 @@ -# serialVersionUID 有什么作用?该如何使用? +# serialVersionUID 有什么作用?该如何使用? +##问题 当一个对象实现 Serializable 接口时,多数 ide 会提示声明一个静态常量 serialVersionUID(版本标识),那 serialVersionUID 到底有什么作用呢?应该如何使用 serialVersionUID ? +##回答 serialVersionUID 是实现 Serializable 接口而来的,而 Serializable 则是应用于Java 对象序列化/反序列化。对象的序列化主要有两种用途: - 把对象序列化成字节码,保存到指定介质上(如磁盘等) From db6ba26407c1c737bdf37af799b367585e7dc414 Mon Sep 17 00:00:00 2001 From: Zang Fengshun Date: Thu, 31 Mar 2016 12:39:36 -0400 Subject: [PATCH 11/12] Create how-to-create-a-java-string-from-the-contents-of-a-file --- ...-a-java-string-from-the-contents-of-a-file | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 contents/how-to-create-a-java-string-from-the-contents-of-a-file diff --git a/contents/how-to-create-a-java-string-from-the-contents-of-a-file b/contents/how-to-create-a-java-string-from-the-contents-of-a-file new file mode 100644 index 0000000..5df9248 --- /dev/null +++ b/contents/how-to-create-a-java-string-from-the-contents-of-a-file @@ -0,0 +1,25 @@ +怎样将文件的内容生成java string +问题: +我现在使用如下的语句有一段时间了。它看起来是最广泛使用的,最起码在我浏览过的网站来说。 +有谁有一个更好/不同的方法来将文件中的内容转换为java中的string吗? + +private String readFile( String file ) throws IOException { + BufferedReader reader = new BufferedReader( new FileReader (file)); + String line = null; + StringBuilder stringBuilder = new StringBuilder(); + String ls = System.getProperty("line.separator"); + + try { + while( ( line = reader.readLine() ) != null ) { + stringBuilder.append( line ); + stringBuilder.append( ls ); + } + + return stringBuilder.toString(); + } finally { + reader.close(); + } +} + +回答: +StackOverFlow 链接:http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file From e3511d0c4e6d3606a7fdd7eba284ba39b7dda069 Mon Sep 17 00:00:00 2001 From: fengshunZang Date: Mon, 4 Apr 2016 17:54:11 -0400 Subject: [PATCH 12/12] new translation file It's a new translation work titled as' how to create a java string from the content of a file' --- ...-a-java-string-from-the-contents-of-a-file | 25 -------- ...java-string-from-the-contents-of-a-file.md | 59 +++++++++++++++++++ 2 files changed, 59 insertions(+), 25 deletions(-) delete mode 100644 contents/how-to-create-a-java-string-from-the-contents-of-a-file create mode 100644 contents/how-to-create-a-java-string-from-the-contents-of-a-file.md diff --git a/contents/how-to-create-a-java-string-from-the-contents-of-a-file b/contents/how-to-create-a-java-string-from-the-contents-of-a-file deleted file mode 100644 index 5df9248..0000000 --- a/contents/how-to-create-a-java-string-from-the-contents-of-a-file +++ /dev/null @@ -1,25 +0,0 @@ -怎样将文件的内容生成java string -问题: -我现在使用如下的语句有一段时间了。它看起来是最广泛使用的,最起码在我浏览过的网站来说。 -有谁有一个更好/不同的方法来将文件中的内容转换为java中的string吗? - -private String readFile( String file ) throws IOException { - BufferedReader reader = new BufferedReader( new FileReader (file)); - String line = null; - StringBuilder stringBuilder = new StringBuilder(); - String ls = System.getProperty("line.separator"); - - try { - while( ( line = reader.readLine() ) != null ) { - stringBuilder.append( line ); - stringBuilder.append( ls ); - } - - return stringBuilder.toString(); - } finally { - reader.close(); - } -} - -回答: -StackOverFlow 链接:http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file diff --git a/contents/how-to-create-a-java-string-from-the-contents-of-a-file.md b/contents/how-to-create-a-java-string-from-the-contents-of-a-file.md new file mode 100644 index 0000000..afaa3d4 --- /dev/null +++ b/contents/how-to-create-a-java-string-from-the-contents-of-a-file.md @@ -0,0 +1,59 @@ +## 怎样将文件的内容生成java string +### 问题: +我现在使用如下的语句有一段时间了。它看起来是最广泛使用的,最起码在我浏览过的网站来说。 +有谁有一个更好/不同的方法来将文件中的内容转换为java中的string吗? + +````java +private String readFile(String file) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line = null; + StringBuilder stringBuilder = new StringBuilder(); + String ls = System.getProperty("line.separator"); + + try { + while((line = reader.readLine())!= null) { + stringBuilder.append(line); + stringBuilder.append(ls); + } + + return stringBuilder.toString(); + } finally { + reader.close(); + } +} + +```` + +### 最佳答案: +#### 从文件中读取全部内容 +下面是java7中利用实用的Method实现的一种紧凑、健壮的写法。 +````java +static String readFile(String path, Charset encoding) + throws IOException +{ + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded, encoding); +} +```` +#### 从文件中读取文件行 +java7增加了一种更方便的从文件中[`读取文件行的方法`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29),用 List表示。 +这种方式是“有损耗的”,因为行分隔符在每行最后被剥夺了。 +````java +List lines = Files.readAllLines(Paths.get(path), encoding); +```` +#### 内存分析 +第一种保留行分隔符的方法能在短时间内需求文件规模几倍的内存,因为短时间内源文件内容(字节数组)和编码的字符(每个都是16位的即使在文件中按8位编译)都曾经在内存中存留。处理那些相对于有限的内存很小的文件是使用这种方法最安全的方式。 +第二种读取文件行的方法通常来说更节省内存,因为输入字节的缓冲不必包含整个文件。但是,对于相比内存很大的文件来说,这种方法也同样不合适。 +对于读取大型文件来说,你需要设计一个不同的系统。这个系统从流中读取一大块文本,处理,然后移动到下一块,重新使用同样的大小固定的内存模块。这里,“大型”取决于电脑的规格。现在,定义“大型”的门槛可能从许多G的RAM开始。 +#### 字符编码 +原来的解答中遗漏了关于字符编码的说明。只有一些特殊情况下,平台的默认设定是你需要的,但是这种情况很少,你应该能够根据自己的需求调整。 +StanderdCharsets类定义了一些编码中需要的常量。 +````java +String content = readFile("test.txt", StandardCharsets.UTF_8); +```` +平台的默认设定中也可从charset类中得到。 +````java +String content = readFile("test.txt", Charset.defaultCharset()); +```` + +StackOverFlow 链接:http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file