未创建新的 txt 文件

New txt file not being created

我正在处理一个设置文件,如果 none 存在,该文件将被创建。我试过创建一个文件,但它也没有用。下面的代码用于创建一个新的txt文件。

            try {
            PrintWriter SettFile = new PrintWriter(new BufferedWriter(new FileWriter("Settings.txt")));
            SettFile.println("170 000 150");
            SettFile.println("114 000 106");
            SettFile.close();
        } catch (IOException e) {
            System.out.println("An error Accured Loading The Settings");
        }
        System.out.println("Setting File Succesfuly Created");

以下代码用于搜索值。

        try{
        BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("Settings")));
        System.out.println("Reading Values from \"Settings.txt\"...");
        int i = 0;
        String TempSettingValue;

        while((TempSettingValue = file.readLine()) != null){
            SettingValue[i] = TempSettingValue;
            System.out.println("Value " + (i + 1) + ": " + SettingValue[i]);
            i++;
        }

        file.close();
    }
    catch(IOException e){
        System.out.println("Something went wrong loading the Settings...");
    }

解决方案

问题是你在找"Settings"

 BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("Settings")));

将该行更改为此。

 BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("Settings.txt")));