-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteFile.java
82 lines (75 loc) · 2 KB
/
WriteFile.java
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
75
76
77
78
79
80
81
82
package bbtrial.nl.logicgate.ace;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* A simple file writer.
* Allows writing of a new file/overwriting a file,
* appending a file, and copying a file with a new name.
* @author smedlock
*
*/
public class WriteFile {
public WriteFile(){
}
/**
* Writes a new file or overwrites the file with the
* specified file name. Text of the file is provided
* as a single string.
* @param filename
* @param fileText
*/
public void overWriteFile(File file, String fileText){
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(fileText);
out.close();
} catch (IOException e) {
}
}
/**
* Appends the existing file of the specified filename
* with the specified text, provided as a single string.
* If file does not exist, creates a new file with the
* specified filename.
* @param filename
* @param fileText
*/
public void appendFile(File file, String fileText){
try{
BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
out.write(fileText);
out.close();
} catch (IOException e){
}
}
/**
* Copies a file under a new name
* @param in : the file to be copied
* @param out : the new file path/name
* @throws IOException
*/
public void copyFile(File in, File out)
throws IOException
{
FileChannel inChannel = new
FileInputStream(in).getChannel();
FileChannel outChannel = new
FileOutputStream(out).getChannel();
try {
inChannel.transferTo(0, inChannel.size(),
outChannel);
}
catch (IOException e) {
throw e;
}
finally {
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
}
}
}