witam, mam problem:
gdy próbuję wybrać opcję wpis danych ukazuje mi sie taki błąd:
Exception in thread "main" java.io.FileNotFoundException: dane.txt (Odmowa dostępu)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.PrintWriter.<init>(Unknown Source)
at pl.javastart.first.Fjp.main(Fjp.java:48)
gdy wybieram opcję zmiana hasla i wpisuję nowe haslo, wyswietla się to samo, tylko
ze ścieżką password.txt.
Kod jest poniżej.
package pl.javastart.first;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Fjp {
public static void main(String[] args) throws FileNotFoundException {
String password, password2, dane_str;
int dec;
System.out.println("Witaj w bezpiecznym banku informacji!");
File haslo = new File("password.txt");
Scanner readfilepass = new Scanner(new File("password.txt"));
password = readfilepass.nextLine();
//traktuje 0 jakby nie bylo hasla, jest na poczatku w pliku password.txt
if ("0".equals(password)) {
System.out.println("Ustaw haslo");
Scanner readpass = new Scanner(System.in);
password = readpass.nextLine();
PrintWriter savepass = new PrintWriter("password.txt");
savepass.println(password);
savepass.close();
}
//pętla od wpisywania hasla
for(int v1 = 1;v1>0;++v1) {
System.out.println("Wpisz haslo");
Scanner readpass2 = new Scanner(System.in);
password2 = readpass2.nextLine();
if(password.equals(password2)) {
v1 = 0;
for(int v2 = 1;v2>0;++v2) {
System.out.println("Co chcesz zrobic?");
System.out.println("1 -> odczyt danych");
System.out.println("2 -> wpis danych");
System.out.println("3 -> zmiana hasla");
Scanner decision = new Scanner(System.in);
dec = decision.nextInt();
decision = null;
if (dec == 1) {
File dane = new File("dane.txt");
Scanner data = new Scanner(new File("dane.txt"));
dane_str = data.nextLine();
System.out.println(dane_str);
Scanner whatever1 = new Scanner(System.in);
}
if (dec == 2) {
Scanner savedata = new Scanner(System.in);
PrintWriter savingdata = new PrintWriter("dane.txt");
savingdata.println(savedata);
savingdata.close();
System.out.println("Zapisano pomyslnie");
Scanner whatever2 = new Scanner(System.in);
}
if(dec == 3) {
System.out.println("Ustaw haslo");
Scanner readpass = new Scanner(System.in);
password = readpass.nextLine();
PrintWriter savepass = new PrintWriter("password.txt");
savepass.println(password);
savepass.close();
System.out.println("Haslo zapisane pomyslnie");
Scanner whatever3 = new Scanner(System.in);
}
}
}
else {
System.out.println("zle haslo");
}
}
}
}