Dlaczego ta funkcja wszystko psuje?

0

Witam! Mam problem!
Dam dwa kawałki kodu:

shtdwnatMainFrame.createFile();
		
		f = new File(scr);
		scan = new Scanner(f);
public static void createFile() throws IOException {
		pr = new PrintWriter(f);
		if(checkFileExist()==false)
		{
				f.createNewFile();
				pr.print("00:00");
				pr.close();
		}
		else if(checkFileExist()==true){
			return;
		}
	}
	
	public static boolean checkFileExist(){
		
		if(f.exists()){
			return true;
		}
		else{
			return false;
		}

Problem polega na tym, że funkcja createFile usuwa wszystko co znajduje się w pliku shtdwnatFile.txt i scanner nic tam nie może wyczytać :/ Czemu tak jest?

2

Dzieje się tak dlatego, że każesz Javie ten plik utworzyć lub wyczyścić w pierwszej linijce metody createFile(). Poczytaj sobie, co robi konstruktor PrintWritera:

Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

W takim przypadku Twój kod jest trochę bez sensu, bo najpierw ten plik tworzysz, a potem sprawdzasz czy istnieje - jest oczywiste, że będzie istniał.

Druga rzecz to porządki w Twoim kodzie:

    public static boolean checkFileExist(){
       return f.exists(); // prościej?
    }

I jeszcze pytanie o to:

if(checkFileExist()==false) {
   // ...
} else if(checkFileExist()==true){
   // ...
}

Ile możliwych wartości może mieć typ boolean? Dwie. Więc jeśli wynik funkcji checkFileExists() nie jest false, to możesz w ciemno strzelać, że będzie to true. Ergo, nie potrzebujesz jeszcze raz wywoływać checkFileExists(), by się o tym przekonać. Nie rób tak, bo sobie prędzej czy później strzelisz w stopę takimi zbędnymi wywołaniami.

0
zyxist napisał(a):

Dzieje się tak dlatego, że każesz Javie ten plik utworzyć lub wyczyścić w pierwszej linijce metody createFile(). Poczytaj sobie, co robi konstruktor PrintWritera:

Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

W takim przypadku Twój kod jest trochę bez sensu, bo najpierw ten plik tworzysz, a potem sprawdzasz czy istnieje - jest oczywiste, że będzie istniał.

Druga rzecz to porządki w Twoim kodzie:

    public static boolean checkFileExist(){
       return f.exists(); // prościej?
    }

I jeszcze pytanie o to:

if(checkFileExist()==false) {
   // ...
} else if(checkFileExist()==true){
   // ...
}

Ile możliwych wartości może mieć typ boolean? Dwie. Więc jeśli wynik funkcji checkFileExists() nie jest false, to możesz w ciemno strzelać, że będzie to true. Ergo, nie potrzebujesz jeszcze raz wywoływać checkFileExists(), by się o tym przekonać. Nie rób tak, bo sobie prędzej czy później strzelisz w stopę takimi zbędnymi wywołaniami.

Dzięki wielkie! Następnym razem bd uważał!

1 użytkowników online, w tym zalogowanych: 0, gości: 1