gpt4 book ai didi

java - 如何使用 ArrayList、Java 更新文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:32 25 4
gpt4 key购买 nike

我正在编写一个方法,它将接受一些命令行参数,验证它们,如果有效,将编辑机场的代码。机场名称及其代码存储在 CSV 文件中。一个例子是“贝尔法斯特,BHD”。命令行参数输入如下,java editAirport EA BEL Belfast,“EA”是 2 个字母的代码,使项目知道我要编辑机场的代码,“BEL”是新代码,贝尔法斯特是机场的名称。当我检查完 cla 并验证它们后,我通读了文件并将它们存储在 ArrayList 中,如“贝尔法斯特,BEL”。然后我想通过从文本文件中删除行并转储到数组列表中来更新文本文件,但我不知道该怎么做。有人可以告诉我一种使用简单代码(没有高级java东西)的方法吗?这是我的程序

    import javax.swing.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class editAirport
{
public static void main(String [] args)throws IOException
{
String pattern = "[A-Z]{3}";
String line, line1, line2;
String[] parts;
String[] parts1;
boolean found1 = false, found2 = false;
File file = new File("Airports.txt"); // I created the file using the examples in the outline
Scanner in = new Scanner(file);
Scanner in1 = new Scanner(file);
Scanner in2 = new Scanner(file);
String x = args[0], y = args[1], z = args[2];
//-------------- Validation -------------------------------
if(args.length != 3) // if user enters more or less than 3 CLA's didplay message
JOptionPane.showMessageDialog(null, "Usage: java editAirport EA AirportCode(3 letters) AirportName");
else if(!(file.exists())) // if "Airports.txt" doesn't exist end program
JOptionPane.showMessageDialog(null, "Airports.txt does not exist");
else // if everything is hunky dory
{
if(!(x.equals("EA"))) //if user doesn't enter EA an message will be displayed
JOptionPane.showMessageDialog(null, "Usage: java editAirport EA AirportCode(3 letters) AirportName");
else if(!(y.matches(pattern))) // If the code doesn't match the pattern a message will be dislayed
JOptionPane.showMessageDialog(null, "Airport Code is invalid");

while(in.hasNext())
{
line = in.nextLine();
parts = line.split(",");
if(y.equalsIgnoreCase(parts[1]))
found1 = true; //checking if Airport code already is in use
if(z.equalsIgnoreCase(parts[0]))
found2 = true; // checking if Airport name is in the file
}
if(found1)
JOptionPane.showMessageDialog(null, "Airport Code already exists, Enter a different one.");
else if(found2 = false)
JOptionPane.showMessageDialog(null, "Airport Name not found, Enter it again.");
else

/*
Creating the ArrayList to store the name,code.
1st while adds the names and coses to arraylist,
checks if the name of the airport that is being edited is in the line,
then it adds the new code onto the name.
sorting the arraylist.
2nd for/while is printing the arraylist into the file
*/
ArrayList<String> airport = new ArrayList<String>();
while(in1.hasNext()) // 1st while
{
line1 = in1.nextLine();
if(line1.contains(z))
{
parts1 = line1.split(",");
parts1[1] = y;
airport.add(parts1[0] + "," + parts1[1]);
}
else
airport.add(line1);
}
Collections.sort(airport); // sorts arraylist
FileWriter aFileWriter = new FileWriter(file, true);
PrintWriter output = new PrintWriter(aFileWriter);
for(int i = 0; i < airport.size();)
{
while(in2.hasNext()) // 2nd while
{
line2 = in2.nextLine();
line2 = airport.get(i);
output.println(line2);
i++;
}
}
output.close();
aFileWriter.close();
}
}
}

}

Airports.txt 文件是这个

    Aberdeen,ABZ
Belfast City,BHD
Dublin,DUB
New York,JFK
Shannon,SNN
Venice,VCE

最佳答案

我认为你的问题可能在于两行:

line2 = in2.nextLine();
line2 = airport.get(i);

这将覆盖内存中的“line2”,但不会覆盖文件中的“line2”。

关于java - 如何使用 ArrayList、Java 更新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com