我需要帮助在java中更改文本文件
我正在做我的编程入门项目。该程序应该使用一个文本文件作为数据库来存储名字、姓氏和电话号码。程序需要能够操作文本文件上的数据 以下是我到目前为止的全部代码:我需要帮助在java中更改文本文件,java,database,text,savechanges,Java,Database,Text,Savechanges,我正在做我的编程入门项目。该程序应该使用一个文本文件作为数据库来存储名字、姓氏和电话号码。程序需要能够操作文本文件上的数据 以下是我到目前为止的全部代码: import java.util.*; import java.io.*; import java.lang.*; import java.nio.*; public class Project { private static Scanner input; private static Formatter x; public stat
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
我需要的帮助是理解如何创建“write”命令。目前,我的代码自动对文本文件进行更改,但我需要它仅在输入W时完成更改。我已经读了一段时间了,我完全被卡住了。我需要重写我所拥有的吗
我正在考虑对temp.txt文件进行所有更改,然后如果输入了W,则重命名temp.txt MiniDB.txt并覆盖该文件。然后我需要删除temp.txt文件。我觉得应该有一个更简单的方法?对于一个基本的解决方案,为什么不将数据库保存在内存中,然后更新它呢。更新完成后,只需将内存中的版本写入一个文件即可获得基本解决方案,为什么不将数据库保留在内存中,然后进行更新。更新完成后,只需将内存中的版本写入文件我怀疑您采取了错误的方法来解决此问题。如果您应该在命令(W)上写入文件,那么最好的解决方案是将数字列表保存在内存中,然后在用户请求时再次写入 比如:
class Entry {
private String firstName;
private String lastName;
private String phoneNumber;
}
List<Entry> entries;
类条目{
私有字符串名;
私有字符串lastName;
私有字符串电话号码;
}
列出条目;
当您收到“W”命令时,您可以遍历条目列表并将每个条目写入文件。我怀疑您采取了错误的方法来解决此问题。如果您应该在命令(W)上写入文件,那么最好的解决方案是将数字列表保存在内存中,然后在用户请求时再次写入 比如:
class Entry {
private String firstName;
private String lastName;
private String phoneNumber;
}
List<Entry> entries;
类条目{
私有字符串名;
私有字符串lastName;
私有字符串电话号码;
}
列出条目;
当您收到“W”命令时,您可以遍历条目列表并将每个条目写入文件。一种方法是使用一些内存对象将数据保持在临时状态 并在完成时将更改写入数据库 我已根据您的要求修改了上述代码 请查收
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
我希望这能解决你的问题。
非常感谢。快乐编码..一种方法是使用一些内存对象将数据保持在临时状态 并在完成时将更改写入数据库 我已根据您的要求修改了上述代码 请查收
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
我希望这能解决你的问题。
非常感谢。快乐编码..您可以使用相同的json对象执行其他操作您可以使用相同的json对象执行其他操作