2016-04-29 5 views
0

Wie das Thema im im versucht, eine bestimmte Zeichenfolge, die in der Regel automatisch generiert wird, in die gleiche Zeichenfolge und es scheint zu funktionieren, weil die temporäre Datei erstellt wird und die string wird durch "" ersetzt, aber es scheint, dass es eine IOException gibt, wenn es darum geht, beim Löschen in das Original umzubenennen, bitte helfen?Java Erstellen einer temporären Datei, Löschen einer bestimmten Zeichenfolge und Umbenennen in Original

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(); 
    } 

} 
+1

Seitliche Anmerkung zur Code-Qualität: solche endlosen If-Ketten zur Auswahl einer Enum ... sehr schlechte Idee. Stattdessen könnten Sie Ihrer enum-Klasse einen int-take-Konstruktor hinzufügen. dann fügen Sie eine Methode wie 'getSettlementFor (int selector)' hinzu. Diese Methode würde alle Enums iterieren und "ihr int" überprüfen; und gib den passenden zurück. Sie wollen wirklich solche "Mappings" nicht außerhalb Ihres Enums streuen; Sie möchten nie mehr als einen Platz in Ihrem Code haben, der solche Mapping-Arbeiten durchführt! – GhostCat

+0

Bitte Stack-Trace zur Frage – Sanjeev

+0

@ Jägermeister hinzufügen Danke für den Kommentar zu meiner Code-Qualität, die Sache wird in einigen Tagen fällig und plante das Polieren sobald es fertig ist. Ich bin super neu zu Java und OOP im Allgemeinen, also weiß ich nicht, wie lange ich genau nehmen werde – YourfavOreo

Antwort

0

ich geprüft, ob es löschbar war (File.Delete wirft eine boolean Ausnahme), so habe ich versucht, es direkt über Fenster zu löschen und anscheinend wurde es von Java verwendet wird, so ging ich davon aus verfinsterte Glitch und beim Schließen und Eclipse-Boote es hat wieder geklappt ..... naja ... das war eher anti-climactic .... Vielen Dank für die Diskussion, ich hätte es bestimmt nie rausgefunden, wenn nicht für die Diskussion: D < 3 Sie sind alle in meinem Herzen

Verwandte Themen