2016-11-28 40 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对象执行其他操作 –