Repository navigation
Expand file tree
/
Copy pathFile.java
More file actions
82 lines (69 loc) · 2 KB
/
Copy pathFile.java
File metadata and controls
82 lines (69 loc) · 2 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
72
73
74
75
76
77
78
79
80
81
82
package file_system;
import constants.Exceptions;
/**
* This class represents a virtual file that has content and is a child of a
* directory
*/
public class File extends FileSystemObject {
private String content;
/**
* Constructs a file with given name and parent
* @param name given name
* @param parent given parent
* @throws Exception if object with given name exists in given parent
*/
public File(String name, Directory parent) throws Exception {
this(name, parent, "");
}
/**
* Constructs a file with given name and parent and content
* @param name given name
* @param parent given parent
* @param content given content
* @throws Exception if object with given name exists in given parent
*/
public File(String name, Directory parent, String content) throws Exception {
super(name, parent);
parent.addChild(this);
this.content = content;
}
/**
* Return content of file
*/
public String getContent() {
return this.content;
}
/**
* Set content of file
* @param content is given content
*/
public void setContent(String content) {
this.content = content;
}
/**
* Append content to the file
* @param contentToAppend is content to append to the file
*/
public void appendContent(String contentToAppend) {
this.content += "\n" + contentToAppend;
}
@Override
public void moveFsObjectIn(FileSystemObject fsObject) throws Exception {
if (!(fsObject instanceof File)) {
throw new Exception(Exceptions.MOVE_DIRECTORY_INTO_FILE_ERROR);
}
setContent(fsObject.getContent());
}
/**
* Constructor for cloning purpose
*/
private File(String name, String content) {
this.name = name;
this.content = content;
this.parent = null;
}
@Override
public FileSystemObject cloneObject() {
return new File(name, content);
}
}