-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTxtFileLineWrite.java
More file actions
74 lines (66 loc) · 1.88 KB
/
TxtFileLineWrite.java
File metadata and controls
74 lines (66 loc) · 1.88 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* @author Yuan Yifan
* Function: Read large text file line by line.
* Usage:
* Firstly, new a object TxtFileLineWrite and initial it by your filename:
* TxtFileLineWrite FWL = new TxtFileLineWrite(FileName);
* Secondly, the function in this object fWrite to write a String.
* For example:
* FWL.fWrite("This is a test!\r\n");
* You have to use "\r\n" in Win to shift a new line.
* Last but not least:
* You have to free file before terminating your program like this:
* FWL.FreeFile();
* Have fun!
*
*/
public class TxtFileLineWrite {
private FileWriter fp = null;
private BufferedWriter bw = null;
private int Flag_Loaded = 0;
public TxtFileLineWrite(){
//Do nothing
}
public TxtFileLineWrite(String FileName){
LoadFile(FileName);
}
public void LoadFile(String FileName){
try{
if (Flag_Loaded==1){
FreeFile();
}else{
fp = new FileWriter(FileName);
bw = new BufferedWriter(fp);
Flag_Loaded = 1;
}
}catch(IOException e) {
System.out.println("Error While Opening the File to Write.");
}
}
public void fWrite(String StrWrite){
try{
bw.write(StrWrite,0,StrWrite.length());
}catch(IOException e) {
System.out.println("Error While Writing the File.");
}
}
public void FreeFile(){
try {
if (Flag_Loaded==1){
bw.close();
fp.close();
Flag_Loaded = 0;
}
} catch (IOException ex) {
System.out.println("Error While Closing the File.");
}
}
@Override
protected void finalize() throws Throwable{
FreeFile();
}
}