view kopieckreta/src/ConfigFile.java @ 14:49dd434149cd

README.md immposiblerush
author prymula <prymula76@outlook.com>
date Sat, 10 Feb 2024 20:40:34 +0100
parents 2787f5e749ae
children
line wrap: on
line source


package src;

/**
 *
 * @author przem
 */

import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;  // Import this class to handle errors
import java.util.Scanner;
import java.util.*;

public class ConfigFile {
    public String userDir="", userHome="", osName="", osVersion="", jvmVersion="";
    String name = "/kopieckreta.txt";
    public ConfigFile() throws IOException{
        for (Map.Entry<?,?> e : System.getProperties().entrySet()) {
            //System.out.println(String.format("%s = %s", e.getKey(), e.getValue())); 
            if (e.getKey().equals("user.dir")) {
                userDir=String.format("%s", e.getValue());
            }
            if (e.getKey().equals("user.home")) {
                userHome=String.format("%s", e.getValue());
                
            }
            if (e.getKey().equals("os.name")) {
                osName=String.format("%s", e.getValue());
                
            }
            if (e.getKey().equals("os.version")) {
                osVersion=String.format("%s", e.getValue());
                
            }
            if (e.getKey().equals("java.vm.version")) {
                jvmVersion=String.format("%s", e.getValue());
                
            }
        }
        
        
        File file=new File (userHome+name);
        if (file.createNewFile()){
            System.out.println("Plik zostal utworzony");
            try {
                FileWriter myWriter = new FileWriter(userHome+name);
                myWriter.write("1");
                myWriter.close();
                System.out.println("Successfully wrote to the file.");
            } catch (IOException e) {
                System.out.println("An error occurred.");
                e.printStackTrace();
            }
        }
        else {
            System.out.println("Plik istnieje");
        }
    }
    public void save(boolean b){
        String s="";
        try {
            FileWriter myWriter = new FileWriter(userHome+name);
            if (b) s="1";
            else s="0";
            myWriter.write(s);
            myWriter.close();
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
    public boolean load() {
        String data = "";
        boolean r =false;
        try {
            File myObj = new File(userHome+name);
            Scanner myReader = new Scanner(myObj);
            while (myReader.hasNextLine()) {
            data = myReader.nextLine();
            System.out.println(data);
        }
        myReader.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
        
        if (data.equals("1")) r=true;
        
        return r;
    }
    
}