【问题标题】:How to replace the first line of a txt document如何替换txt文档的第一行
【发布时间】:2022-01-18 20:14:53
【问题描述】:

我想替换文档Vokabeln.txt的第一行,其中存储了词汇的数量,这样当添加一个词汇时,数量就会增加一个。 谢谢你帮助我。

import java.io.*;
public class Vokabeltrainer
{
    private String file;
    private String line;
    private int anzahlVokabeln;
    private boolean status = true;
    Scanner sc = new Scanner(System.in);
    private ArrayList <Vokabel> Vokabelliste;
    public Vokabeltrainer()
    {
        this.file = "";
        this.Vokabelliste = new ArrayList<Vokabel>();
    }

    public void main() throws IOException
    {
        System.out.println("Vokabeln.txt");
        this.file = ("Vokabeln.txt");
        try (BufferedReader br = new BufferedReader(new FileReader(file)))
        {
            line = br.readLine();
            anzahlVokabeln = Integer.parseInt(line);
            for(int i = 0;i < anzahlVokabeln; i++)
            {
                line = br.readLine();
                this.Vokabelliste.add(new Vokabel(line.split("\\s+")[0],line.split("\\s+")[1]));
            }
        }
        while(status==true)
        {
            System.out.println("Was willst du machen \n-Vokabel hinzufügen\n-Vokabel enfernen\n-Vokabeln Abfragen\n-Programm Quit");
            String line = sc.nextLine();
            if(line.equals("one")||line.equals("Add vocabulary"))
            {
                Vokabelhinzufügen();
            }
            else if(line.equals("two")||line.equals("Remove vocabulary"))
            {

            }
            else if(line.equals("three")||line.equals("Vokabeln Abfragen"))
            {

            }
            else if(line.equals("four")||line.equals("Quit"))
            {
                status = false;
                //Maybe Statistics from the User
            }
            else
            {
                System.out.println("This option doesnt exists.");
            }
        }
    }

    public void Vokabelhinzufügen()
    {
        boolean vokabelhinzustatus = true;
        String Vokabel = "";
        while(vokabelhinzustatus==true)
        {
            System.out.println("Please enter the vocabulary now. (Hallo Hello)");
            Vokabel = sc.nextLine();
            try(PrintWriter output = new PrintWriter(new FileWriter("Vokabeln.txt",true))) 
            {
                output.printf("%s\r\n", Vokabel.toLowerCase());
                String after = String.valueOf(anzahlVokabeln+1);
                String before = String.valueOf(anzahlVokabeln);
//At this point the replace has to be. before is the number before the translation was added and after is after the translation was added.
            } 

            catch (Exception e) {}            
            System.out.println("Vocabulary Successfully Added");
            System.out.println("Exit Add Vocabulary?");
            String line = sc.nextLine();
            if(line.equals("yes"))
            {
                break;
            }
        }
    }

    public void Vokabelentfernen()
    {

    }
}

【问题讨论】:

  • 运行当前代码时会发生什么。
  • 添加了词汇,但下次我读入文件时,程序不会读入新的词汇
  • 什么是“数字”?什么号码?您需要添加更多详细信息。比如,这个文件在你运行程序之前和之后是什么样子的?将您的评论添加为问题的详细信息,并更具体地说明问题。
  • 所以文件改变了?你只读过一次文件,所以我看不出程序会如何读取新的词汇。
  • 目前还没有添加,只是存储词汇

标签: java txt


【解决方案1】:

不是您的问题的答案; 而是一个设计建议。

您似乎有一个存储事物的文件 (也许是沃卡布林) 并且文件中的每个 thing 的结构都相同, 但包含不同的细节。

不要将计数存储在文件中。 反而, 只需将事物存储在文件中并全部阅读。

如果你必须, 在文件末尾添加一个标记,指示“内容结束”。 出于某种原因,您可能希望在文件中存储 things 的计数。 如果是这样的话, 将计数存储为文件结束标记的一部分。

如果你使用这种技术, 你的添加到文件算法变成这样:

  1. 读一行。
  2. 这条线是东西吗?
  3. 如果是,写出来,
  4. 如果否,则解析文件结束标记,增加计数,然后写入文件结束标记。

【讨论】:

  • 等一下,我可以得到一个txt文档的行数吗?
  • 阅读时数数。
猜你喜欢
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2018-09-13
  • 2011-08-31
  • 1970-01-01
  • 2017-02-12
  • 2018-08-05
相关资源
最近更新 更多