【问题标题】:Java add line to specific part of text fileJava将行添加到文本文件的特定部分
【发布时间】:2013-08-12 19:13:04
【问题描述】:

下面的评论正在回答另一个问题,这是我提出新问题的唯一方法......

好的。我的程序就像在 .txt 文件上写入信息。目前它正在将信息写入文本文件的末尾,如下所示:

t/1/15/12
o/1/12/3
o/2/15/8
... (lots of lines like this.. all with different numbers)
o/1/16/4

然后..当我使用添加行时:

BufferedWriter fw = new BufferedWriter(new FileWriter(new File("C://Users/Mini/Desktop/Eclipse/Japda/map/" +Numbers.map +".txt"), true));
            fw.newLine();
            fw.write(RightPanel.mode.charAt(0) +"/" +ID +"/" +Numbers.middlex +"/" +Numbers.middley);
            fw.close();

它将我想要但当前添加到文本文件末尾的行..但是我希望它将该行写入文本文件的特定部分..我已经知道我的行号想写..(根据其他行计算..) :D 有什么办法吗?或者在该文本文件中间编辑特定行的最佳方法是什么?

【问题讨论】:

  • 我刚刚看到你把代码放在这里了。快速浏览一下,但您不能将“==”与字符串一起使用。你必须这样做: if (answer.equals(rightanswer)){
  • 另外,你的while语句必须使用我写的方法。
  • 刚刚编辑了代码,仍然多次问同样的问题.. :S
  • 完全改变了问题xDD因为我不能再问新问题了.. :(

标签: java windows loops random helper


【解决方案1】:

要达到您的要求,您需要使用 RandomAccessFile。脚步: 首先创建一个 RandomAccessFile 然后:

  1. 创建一个名为 lineStart 的变量,该变量最初设置为 0 (文件开头)

  2. 使用readline逐行读取文件

  3. 检查它是否是您希望在前面插入的所需行

  4. 如果位置正确,则 lineStart 将保持该位置 就在您希望插入的行之前。使用寻求 最初使用 seek(0) 将您定位在正确的位置 将你定位在文件的开头,然后 seek(lineStart) 得到 所需的位置。然后,您使用 writeChars 写入文件。 请记住,您必须明确编写换行符。

  5. 如果它不是您希望插入的位置,则调用 getFilePointer,然后 在 lineStart 中存储值

重复步骤 2-5,直到您到达所需的插入位置

【讨论】:

    【解决方案2】:

    你想要一个 do-while 循环

    do {
         //code
    } while (expression);
    

    来源:

    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

    你可能想要这样的东西:

    int[] done = new int[100];
    int randomquestion;
    do{
        randomquestion = (int)(Math.random() * 83 + 1);
        if(done[randomquestion] != 1)
        {
            //ask random question
            //if answer is correct, set done[randomquestion] = 1
            //else just let do-while loop run
        }
        //check if all questions are answered
    } while (!areAllQuestionsComplete(done));
    

    这里是方法areAllQuestionsComplete(int[]):

    private boolean areAllQuestionsComplete(int[] list)
    {
        for(int i = 0; i<list.length; i++)
        {
            if(list[i] != 1)
            {
                return false;//found one false, then all false
            }
        }
        return true;//if it makes it here, then you know its all done
    }
    

    查看您的最新代码:

    for(int i = 0; i<done.length; i++)
    {
        done[i] = 0;//need default values else wise itll just be NULL!!!
    }
    
    do{
        ran = (int)(Math.random() * 83 + 1);
        //before entering the do-while loop, you must set default values in the entire done[] array
        if(done[ran] != 1)
        {
            //ask random question
            //if answer is correct, set done[ran] = 1
            //else just let do-while loop run
    
            if (ran == 1) { //1
            question = "kala";
            rightanswer = "fish";}
            if (ran == 2) { //2
            question = "peruna";
            rightanswer = "potato";}
            if (ran == 3) { //3
            question = "salaatti";
            rightanswer = "cabbage";}
            if (ran == 4) { //4
            question = "kalkkuna";
            rightanswer = "turkey";}
            if (ran == 5) { //5
            question = "kia";
            rightanswer = "tikku";}
    
            //YOU MUST HAVE EVERY CONDITION COVERED
            //say your random number makes the number 10
            //you dont set question to  anything at all (hence getting null!)           
    
            System.out.println(question);
            System.out.print("Vastaus?: ");
            answer = in.readLine();
            //if (answer == rightanswer){
            //must use .equals with Strings...not ==
            if (answer.equals(rightanswer)){            
            right++;
            done[ran] = 1;}
            else{wrong++;}
            }
            //check if all questions are answered
    } while (!areAllQuestionsComplete(done));//use the method I wrote!
    

    编辑:

    您必须将默认值放入数组中。创建数组时,默认值为 null。

    int[] done = new int[100];//create array but everything is null
    for(int i = 0; i<done.length; i++)
    {
        done[i] = 0;//need default values else wise it'll just be NULL!!!
    }
    //must be done before the do-while loop starts
    

    最后,确保您的随机数生成器选择正确的数字范围。如果你有一个大小为 100 的数组,那么它的索引将是 0-99。这意味着没有完成[100]。它从完成[0] 到完成[99]。

    如果你已经将 done[] 的大小设为 5,那么它的范围将从 done[0] 到 done[4]。这意味着你应该像这样随机生成:

    随机问题 = (int)(Math.random() * 5);

    【讨论】:

    • 也可以拥有} while(!allQuestionComplete(done)); 并为自己节省几行代码。
    • 随机问题 = (int)(Math.random() * 83 + 1);}
    • @minisurma 好收获!谢谢!
    • 我将整个代码添加到我的问题中。仍然遇到我一开始遇到的同样问题 + 正确和错误的答案不算数。
    • 哦!现在我明白了,泰! :D 所以我首先为每一个 done[] 赋予价值! :o 谢谢!你是伟大的老师! ^^
    猜你喜欢
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多