当前位置: 首页 > 知识库问答 >
问题:

替换txt文件中的特定行

林波鸿
2023-03-14

我正在从文本文件中读取预订系统的座位数据,并将其写入ArrayList中。一个行将被放入一个可变的fileline中。文件行是从每个“,”中拆分出来的,并放入名为Components的数组中。然后将组件放入对象中。

txt文件示例

1a, first, window, notable, forward, noeoa, false, null 
1b, first, nowindow, notable, forward, noeoa, false, null
2a, first, window, notable, forward, eoa, false, null
2b, first, nowindow, notable, forward, eoa, false, null
3a, first, window, table, forward, noeoa, false, null
3b, first, nowindow, table, forward, noeoa, false, null

然后进入一个if语句,检查座位是否合适,如果合适,我想将false更改为true,并将乘客名添加为null,但只在合适的座位上。我想知道是否有任何方法可以覆盖文本文件中的特定行。行号存储在一个名为lineCounter的变量中

Scanner inFile = new Scanner(
            new FileReader("seats.txt"));
    ArrayList<seat> seats = new ArrayList<>();
    Boolean var = false;

    while (inFile.hasNext()) {

        String fileLine = inFile.nextLine();

        seats.add(new seat(fileLine));

        if (seat.classs.equals(seatClass) && (seat.window.equals(seatLocation) || seat.both != null)
                && seat.table.equals(seatTable) && seat.face.equals(seatFace) && seat.EOA.equals(seatEOA)
                && seat.resevered == false) {
            System.out.print("Seat " + seat.seat + " Booked");
            var = true;
            seat.resevered = true;
            seat.resName = emailSelect;


        }

其中文件线被拆分

public seat(String fileLine)
{

String[] components = fileLine.split(",");

if (components[7].equals("null"))
    this.resName = null;
else
    this.resName = components[7];

if (components[2].equals(" both"))
    this.both = "notnull";
else
    this.both = null;

this.seat = (components[0].trim());
this.classs = (components[1].trim());
this.window = (components[2].trim());
this.table = (components[3].trim());
this.face = (components[4].trim());
this.EOA = (components[5].trim());
this.resevered = Boolean.parseBoolean(components[6].trim());

}

共有1个答案

太叔志尚
2023-03-14

假设您想要覆盖文件中的数据,请查看下面的代码,我在修改了您的代码后编写了这些代码

Scanner inFile = new Scanner(
        new FileReader("seats.txt"));
ArrayList<seat> seats = new ArrayList<>();
Boolean var = false;
int count=0;
while (inFile.hasNext()) {

    String fileLine = inFile.nextLine();
    count++;

    if(count==lineCounter){
       fileLine=fileLine.replace(oldValue,overWriteValue);
    }
    seats.add(new seat(fileLine));

    if (seat.classs.equals(seatClass) && (seat.window.equals(seatLocation) || seat.both != null)
            && seat.table.equals(seatTable) && seat.face.equals(seatFace) && seat.EOA.equals(seatEOA)
            && seat.resevered == false) {
        System.out.print("Seat " + seat.seat + " Booked");
        var = true;
        seat.resevered = true;
        seat.resName = emailSelect;


    }

解决办法是直截了当的,我希望你找过这个,或者让我知道。

 类似资料:
  • 我仍然是python的新手,我已经尝试过这种方式覆盖txt文件上的一行 ''' 答案 =输入(“俄/秒/升/米:”) ''' 它会替换txt文件上的所有文本行,无论我想做什么,当我选择R时,它会写入txt文件的第一行,当我选择S时,它会写入txt文件的第三行 我现在已经尝试过了 ''' ''' 有人能告诉我正确的方向吗

  • 问题内容: 我有一个名为log.txt的文本文件,它具有以下数据 第一个逗号之前的数字是指定每个项目的索引。 我想做的是读取文件,然后将给定行中字符串的一部分(例如textFiles / a.txt)替换为另一值(例如something / bob.txt)。 这就是我到目前为止 问题答案: 一种方法是使用: 您还可以使用正则表达式,或查找搜索字符串在一行中的何处。

  • 问题内容: 好的,所以我有这个yaml文件,我想替换一个字符串 与字符串 但是我不知道该怎么办。这是完整的Yaml文件 问题答案: 假设您使用的操作系统不错,并且您的YAML文档称为:

  • 问题内容: 如何使用php替换特定行。我不知道行号。我要替换包含特定单词的行。 问题答案: 您可以在较小的文件上使用一种方法,该文件可以两次装入您的内存: 快速说明,PHP> 5.3.0支持lambda函数,因此您可以删除命名的函数声明并将映射缩短为: 从理论上讲,您可以使它成为单个(很难理解)的php语句: 您应该对较大的文件使用另一种(较少占用内存的方法):

  • 假设我有一个txt文件,如下所示: 用户在变量上输入后,txt文件如下: 如何删除空行?

  • 问题内容: 我的Linux机器上有一个2GB的文本文件,我正尝试将其导入数据库。 我遇到的问题是,正在处理此rdf文件的脚本在一行上令人窒息: 我想替换用。我无法在所有行上进行搜索/替换,但是我有行号,因此我希望有一些简单的方法可以将一行替换为新文本。 有什么想法/建议吗? 问题答案: