Repository navigation
Expand file tree
/
Copy pathFileOperations.java
More file actions
66 lines (62 loc) · 1.65 KB
/
Copy pathFileOperations.java
File metadata and controls
66 lines (62 loc) · 1.65 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
import java.io.*;
import java.awt.*;
public class FileOperations {
FileInputStream fis;
BufferedReader br;
public File f;
public void readAndappendToTextArea(String selectedFile, TextArea ta) throws Exception{
ta.setText(""); // Clear the contents of the text area if the new file is being opened.
try{
String line = "";
openFile(selectedFile);
if(selectedFile.endsWith(".mg")){
StringBuilder data = new StringBuilder();
Decrypt decrypt = new Decrypt(); // for the decryption of the key.
DataInputStream dis = new DataInputStream(fis);
StringBuilder encLine = new StringBuilder();
while(true){
try{
char currentChar = dis.readChar();
encLine.append(currentChar);
}catch(EOFException e){
ta.append(decrypt.decryptData(encLine.toString()));
break;
}
}
}else{
while((line = br.readLine()) != null){
ta.append(line + "\n");
}
}
fis.close();
br.close();
}catch(IOException e){
throw e;
}
}
private void openFile(String selectedFile) throws Exception{
try{
f = new File(selectedFile);
fis = new FileInputStream(f);
br = new BufferedReader(new InputStreamReader(fis));
}catch(Exception e){
throw e;
}
}
public void saveFile(String savedFileName, TextArea ta) throws Exception{
Encrypt encrypt = new Encrypt(ta.getText());
String encryptedData = encrypt.encryptText();
try{
if(f == null){
f = new File(savedFileName);
}
FileOutputStream fos = new FileOutputStream(f, false);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(encryptedData);
fos.close();
dos.close();
}catch(Exception e){
throw e;
}
}
}