-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputFileFormat.java
74 lines (67 loc) · 1.97 KB
/
InputFileFormat.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
import java.io.*;
import java.util.*;
/**
* Represents communication from Mandor to the outside world.
* This class is immutable.
*/
public abstract class InputFileFormat implements FileFormat, Immutable
{
/** The string that will be written to a file */
public final String stringRepresentation;
public InputFileFormat(String stringRepresentation)
{
this.stringRepresentation = stringRepresentation;
}
/**
* Writes the file to disk.
*/
public void write(String filename)
{
writeStringToDisk(stringRepresentation, filename);
}
/**
* Convenience method that writes a string to a file.
*/
public static void writeStringToDisk(String string, String filename)
{
try (PrintWriter outputFile = new PrintWriter(filename))
{
outputFile.print(string);
}
catch (IOException e)
{
// abort program if there's a problem
System.out.println("Error writing to " + filename + "!");
e.printStackTrace();
}
}
/**
* Convenience method that appends a string to a file.
*/
public static void appendStringToDisk(String string, String filename)
{
try
{
File file = new File(filename);
if ( ! file.exists() )
file.createNewFile();
FileWriter fileWriter = new FileWriter(filename,true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(string);
bufferedWriter.close();
}
catch (IOException e)
{
System.out.println("Error appending to " + filename + "!");
e.printStackTrace();
}
}
public int hashCode()
{
return Objects.hash(stringRepresentation);
}
public String toString()
{
return stringRepresentation;
}
}