2016-11-28 68 views
1

我正在編寫我的編程介紹項目。該程序應該使用文本文件作爲數據庫來存儲名字,姓氏和電話號碼。該程序需要能夠處理文本文件上的數據。我需要幫助對java中的文本文件進行更改

Instructions

這裏是我的全部代碼至今:

import java.util.*; 
import java.io.*; 
import java.lang.*; 
import java.nio.*; 

public class Project 
{ 
private static Scanner input; 
private static Formatter x; 

public static void main(String[] args) 
{ 
    String choice = ""; 
    Scanner userInput = new Scanner(System.in); 
    System.out.printf("Please choose one of the following commands:%nL (Listing), I (insert), S (search), D (delete)%nM (modify), W (write), Q (quit with saving)"); 

    loop: while (choice != "Q") { 
     System.out.printf("%nEnter your Command: "); 
     choice = userInput.nextLine(); 

     switch (choice) 
     { 
      case "L": 
       Listing(); 
       break; 

      case "I": 
       Insert(); 
       break; 

      case "S": 
       break; 

      case "D": 
       break; 

      case "M": 
       break; 

      case "W": 
       break; 

      case "Q": 
       break loop; 
     } // end switch statement 
    } // end while loop 
} // end method main 

public static void Listing() // List records from MiniDB.txt file 
{ 
    try { 
      File file = new File("MiniDB.txt"); 
      input = new Scanner(file); 
     } 
    catch (IOException ioException) { 
    System.err.println("Cannot open file."); 
    System.exit(1); 
     } 
     if (input.hasNext()) { // If there are records in the file print them, else print none found 
      while (input.hasNext()) // while there is more to read 
      { 
       String firstName = input.next(); 
       String lastName = input.next(); 
       String phoneNumber = input.next(); 

      System.out.printf("%-13s%-13s%s%n", firstName + ",", lastName, phoneNumber); } 
      } 
     else { 
      System.out.printf("No records found"); 
     } 
} // end method Listing 

public static void Insert() // insert a record 
{ 
    try { 
      String file = "MiniDB.txt"; 
      input = new Scanner(System.in); 
      PrintWriter outputStream = new PrintWriter(new FileWriter(file, true)); 

      System.out.printf("Last Name: "); 
      String lastName = input.next(); 
      System.out.printf("%nFirst Name: "); 
      String firstName = input.next(); 
      System.out.printf("%nTelephone Number: "); 
      String phoneNumber = input.next(); 

      outputStream.printf("%n%s %s %s", firstName, lastName, phoneNumber); 
      outputStream.close(); 
      System.out.printf("%nDone."); 
    } 
    catch (IOException ioException) { 
    System.err.println("Cannot open file."); 
    System.exit(1); 
    } 
} // end method Insert 

public static void Search(); // search a record 
{ 

} 
} // end class Project 

我需要什麼是瞭解如何創建「寫」命令幫助。目前,我的代碼會自動對文本文件進行更改,但我只需要在輸入W時完成更改。我一直在閱讀這篇文章,而且我完全陷入困境。我需要重寫我的東西嗎?

我在考慮對temp.txt文件進行所有更改,然後如果輸入W,則重命名temp.txt MiniDB.txt並覆蓋該文件。然後我需要刪除temp.txt文件。我覺得應該有一個更簡單的方法?

回答

0

對於一個基本的解決方案,爲什麼不把數據庫保存在內存中然後更新它。當更新完成時,將內存版本寫入文件

0

我懷疑你採取了錯誤的方法來解決這個問題。如果您應該在命令(W)上編寫文件,那麼最好的解決方案是將數字列表保存在內存中,然後在用戶請求時再次將其寫出。

所以喜歡的東西:

class Entry { 
    private String firstName; 
    private String lastName; 
    private String phoneNumber; 
} 

List<Entry> entries; 

當您收到一個「W」命令,您可以通過條目列表進行迭代,並寫每一個到文件中。

0

一種方法是使用一些內存對象 將數據保持在臨時狀態,並在最終確定時將更改寫入數據庫。

我已經修改了上面的代碼按您的要求 請檢查

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

import org.json.JSONException; 
import org.json.JSONObject; 

public class Project { 
    private static Scanner input; 
    static JSONObject obj = new JSONObject(); 
    static String choice = ""; 
    static int i = 0; 
    final static String file_path = "d:\\1\\MiniDB.txt"; 

    public static void menu() throws IOException, JSONException { 
     System.out.println("Please choose one of the following commands:"); 
     System.out.println("L (Listing)"); 
     System.out.println("I (Insert)"); 
     System.out.println("D (delete)"); 
     System.out.println("S (search)"); 
     System.out.println("M (modify)"); 
     System.out.println("W (write)"); 
     System.out.println("Q (quit with saving)"); 
     choice = input.nextLine(); 
     if (choice.equalsIgnoreCase("I")) { 
      Insert(); 
     } 
     if (choice.equalsIgnoreCase("L")) { 
      Listing(); 
     } 
     if (choice.equalsIgnoreCase("W")) { 
      write(obj); 
     } 
     if (choice.equalsIgnoreCase("Q")) { 
      i = 1; 
      System.out.println("End"); 
     } 
    } 

    public static void main(String[] args) { 
     try { 
      input = new Scanner(System.in); 
      while (i == 0) { 
       menu(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void Listing() // List records from MiniDB.txt file 
    { 
     try { 
      File file = new File(file_path); 
      input = new Scanner(file); 
     } catch (IOException ioException) { 
      System.err.println("Cannot open file."); 
      System.exit(1); 
     } 
     if (input.hasNext()) { // If there are records in the file print them, 
           // else print none found 
      while (input.hasNext()) // while there is more to read 
      { 
       String firstName = input.next(); 
       String lastName = input.next(); 
       String phoneNumber = input.next(); 

       System.out.printf("%-13s%-13s%s%n", firstName + ",", lastName, 
         phoneNumber); 
      } 
     } else { 
      System.out.printf("No records found"); 
     } 
    } // end method Listing 

    public static void Insert() // insert a record 
    { 
     try { 
      System.out.println("Last Name: "); 
      obj.put("Last Name", input.nextLine()); 
      System.out.println("First Name: "); 
      obj.put("First Name", input.nextLine()); 
      System.out.println("Telephone Number: "); 
      obj.put("Telephone Number", input.nextLine()); 
     } catch (Exception e) { 
      System.exit(1); 
     } 
    } 

    public static void write(JSONObject obj2) throws IOException, JSONException { 
     File file = new File(file_path); 
     PrintWriter outputStream = new PrintWriter(new FileWriter(file, true)); 
     outputStream.printf("%n%s %s %s", obj.get("Last Name"), 
       obj.get("First Name"), obj.get("Telephone Number")); 
     System.out.println("changes Done."); 
     outputStream.close(); 
    } 

    public static void Search() { 
    } 

    { 
    } 
} // end class Project 

我希望這將解決您的問題。 謝謝!快樂編碼..

+0

您可以使用相同的json對象執行其他操作 –