繁体   English   中英

Java将行添加到文本文件的特定部分

[英]Java add line to specific part of text file

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

好的。 我的程序就像在.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有什么方法可以做到这一点? 还是在该文本文件中间编辑一个特定行的最佳方法是什么?

您想要一个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]。 这意味着您应该像这样随机生成:

randomquestion =(int)(Math.random()* 5);

为了实现您所需要的,您将需要使用RandomAccessFile。 步骤:首先创建一个RandomAccessFile,然后:

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

  2. 使用readline逐行读取文件

  3. 检查是否是您希望插入的必需行

  4. 如果位置正确,则lineStart将保留您要插入的行之前的位置。 首先使用seek(0)将您定位在文件的开头,然后使用seek(lineStart)获得所需的位置,从而使用seek将您定位在正确的位置。 然后,您可以使用writeChars写入文件。 请记住,您必须显式编写换行符。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM