I have a java app that gets commands from a webpanel, and when it executes a command, it saves somewhere so that it knows that it has executed already. then when it next executes a command, it checks the list before executing the command, this works fine one a PC, but on a mac, it seems to not work.
it saves the commands, but when it checks for new commands, it executes all previous commands aswell.
n3.data contains:
1,2,3,4,
each command is given an id (in this case 1 2 3 and 4), the app is supposed to check what command ids it has used and then execute if the id is not in the specified file (n3.data)
here is the code.
public void save(int id) {
String osName = System.getProperty("os.name");
if(osName.contains("Windows")){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "\\app.data", true));
bw.write(id + ",");
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} else if(osName.contains("Mac")){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/n3.data", true));
bw.write(id + ",");
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void createNew() {
String osName = System.getProperty("os.name");
File win = new File(System.getProperty("user.home") + "\\app.data");
File mac = new File(System.getProperty("user.home") + "/n3.data");
if(osName.contains("Windows") && !win.exists()){
try {
new File(System.getProperty("user.home") + "\\app.data").createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}else if(osName.contains("Mac") && !mac.exists()){
try {
new File(System.getProperty("user.home") + "/n3.data").createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void saveNew() {
String osName = System.getProperty("os.name");
if(osName.contains("Windows")){
StringBuilder sb = new StringBuilder();
for (int i : processedIds) {
sb.append(i + ",");
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "\\app.data"));
bw.write(sb.toString());
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}else if(osName.contains("Mac")){
StringBuilder sb = new StringBuilder();
for (int i : processedIds) {
sb.append(i + ",");
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/n3.data"));
bw.write(sb.toString());
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void loadSave() throws IOException {
String osName = System.getProperty("os.name");
if(osName.contains("Windows")){
File file = new File(System.getProperty("user.home") + "\\app.data");
if (file.exists()) {
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
for (String s : sb.toString().split(",")) {
try {
processedIds.add(Integer.parseInt(s));
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
file.createNewFile();
}
}else if(osName.contains("Mac")){
File file = new File(System.getProperty("user.home") + "/n3.data");
if (file.exists()) {
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
for (String s : sb.toString().split(",")) {
try {
processedIds.add(Integer.parseInt(s));
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (!file.exists()){
file.createNewFile();
}
}
}
I figured it out, anyone having the same problem, the code doesn’t check properly if the file exists or not. just fix that and it should work.