-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkbFiles.java
More file actions
54 lines (47 loc) · 1.63 KB
/
kbFiles.java
File metadata and controls
54 lines (47 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package kbJava;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class kbFiles {
public void writeBufferedFile() {
String filePath = "D:\\TestBufferedFile.txt";
//String filePath = "/TestBufferedFile.txt";
File file = new File(filePath);
Writer fileWriter = null;
BufferedWriter bufferedWriter = null;
List<String> contentList = new ArrayList<String>();
for (int i = 0; i <= 1000; i++) {
contentList.add(Integer.toString(i));
}
String[] content = new String[contentList.size()];
contentList.toArray(content);
//String[] content = {"aa", "bb"};
try {
//Use this for append mode
//if(!file.exists()){file.createNewFile();}
//fileWriter = new FileWriter(file,true);
fileWriter = new FileWriter(file);
bufferedWriter = new BufferedWriter(fileWriter);
for (String line : content) { //actually we can use arraylist here; directly replace content with contenList
line += System.getProperty("line.separator");
bufferedWriter.write(line);
}
} catch (IOException e) {
System.err.println("Error writing the file : ");
e.printStackTrace();
} finally {
if (bufferedWriter != null && fileWriter != null) {
try {
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}