Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d1deb8e

Browse filesBrowse files
committed
Merge pull request giantray#27 from andyfeng101/master
新增“在java中如何创建文件和写文件”
2 parents 5e1558d + cdee8a7 commit d1deb8e
Copy full SHA for d1deb8e

File tree

Expand file treeCollapse file tree

2 files changed

+153
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+153
-0
lines changed

‎contents/get-current-stack-trace-in-java.md

Copy file name to clipboardExpand all lines: contents/get-current-stack-trace-in-java.md
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e)
99
```
1010

11+
````java
12+
Thread.currentThread().getStackTrace();
13+
````
14+
1115
stackoverflow原址:
1216
http://stackoverflow.com/questions/1069066/how-can-i-get-the-current-stack-trace
+149Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
## 用java怎么样创建一个文件并向该文件写文本内容
2+
3+
### 问:在java里最简单的创建文件写文件的方法是什么
4+
5+
### 最佳答案:
6+
创建一个文本文件(注意:如果该文件存在,则会覆盖该文件)
7+
````java
8+
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
9+
writer.println("The first line");
10+
writer.println("The second line");
11+
writer.close();
12+
````
13+
创建一个位二进制文件(同样会覆盖这文件)
14+
````java
15+
byte data[] = ...
16+
FileOutputStream out = new FileOutputStream("the-file-name");
17+
out.write(data);
18+
out.close();
19+
````
20+
21+
Java 7+ 用户可以用[`File`](http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/file/Files.html)类来写文件
22+
创建一个文本文件
23+
````java
24+
List<String> lines = Arrays.asList("The first line", "The second line");
25+
Path file = Paths.get("the-file-name.txt");
26+
Files.write(file, lines, Charset.forName("UTF-8"));
27+
//Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
28+
````
29+
创建一个二进制文件
30+
````java
31+
byte data[] = ...
32+
Path file = Paths.get("the-file-name");
33+
Files.write(file, data);
34+
//Files.write(file, data, StandardOpenOption.APPEND);
35+
````
36+
37+
### 其他的答案(1):
38+
在Java 7+中
39+
````java
40+
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
41+
new FileOutputStream("filename.txt"), "utf-8"))) {
42+
writer.write("something");
43+
}
44+
````
45+
还有一些实用的方法如下:
46+
* [`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 包
47+
* [`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+
Note also that you can use a FileWriter, but it uses the default encoding,
49+
which is often a bad idea - it's best to specify the encoding explicitly.
50+
还要注意可以使用 `FileWriter`,但是它使用的是默认编码,这不是很好的方法,最好是明确指定编码
51+
52+
53+
下面是来自于prior-to-java-7的原始方法
54+
````java
55+
Writer writer = null;
56+
57+
try {
58+
writer = new BufferedWriter(new OutputStreamWriter(
59+
new FileOutputStream("filename.txt"), "utf-8"));
60+
writer.write("Something");
61+
} catch (IOException ex) {
62+
// report
63+
} finally {
64+
try {writer.close();} catch (Exception ex) {/*ignore*/}
65+
}
66+
````
67+
可以看[`Reading, Writing, and Creating Files`](http://docs.oracle.com/javase/tutorial/essential/io/file.html)(包含NIO2)
68+
69+
### 其他答案(2):
70+
````java
71+
public class Program {
72+
public static void main(String[] args) {
73+
String text = "Hello world";
74+
BufferedWriter output = null;
75+
try {
76+
File file = new File("example.txt");
77+
output = new BufferedWriter(new FileWriter(file));
78+
output.write(text);
79+
} catch ( IOException e ) {
80+
e.printStackTrace();
81+
} finally {
82+
if ( output != null ) output.close();
83+
}
84+
}
85+
}
86+
````
87+
88+
### 其他答案(3):
89+
如果已经有想要写到文件中的内容,[`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) 作为 Java 7 附加部分的native I/O,提供了简单高效的方法来实现你的目标
90+
91+
基本上创建文件,写文件只需要一行,而且是只需一个方法调用!
92+
下面的例子创建并且写了6个不同的文件来展示是怎么使用的
93+
94+
````java
95+
Charset utf8 = StandardCharsets.UTF_8;
96+
List<String> lines = Arrays.asList("1st line", "2nd line");
97+
byte[] data = {1, 2, 3, 4, 5};
98+
99+
try {
100+
Files.write(Paths.get("file1.bin"), data);
101+
Files.write(Paths.get("file2.bin"), data,
102+
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
103+
Files.write(Paths.get("file3.txt"), "content".getBytes());
104+
Files.write(Paths.get("file4.txt"), "content".getBytes(utf8));
105+
Files.write(Paths.get("file5.txt"), lines, utf8);
106+
Files.write(Paths.get("file6.txt"), lines, utf8,
107+
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
108+
} catch (IOException e) {
109+
e.printStackTrace();
110+
}
111+
````
112+
113+
### 其他答案(4):
114+
下面是一个小程序来创建和写文件。该版本的代码比较长,但是可以容易理解
115+
````java
116+
import java.io.BufferedWriter;
117+
import java.io.File;
118+
import java.io.FileOutputStream;
119+
import java.io.IOException;
120+
import java.io.OutputStreamWriter;
121+
import java.io.Writer;
122+
123+
public class writer {
124+
public void writing() {
125+
try {
126+
//Whatever the file path is.
127+
File statText = new File("E:/Java/Reference/bin/images/statsTest.txt");
128+
FileOutputStream is = new FileOutputStream(statText);
129+
OutputStreamWriter osw = new OutputStreamWriter(is);
130+
Writer w = new BufferedWriter(osw);
131+
w.write("POTATO!!!");
132+
w.close();
133+
} catch (IOException e) {
134+
System.err.println("Problem writing to the file statsTest.txt");
135+
}
136+
}
137+
138+
public static void main(String[]args) {
139+
writer write = new writer();
140+
write.writing();
141+
}
142+
}
143+
````
144+
145+
146+
147+
148+
stackoverflow链接:
149+
http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.