-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFile.java
More file actions
69 lines (56 loc) · 1.26 KB
/
TextFile.java
File metadata and controls
69 lines (56 loc) · 1.26 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
package apps.files;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import apps.text.Sentence;
import apps.text.Word;
public class TextFile extends File
{
public TextFile(Directory path, String name)
{
super(path, name, "txt");
}
@Override
public int append(byte[] bytes)
{
try
{
Files.write(systemRepresentation.getAbsoluteFile().toPath(), bytes, StandardOpenOption.APPEND);
return bytes.length;
}
catch (IOException e)
{
e.printStackTrace();
return 0;
}
}
public int append(Sentence sentence)
{
return this.append(sentence.toString().getBytes(StandardCharsets.UTF_8));
}
public int append(Word word)
{
return this.append(word.toString().getBytes(StandardCharsets.UTF_8));
}
@Override
public String toString()
{
try
{
FileReader reader = new FileReader(systemRepresentation);
long size = Files.size(Paths.get(systemRepresentation.getAbsolutePath()));
char[] data = new char[(int) size];
reader.read(data, 0, (int) size);
reader.close();
return new String(data);
}
catch (IOException e)
{
e.printStackTrace();
return "";
}
}
}