-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriter.java
52 lines (48 loc) · 1.35 KB
/
Writer.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
import java.io.*;
/**
* The Write class, to write to files.
*
* @author Matthew Hillier
* @version 1.0
*/
public class Writer
{
/**
* Overwrites an old file with a new file.
* @param newFile The new file to write with.
* @param oldFile The old file to overwrite.
*/
public static void overwriteFile(String newFile, String oldFile)
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(newFile + ".txt"));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
FileWriter writer = new FileWriter(oldFile + ".txt");
try
{
while((line = reader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append(ls);
}
writer.write(stringBuilder.toString());
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
reader.close();
writer.close();
}
}
catch(Exception error)
{
System.out.println(error.getMessage());
}
}
}