Skip to content

Commit

Permalink
优化“用java怎么创建一个文件并向该文件写文本内容”
Browse files Browse the repository at this point in the history
  • Loading branch information
giantray committed Mar 2, 2016
1 parent d1deb8e commit 532ff0c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ stackoverflow-Java-top-qa
* [反射是什么及其用途](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枚举成员使用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)

> 编程技巧
Expand Down Expand Up @@ -137,7 +138,6 @@ stackoverflow-Java-top-qa
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
- [What is the difference between a soft reference and a weak reference in Java?](http://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java)
- [How to create a file and write to a file in Java?](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java)
- [What is the equivalent of the C++ Pair<L,R> in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java)
- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
- [How do I “decompile” Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)
Expand Down
5 changes: 3 additions & 2 deletions contents/how-to-create-a-file-and-write-to-a-file-in-java.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 用java怎么样创建一个文件并向该文件写文本内容
## 用java怎么创建一个文件并向该文件写文本内容

### 问:在java里最简单的创建文件写文件的方法是什么

Expand All @@ -10,7 +10,7 @@ writer.println("The first line");
writer.println("The second line");
writer.close();
````
创建一个位二进制文件(同样会覆盖这文件)
创建一个二进制文件(同样会覆盖这文件)
````java
byte data[] = ...
FileOutputStream out = new FileOutputStream("the-file-name");
Expand Down Expand Up @@ -45,6 +45,7 @@ try (Writer writer = new BufferedWriter(new OutputStreamWriter(
还有一些实用的方法如下:
* [`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`,但是它使用的是默认编码,这不是很好的方法,最好是明确指定编码
Expand Down

0 comments on commit 532ff0c

Please sign in to comment.