1

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

好的。我的程序就像在 .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 有什么办法吗?或者在该文本文件中间编辑特定行的最佳方法是什么?

4

2 回答 2

1

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

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

  2. 使用 readline 逐行读取文件

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

  4. 如果它是正确的位置,那么 lineStart 将保持在您希望插入之前的行之前的位置。使用 seek 将您定位在正确的位置,首先使用 seek(0) 将您定位在文件的开头,然后 seek(lineStart) 获得所需的位置。然后,您使用 writeChars 写入文件。请记住,您必须明确编写换行符。

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

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

于 2014-07-13T14:08:17.713 回答
1

你想要一个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);

于 2013-08-12T19:17:49.390 回答