Skip to content

Commit 532ff0c

Browse files
committed
优化“用java怎么创建一个文件并向该文件写文本内容”
1 parent d1deb8e commit 532ff0c

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ stackoverflow-Java-top-qa
5151
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md)
5252
* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
5353
* [比较java枚举成员使用equal还是==](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/comparing-java-enum-members-or-equals.md)
54+
* [用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)
5455

5556
> 编程技巧
5657
@@ -137,7 +138,6 @@ stackoverflow-Java-top-qa
137138
- [Why does Math.round(0.49999999999999994) return 1](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1)
138139
- [Eclipse: Set maximum line length for auto formatting?](http://stackoverflow.com/questions/3697287/eclipse-set-maximum-line-length-for-auto-formatting)
139140
- [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)
140-
- [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)
141141
- [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)
142142
- [What is the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp)
143143
- [How do I “decompile” Java class files?](http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files)

contents/how-to-create-a-file-and-write-to-a-file-in-java.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 用java怎么样创建一个文件并向该文件写文本内容
1+
## 用java怎么创建一个文件并向该文件写文本内容
22

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

@@ -10,7 +10,7 @@ writer.println("The first line");
1010
writer.println("The second line");
1111
writer.close();
1212
````
13-
创建一个位二进制文件(同样会覆盖这文件)
13+
创建一个二进制文件(同样会覆盖这文件)
1414
````java
1515
byte data[] = ...
1616
FileOutputStream out = new FileOutputStream("the-file-name");
@@ -45,6 +45,7 @@ try (Writer writer = new BufferedWriter(new OutputStreamWriter(
4545
还有一些实用的方法如下:
4646
* [`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 包
4747
* [`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
48+
4849
Note also that you can use a FileWriter, but it uses the default encoding,
4950
which is often a bad idea - it's best to specify the encoding explicitly.
5051
还要注意可以使用 `FileWriter`,但是它使用的是默认编码,这不是很好的方法,最好是明确指定编码

0 commit comments

Comments
 (0)