-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileOperation.java
100 lines (86 loc) · 2.64 KB
/
FileOperation.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.*;
import java.io.*;
/**
* File Operation Class
* For read and write to file txt
*
* @author Novi Kesumaningtyas
* @version 1 - 25 May 2017
*/
public class FileOperation
{
/**
* Constructor for objects of class FileOperation
*/
public FileOperation()
{
}
/**
* readText(String filename)
* Read movies details from the text given
* and keep it in string
*
* @param filename - file name which this method should read
*
* @return String - contain all words from the file
*/
public String readText(String filename)
{
StringBuilder texts = new StringBuilder();
try
{
FileReader inputFile = new FileReader(filename);
Scanner parser = new Scanner(inputFile);
while (parser.hasNextLine())
{
String line = parser.nextLine();
texts.append(line + ";");
}
inputFile.close();
return texts.toString();
}
catch(FileNotFoundException exception)
{
String error = filename + " not found";
System.out.println(error);
return error;
}
catch(IOException exception)
{
String error = "Unexpected I/O error occured";
System.out.println(error);
return error;
}
}
/**
* writeToText(String filename, String moviesDetails, String lastLine)
* Write movies details back to the text file
*
* @param String filename - where the files should be written
* @param String moviesDetails - content should be written to the file
* @param String lastLine - the last line from movies detail should be written to the file
*/
public void writeToText(String filename, String moviesDetails, String lastLine)
{
String[] texts = moviesDetails.split(";");
try
{
PrintWriter output = new PrintWriter(new FileWriter(filename));
for(int line = 0; line < texts.length ; line++)
{
output.println(texts[line]);
}
output.print(lastLine);
output.close();
System.out.println("\t\tSuccessfully write movies list to the file");
}
catch(FileNotFoundException exception)
{
System.out.println(filename + " not found");
}
catch(IOException exception)
{
System.out.println("Unexpected I/O error occured");
}
}
}