From 16319144ef8982f5f610b559f021c6620e27ce99 Mon Sep 17 00:00:00 2001 From: "baolongf@yahoo.com" Date: Fri, 26 Feb 2016 22:41:47 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9C=E5=9C=A8java?= =?UTF-8?q?=E4=B8=AD=E5=A6=82=E4=BD=95=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=92=8C=E5=86=99=E6=96=87=E4=BB=B6=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...eate-a-file-and-write-to-a-file-in-java.md | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 contents/how-to-create-a-file-and-write-to-a-file-in-java.md diff --git a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md new file mode 100644 index 0000000..daf03ce --- /dev/null +++ b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md @@ -0,0 +1,149 @@ +## 用java怎么样创建一个文件并向该文件写文本内容 + +### 问:在java里最简单的创建文件写文件的方法是什么 + +### 最佳答案: +创建一个文本文件(注意:如果该文件存在,则会覆盖该文件) +````java +PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8"); +writer.println("The first line"); +writer.println("The second line"); +writer.close(); +```` +创建一个位二进制文件(同样会覆盖这文件) +````java +byte data[] = ... +FileOutputStream out = new FileOutputStream("the-file-name"); +out.write(data); +out.close(); +```` + +Java 7+ 用户可以用[`File`](http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html)类来写文件 +创建一个文本文件 +````java +List lines = Arrays.asList("The first line", "The second line"); +Path file = Paths.get("the-file-name.txt"); +Files.write(file, lines, Charset.forName("UTF-8")); +//Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND); +```` +创建一个二进制文件 +````java +byte data[] = ... +Path file = Paths.get("the-file-name"); +Files.write(file, data); +//Files.write(file, data, StandardOpenOption.APPEND); +```` + +### 其他的答案(1): +在Java 7+中 +````java +try (Writer writer = new BufferedWriter(new OutputStreamWriter( + new FileOutputStream("filename.txt"), "utf-8"))) { + writer.write("something"); +} +```` +还有一些实用的方法如下: +* [`FileUtils.writeStringtoFile(..)`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile%28java.io.File,%20java.lang.String,%20java.nio.charset.Charset%29) 来自于 commons-io 包 +* [`Files.write(..)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#write%28java.lang.CharSequence,%20java.io.File,%20java.nio.charset.Charset%29) 来自于 guava +Note also that you can use a FileWriter, but it uses the default encoding, +which is often a bad idea - it's best to specify the encoding explicitly. +还要注意可以使用 `FileWriter`,但是它使用的是默认编码,这不是很好的方法,最好是明确指定编码 + + +下面是来自于prior-to-java-7的原始方法 +````java +Writer writer = null; + +try { + writer = new BufferedWriter(new OutputStreamWriter( + new FileOutputStream("filename.txt"), "utf-8")); + writer.write("Something"); +} catch (IOException ex) { + // report +} finally { + try {writer.close();} catch (Exception ex) {/*ignore*/} +} +```` +可以看[`Reading, Writing, and Creating Files`](http://docs.oracle.com/javase/tutorial/essential/io/file.html)(包含NIO2) + +### 其他答案(2): +````java +public class Program { + public static void main(String[] args) { + String text = "Hello world"; + BufferedWriter output = null; + try { + File file = new File("example.txt"); + output = new BufferedWriter(new FileWriter(file)); + output.write(text); + } catch ( IOException e ) { + e.printStackTrace(); + } finally { + if ( output != null ) output.close(); + } + } +} +```` + +### 其他答案(3): +如果已经有想要写到文件中的内容,[`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) 作为 Java 7 附加部分的native I/O,提供了简单高效的方法来实现你的目标 + +基本上创建文件,写文件只需要一行,而且是只需一个方法调用! +下面的例子创建并且写了6个不同的文件来展示是怎么使用的 + +````java +Charset utf8 = StandardCharsets.UTF_8; +List lines = Arrays.asList("1st line", "2nd line"); +byte[] data = {1, 2, 3, 4, 5}; + +try { + Files.write(Paths.get("file1.bin"), data); + Files.write(Paths.get("file2.bin"), data, + StandardOpenOption.CREATE, StandardOpenOption.APPEND); + Files.write(Paths.get("file3.txt"), "content".getBytes()); + Files.write(Paths.get("file4.txt"), "content".getBytes(utf8)); + Files.write(Paths.get("file5.txt"), lines, utf8); + Files.write(Paths.get("file6.txt"), lines, utf8, + StandardOpenOption.CREATE, StandardOpenOption.APPEND); +} catch (IOException e) { + e.printStackTrace(); +} +```` + +### 其他答案(4): +下面是一个小程序来创建和写文件。该版本的代码比较长,但是可以容易理解 +````java +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; + +public class writer { + public void writing() { + try { + //Whatever the file path is. + File statText = new File("E:/Java/Reference/bin/images/statsTest.txt"); + FileOutputStream is = new FileOutputStream(statText); + OutputStreamWriter osw = new OutputStreamWriter(is); + Writer w = new BufferedWriter(osw); + w.write("POTATO!!!"); + w.close(); + } catch (IOException e) { + System.err.println("Problem writing to the file statsTest.txt"); + } + } + + public static void main(String[]args) { + writer write = new writer(); + write.writing(); + } +} +```` + + + + +stackoverflow链接: +http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java From 62775a8ca5f2047fdfdf49320475c20624392060 Mon Sep 17 00:00:00 2001 From: "baolongf@yahoo.com" Date: Sun, 28 Feb 2016 14:13:15 +0800 Subject: [PATCH 2/3] modify get-current-stack-trace-in-java --- contents/get-current-stack-trace-in-java.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contents/get-current-stack-trace-in-java.md b/contents/get-current-stack-trace-in-java.md index 1e2fa83..9b17cc6 100644 --- a/contents/get-current-stack-trace-in-java.md +++ b/contents/get-current-stack-trace-in-java.md @@ -8,5 +8,9 @@ String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e) ``` +````java +Thread.currentThread().getStackTrace(); +```` + stackoverflow鍘熷潃锛 http://stackoverflow.com/questions/1069066/how-can-i-get-the-current-stack-trace \ No newline at end of file From cdee8a74046ae20aecb9016797515d073717a5b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B0=E5=AE=9D=E9=BE=99=20fengbaolong=20=2888342=29?= Date: Mon, 29 Feb 2016 08:57:25 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E6=94=B9=E4=B8=BAUTF-8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...eate-a-file-and-write-to-a-file-in-java.md | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md index daf03ce..b94e173 100644 --- a/contents/how-to-create-a-file-and-write-to-a-file-in-java.md +++ b/contents/how-to-create-a-file-and-write-to-a-file-in-java.md @@ -1,16 +1,16 @@ -## 用java怎么样创建一个文件并向该文件写文本内容 +## 鐢╦ava鎬庝箞鏍峰垱寤轰竴涓枃浠跺苟鍚戣鏂囦欢鍐欐枃鏈唴瀹 -### 问:在java里最简单的创建文件写文件的方法是什么 +### 闂細鍦╦ava閲屾渶绠鍗曠殑鍒涘缓鏂囦欢鍐欐枃浠剁殑鏂规硶鏄粈涔 -### 最佳答案: -创建一个文本文件(注意:如果该文件存在,则会覆盖该文件) +### 鏈浣崇瓟妗: +鍒涘缓涓涓枃鏈枃浠讹紙娉ㄦ剰锛氬鏋滆鏂囦欢瀛樺湪锛屽垯浼氳鐩栬鏂囦欢锛 ````java PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8"); writer.println("The first line"); writer.println("The second line"); writer.close(); ```` -创建一个位二进制文件(同样会覆盖这文件) +鍒涘缓涓涓綅浜岃繘鍒舵枃浠讹紙鍚屾牱浼氳鐩栬繖鏂囦欢锛 ````java byte data[] = ... FileOutputStream out = new FileOutputStream("the-file-name"); @@ -18,15 +18,15 @@ out.write(data); out.close(); ```` -Java 7+ 用户可以用[`File`](http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html)类来写文件 -创建一个文本文件 +Java 7+ 鐢ㄦ埛鍙互鐢╗`File`](http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html)绫绘潵鍐欐枃浠 +鍒涘缓涓涓枃鏈枃浠 ````java List lines = Arrays.asList("The first line", "The second line"); Path file = Paths.get("the-file-name.txt"); Files.write(file, lines, Charset.forName("UTF-8")); //Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND); ```` -创建一个二进制文件 +鍒涘缓涓涓簩杩涘埗鏂囦欢 ````java byte data[] = ... Path file = Paths.get("the-file-name"); @@ -34,23 +34,23 @@ Files.write(file, data); //Files.write(file, data, StandardOpenOption.APPEND); ```` -### 其他的答案(1): -在Java 7+中 +### 鍏朵粬鐨勭瓟妗堬紙1锛: +鍦↗ava 7+涓 ````java try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("filename.txt"), "utf-8"))) { writer.write("something"); } ```` -还有一些实用的方法如下: -* [`FileUtils.writeStringtoFile(..)`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile%28java.io.File,%20java.lang.String,%20java.nio.charset.Charset%29) 来自于 commons-io 包 -* [`Files.write(..)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#write%28java.lang.CharSequence,%20java.io.File,%20java.nio.charset.Charset%29) 来自于 guava +杩樻湁涓浜涘疄鐢ㄧ殑鏂规硶濡備笅锛 +* [`FileUtils.writeStringtoFile(..)`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile%28java.io.File,%20java.lang.String,%20java.nio.charset.Charset%29) 鏉ヨ嚜浜 commons-io 鍖 +* [`Files.write(..)`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#write%28java.lang.CharSequence,%20java.io.File,%20java.nio.charset.Charset%29) 鏉ヨ嚜浜 guava Note also that you can use a FileWriter, but it uses the default encoding, which is often a bad idea - it's best to specify the encoding explicitly. -还要注意可以使用 `FileWriter`,但是它使用的是默认编码,这不是很好的方法,最好是明确指定编码 +杩樿娉ㄦ剰鍙互浣跨敤 `FileWriter`锛屼絾鏄畠浣跨敤鐨勬槸榛樿缂栫爜锛岃繖涓嶆槸寰堝ソ鐨勬柟娉曪紝鏈濂芥槸鏄庣‘鎸囧畾缂栫爜 -下面是来自于prior-to-java-7的原始方法 +涓嬮潰鏄潵鑷簬prior-to-java-7鐨勫師濮嬫柟娉 ````java Writer writer = null; @@ -64,9 +64,9 @@ try { try {writer.close();} catch (Exception ex) {/*ignore*/} } ```` -可以看[`Reading, Writing, and Creating Files`](http://docs.oracle.com/javase/tutorial/essential/io/file.html)(包含NIO2) +鍙互鐪媅`Reading, Writing, and Creating Files`](http://docs.oracle.com/javase/tutorial/essential/io/file.html)(鍖呭惈NIO2) -### 其他答案(2): +### 鍏朵粬绛旀锛2锛夛細 ````java public class Program { public static void main(String[] args) { @@ -85,11 +85,11 @@ public class Program { } ```` -### 其他答案(3): -如果已经有想要写到文件中的内容,[`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) 作为 Java 7 附加部分的native I/O,提供了简单高效的方法来实现你的目标 +### 鍏朵粬绛旀锛3锛夛細 +濡傛灉宸茬粡鏈夋兂瑕佸啓鍒版枃浠朵腑鐨勫唴瀹癸紝[`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) 浣滀负 Java 7 闄勫姞閮ㄥ垎鐨刵ative I/O锛屾彁渚涗簡绠鍗曢珮鏁堢殑鏂规硶鏉ュ疄鐜颁綘鐨勭洰鏍 -基本上创建文件,写文件只需要一行,而且是只需一个方法调用! -下面的例子创建并且写了6个不同的文件来展示是怎么使用的 +鍩烘湰涓婂垱寤烘枃浠讹紝鍐欐枃浠跺彧闇瑕佷竴琛岋紝鑰屼笖鏄彧闇涓涓柟娉曡皟鐢紒 +涓嬮潰鐨勪緥瀛愬垱寤哄苟涓斿啓浜6涓笉鍚岀殑鏂囦欢鏉ュ睍绀烘槸鎬庝箞浣跨敤鐨 ````java Charset utf8 = StandardCharsets.UTF_8; @@ -110,8 +110,8 @@ try { } ```` -### 其他答案(4): -下面是一个小程序来创建和写文件。该版本的代码比较长,但是可以容易理解 +### 鍏朵粬绛旀锛4锛夛細 +涓嬮潰鏄竴涓皬绋嬪簭鏉ュ垱寤哄拰鍐欐枃浠躲傝鐗堟湰鐨勪唬鐮佹瘮杈冮暱锛屼絾鏄彲浠ュ鏄撶悊瑙 ````java import java.io.BufferedWriter; import java.io.File; @@ -145,5 +145,5 @@ public class writer { -stackoverflow链接: +stackoverflow閾炬帴锛 http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java