-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.java
More file actions
162 lines (151 loc) · 5.6 KB
/
FileManager.java
File metadata and controls
162 lines (151 loc) · 5.6 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.io.*;
import java.util.Scanner;
/**
* @author Brianna Penkala
* This class represents a file manager
*/
public class FileManager {
/** A field that initializes a placeholder file */
static File file = new File("Passwords");
/** A field that initializes a file directory */
static File directory = new File("Password Files");
/** A field that initializes an array for the directory */
static File[] directoryArray = directory.listFiles();
/** A method that gets the directory array
* @return an array of files from the directory
* */
public static File[] getDirectoryList() {
return directoryArray;
}
/** A method that sets the working file
* @param verifiedFile the new working file
* */
public static void setFile(File verifiedFile) {
file = verifiedFile;
}
/** A method that checks if a file exists
* @return true if the file exists
* */
public boolean fileExists() {
return file.isFile();
}
/** A method that creates an initial file
* @param fileName the name of the new file
* @param salt the salt to be stored in the file
* @param masterPassword the master password to be stored in the file
* */
public static void createInitialFile(String fileName, String salt, String masterPassword) {
file = new File(directory, fileName);
try {
FileWriter writer = new FileWriter(file, true);
writer.write(salt + " : " + masterPassword);
writer.close();
} catch (IOException e) {
System.out.println("I/O issue.");
}
}
/** A method that appends a password to a file
* @param website the website associated with the new password
* @param encryptedPassword the encrypted password to be added
* */
public static void appendPassword(String website, String encryptedPassword) {
if(contains(website)) {
overwriteFile(website); //just removes the old website password line, appends as usual with the rest of this method
System.out.println("Website already has a password. Replacing old password.");
}
try {
FileWriter writer = new FileWriter(file, true);
writer.write(System.lineSeparator());
writer.write(website + " : " + encryptedPassword);
writer.close();
} catch (IOException e) {
System.out.println("IO issue.");
}
}
/** A method that gets a password in a file
* @param website the website associated with the password
* @return the password
* */
public static String getPassword(String website) {
Scanner myReader = null;
try {
myReader = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
int lengthWeb = website.length(); //gets length of username to determine correct line in file
String password;
while (myReader.hasNextLine()) {
String line = myReader.nextLine();
if (line.substring(0, lengthWeb).equals(website)) {
int index = line.indexOf(": ");
password = line.substring(index + 2);
return password;
}
}
return "fail";
}
/** A method that gets the salt from a file
* @param file the file that contains the salt
* @return the salt
* */
public static String getSalt(File file) {
Scanner myReader = null;
try {
myReader = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
String line = myReader.nextLine();
int index = line.indexOf(" :");
return line.substring(0, index);
}
/** A method that checks if a file contains a specified website
* @param website the website being checked
* @return true if the website is found in the file
* */
public static boolean contains(String website) {
Scanner myReader;
boolean containsWebsite = false;
try {
myReader = new Scanner(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while(myReader.hasNextLine()) {
String line = myReader.nextLine();
if(line.contains(website)) {
containsWebsite = true;
}
}
return containsWebsite;
}
/** A method that creates a duplicate file without the line containing the password to be rewritten
* @param website the website associated with the password to be replaced
* */
public static void overwriteFile(String website) { //will only run when it is confirmed the file contains the website
File oldFile = file;
File newFile = new File(directory, file.getName() + " "); //temp fix
Scanner myReader = null;
try {
myReader = new Scanner(oldFile);
//really need to write this once
FileWriter writer = new FileWriter(newFile, true);
String line = myReader.nextLine();
writer.write(line);
while (myReader.hasNextLine()) {
line = myReader.nextLine();
if (!line.contains(website)) {
writer.write(System.lineSeparator());
writer.write(line);
}
}
writer.close();
} catch (IOException e) {
System.out.println("I/O issue.");
}
myReader.close();
oldFile.delete();
file = newFile;
}
}