Here I have 2 methods.
private boolean readData(){
if (!new File(config).exists()) {
return false;
} else {
Hashtable<String, String> data;
FileInputStream fis;
try {
fis = new FileInputStream(config);
ObjectInputStream oin = new ObjectInputStream(fis);
data = (Hashtable<String, String>) oin.readObject();
username = data.get("username");
password = data.get("password");
folder = data.get("folder");
fis.close();
oin.close();
return true;
} catch (FileNotFoundException fnfe) {
return false;
} catch (IOException ioe) {
return false;
} catch (ClassNotFoundException cnfe) {
return false;
}
}
}
private boolean writeData() {
try {
FileOutputStream fos = new FileOutputStream(config);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Hashtable<String, String> data = new Hashtable<>();
sout(username);
data.put("username", username);
data.put("password", password);
data.put("folder", folder);
oos.writeObject(data);
oos.flush();
oos.close();
Runtime.getRuntime().exec("attrib +H " + config);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
I couldn't find an answer on Stack Overflow.
By the way - exception is always throws from writeData, FileOutputStream.
P.S.
I forgot to add. When the program is started for the first time, it successfully records the file data. As well, it reads data from a file always without problems. Problem is appearing whem i'm trying to rewrite this file.
if(new File(config).canWrite()){
sout("Can write");
} else{
sout("I can't write");
}
writeData();
Is printing I can't write but actually it works. I mean that the file appears and the data is successfully written into it.
readData()and 'writeData()`