簡體   English   中英

Java創建臨時文件,刪除特定的字符串並重命名為原始

[英]Java creating a temp file , deleting a specific string and rename to original

正如該主題指出的那樣,im試圖獲取通常自動生成為相同字符串的特定字符串,並且似乎可以正常工作,因為已創建了臨時文件,並且該字符串被替換為“”,但似乎出現了IOException刪除時重命名為原始文件,請幫助?

import java.io.*;
import java.util.Scanner;

/**
 * Main class to test the Road and Settlement classes
 * 
 * @author Chris Loftus (add your name and change version number/date)
 * @version 1.0 (24th February 2016)
 *
 */
public class Application {

    private Scanner scan;
    private Map map;
    private static int setting;

    public Application() {
        scan = new Scanner(System.in);
        map = new Map();
    }

    private void runMenu() {
        setting = scan.nextInt();
        scan.nextLine();

    }

    // STEP 1: ADD PRIVATE UTILITY MENTHODS HERE. askForRoadClassifier, save and
    // load provided

    private Classification askForRoadClassifier() {
        Classification result = null;
        boolean valid;
        do {
            valid = false;
            System.out.print("Enter a road classification: ");
            for (Classification cls : Classification.values()) {
                System.out.print(cls + " ");
            }
            String choice = scan.nextLine().toUpperCase();
            try {
                result = Classification.valueOf(choice);
                valid = true;
            } catch (IllegalArgumentException iae) {
                System.out.println(choice + " is not one of the options. Try again.");
            }
        } while (!valid);
        return result;
    }

    private void deleteSettlement() {

        String name;
        int p;
        SettlementType newSetK = SettlementType.CITY;
        int set;
        System.out.println("Please type in the name of the settlement");
        name = scan.nextLine();
        System.out.println("Please type in the population of the settlment");
        p = scan.nextInt();
        scan.nextLine();
        System.out.println("Please type in the number of the type of settlement .");
        System.out.println("1: Hamlet");
        System.out.println("2: Village");
        System.out.println("3: Town");
        System.out.println("4: City");
        set = scan.nextInt();
        scan.nextLine();

        if (set == 1) {
            newSetK = SettlementType.HAMLET;
        }

        if (set == 2) {
            newSetK = SettlementType.VILLAGE;
        }

        if (set == 3) {
            newSetK = SettlementType.TOWN;
        }

        if (set == 4) {
            newSetK = SettlementType.CITY;
        }

        String generatedResult = "Name: " + name + " Population: " + p + " SettlementType " + newSetK;

        String status = searchAndDestroy(generatedResult);
    }

    private String searchAndDestroy(String delete) {
        File file = new File("C:\\Users\\Pikachu\\workspace\\MiniAssignment2\\settlements.txt");

        try {
            File temp = File.createTempFile("settlement", ".txt", file.getParentFile());

            String charset = "UTF-8";

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));

                PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));

                for (String line; (line = reader.readLine()) != null;) {
                    line = line.replace(delete, "");
                    writer.println(line);

                }
                System.out.println("Deletion complete");
                    reader.close();
                    writer.close();
                file.delete();
                temp.renameTo(file);
            } 



            catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                System.out.println("Sorry! Can't do that! 1");
            }

            catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("Sorry! Can't do that! 2");
            }


        } 

        catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Sorry! Can't do that! , IO Exception error incurred 3");
        }


        return null;
    }

    private void save() {
        map.save();
    }

    private void load() {
        map.load();
    }

    public void addSettlement() {
        String name;
        int p;
        SettlementType newSetK = SettlementType.CITY;
        int set;
        System.out.println("Please type in the name of the settlement");
        name = scan.nextLine();
        System.out.println("Please type in the population of the settlment");
        p = scan.nextInt();
        scan.nextLine();
        System.out.println("Please type in the number of the type of settlement .");
        System.out.println("1: Hamlet");
        System.out.println("2: Village");
        System.out.println("3: Town");
        System.out.println("4: City");
        set = scan.nextInt();
        scan.nextLine();

        if (set == 1) {
            newSetK = SettlementType.HAMLET;
        }

        if (set == 2) {
            newSetK = SettlementType.VILLAGE;
        }

        if (set == 3) {
            newSetK = SettlementType.TOWN;
        }

        if (set == 4) {
            newSetK = SettlementType.CITY;
        }

        new Settlement(name, newSetK, p);
    }

    private void printMenu() {
        System.out.println("Please type in the number of the action that you would like to perform");
        System.out.println("1: Create Settlement");
        System.out.println("2: Delete Settlement");
        System.out.println("3: Create Road");
        System.out.println("4: Delete Road");
        System.out.println("5:Display Map");
        System.out.println("6:Save Map");
    }

    public static void main(String args[]) {
        Application app = new Application();

        app.printMenu();
        app.runMenu();
        System.out.println(setting);
        if (setting == 1) {
            app.addSettlement();
        }

        if (setting == 2) {
            app.deleteSettlement();
        }
        app.load();
        app.runMenu();
        app.save();
    }

}

我檢查了它是否可刪除(file.delete拋出一個布爾異常),所以我嘗試直接通過Windows刪除它,顯然它已被Java使用,因此我認為出現了小故障,在關閉並重新啟動Eclipse后它就可以工作了。很好...那是反氣候的....非常感謝您的討論,如果沒有討論,我絕對不會想出來的:D <3您全心全意

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM