Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ArrayList、Java更新文件_Java_Arraylist - Fatal编程技术网

如何使用ArrayList、Java更新文件

如何使用ArrayList、Java更新文件,java,arraylist,Java,Arraylist,我正在编写一个方法,该方法将接收一些命令行参数,验证它们,如果有效,将编辑机场代码。机场名称及其代码存储在CSV文件中。例如“贝尔法斯特有限公司”。命令行参数输入如下:java editAirport EA BEL Belfast,“EA”是使项目知道我要编辑机场代码的2letter代码,“BEL”是新代码,Belfast是机场的名称。 当我检查了cla并验证了它们后,我通读了文件并将它们存储在一个ArrayList中,名为“Belfast,BEL”。然后我想通过删除文本文件中的行并将其转储到a

我正在编写一个方法,该方法将接收一些命令行参数,验证它们,如果有效,将编辑机场代码。机场名称及其代码存储在CSV文件中。例如“贝尔法斯特有限公司”。命令行参数输入如下:java editAirport EA BEL Belfast,“EA”是使项目知道我要编辑机场代码的2letter代码,“BEL”是新代码,Belfast是机场的名称。 当我检查了cla并验证了它们后,我通读了文件并将它们存储在一个ArrayList中,名为“Belfast,BEL”。然后我想通过删除文本文件中的行并将其转储到arraylist中来更新文本文件,但我不知道该怎么做。有人能告诉我一种使用简单代码(没有高级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();    
        }   
    }
}

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

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

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

我投票将此问题作为离题题题结束,因为这是一个“有人能给我看代码吗”的家庭作业转储类型的问题。这是怎么离题的!!我需要帮助解决一个我已经遇到了几天的严重问题,但我找不到任何解决方案。这里的混乱可能是代码中的大注释,看起来很像许多人所做的“为我填写这一点!!!”。事实上,你已经在下面几行中实现了它-你有一个合理的问题在这里,只是有点难以理解:)对不起,我只是对它进行了评论,以便人们能够理解正在发生的事情,我把大的评论放在最后一行,因为在那一行中发生了很多事情。我的主要问题也是在最后一个问题上,当我试图打印arraylist时,我只是把它添加到代码的末尾,而不是更新它。是的,有没有办法覆盖文件中的实际行呢?不是像你想做的那样-你不能只是说“将x行设置为值s”。您必须再次写出整个文件,在到达要替换的行的地方,打印该行而不是刚才读取的行。让我这样说:您正在读取值(例如)[a,b,c,d,e,f…],并且您想将“c”更改为“p”,所以您要写出[a,b,p,d,e,f…]-它几乎已经在做了。(哦,你可能想调用“output.flush()”?)。好吧,我在arraylist中把它们都正确地按顺序排列了,那么如何在arraylist中打印数据(我猜是for循环)在文件中,我只是不明白如何做。抱歉,如果这很烦人,我仍然只是在学习Java抱歉,误读了您的代码。然后您真的只想写入输出文件,而不必费心先读取它-然后您将用机场列表中的值覆盖文件中的任何值。
line2 = in2.nextLine();
line2 = airport.get(i);