-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelloWorldModel.java
More file actions
71 lines (66 loc) · 2.37 KB
/
Copy pathHelloWorldModel.java
File metadata and controls
71 lines (66 loc) · 2.37 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
*
* @author AfzaalAhmad
*/
public class HelloWorldModel {
public String getData() throws FileNotFoundException, IOException {
if(!(new File("c:\\file.txt").isFile())) {
// Create -- Make sure file exists -- the file before continuing
Files.createFile(Paths.get("F:\\file.txt"));
}
String data;
// We will be using a try-with-resource block
try (BufferedReader reader = new BufferedReader(
new FileReader("c:\\file.txt"))) {
// Access the data from the file
// Create a new StringBuilder
StringBuilder string = new StringBuilder();
// Read line-by-line
String line = reader.readLine();
string.append("<html>");
// While there comes a new line, execute this
while(line != null) {
// Add these lines to the String builder
string.append(line);
string.append("<br />");
// Read the next line
line = reader.readLine();
}
string.append("</html>");
data = string.toString();
} catch (Exception er) {
// Since there was an error, you probably want to notify the user
// For that error. So return the error.
data = er.getMessage();
}
// Return the string read from the file
return data;
}
public boolean writeData(String data) throws IOException, FileNotFoundException
{
// Save the data to the File
try (BufferedWriter writer = new BufferedWriter(
new FileWriter("c:\\file.txt"))) {
// Write the data to the File
writer.write(data);
// Return indicating the data was written
return true;
} catch (Exception er) {
return false;
}
}
}