【问题标题】:Rereading a small text file in Java [duplicate]用Java重读一个小文本文件[重复]
【发布时间】:2014-01-05 17:25:39
【问题描述】:

由于我对 Java 的经验不足(我在过去两个月的学校里才开始学习它),我遇到了一个问题。

我正在为我的一个朋友制作这个“侮辱机器”程序,作为一个玩笑。该程序引用一个文本文件并查找它有多少行。然后它从 1-最大行号生成一个数字,并将所有文本存储在一个数组中,并打印出该行号中的文本。用户还可以创建侮辱,并使用新的侮辱更新文本文件。

当我现在运行它的侮辱部分时,(我使用的是BufferedReader,也许有更简单的方法?)它的位置仍然在文本文件的底部,我找不到方法再次重新读取文本文件以更新数组。如果我回到侮辱性的部分,它会在我到达文件末尾时打印出 NULL。请问有什么办法解决这个问题吗?

仅供参考,回复创建一个新的BufferedReader,用户可以来回创建和核心程序,所以我需要无限的BufferedReaders

(我确实意识到我的代码可能不是最有效的,我才刚刚开始编程)

import java.util.Scanner;
import java.io.*;
import java.util.Random;
public class InsultingMachine{
    public static void main(String args[]) throws IOException{
        File insult = new File("insults.txt");
        boolean exist = insult.exists();

        if(exist == false){
            System.out.println("The file does not exist, creating the file...?");
            PrintWriter writer = new PrintWriter("insults.txt", "UTF-8");
            writer.close();
            System.out.println("Done");
        }

        //objects
        Scanner input = new Scanner(System.in);
        Scanner inputIn = new Scanner(System.in);
        Scanner inputIn2 = new Scanner(System.in);
        Random ranGen = new Random();
        BufferedReader reader = new BufferedReader(new FileReader("insults.txt"));
        BufferedReader reader2 = new BufferedReader(new FileReader("insults.txt"));
        BufferedWriter wr = new BufferedWriter(new FileWriter("insults.txt", true));

        //vars
        int i, ranInt;
        int lineNum = 0;
        int lineNum2 = 0;
        int counter = 0, counter2 = 0, counter3 = 0, menuCounter = 0;
        String outputting;
        String s1, s2;

        while (menuCounter == 0){
            System.out.println("Hello, please select an option from the menu: ");
            System.out.println("(1) INSULTS");
            System.out.println("(2) Create insults");
            System.out.println("(3) Exit");

            i = input.nextInt();

            switch(i){
                case 1:
                    //Establish Line Number

                    while(reader.readLine() != null){
                        lineNum++;
                    }
                    //store the lines of the text file
                    String L[] = new String[lineNum];

                    lineNum2 = 0;
                    while(lineNum2 < lineNum){
                        L[lineNum2] = reader2.readLine();
                        lineNum2++;
                    }

                    System.out.println("You may start you fool...Enter an insult --type exit to exit-- :");
                    while (counter == 0){
                        s2 = inputIn.nextLine();

                        if(s2.equalsIgnoreCase("exit")){
                            break;
                        }

                        //generate number from 1 to max line number
                        ranInt = ranGen.nextInt(lineNum);
                        if (ranInt == 0)
                        {
                            ++ranInt;
                        }

                        System.out.println(L[ranInt]);
                    }
                    break;

                case 2:
                    while(counter3 == 0){
                        System.out.println("Please enter the insult you would like to add --type exit to exit-- : ");
                        s2 = inputIn2.nextLine();

                        if(s2.equalsIgnoreCase("exit"))
                        {
                            wr.close();
                            break;
                        }
                        wr.newLine();
                        wr.write(s2);
                    }
                    break;

                case 3:
                    menuCounter++;
                    break;
                default:
                    System.out.println("Please enter a correct value...");
                    break;
            }
        }
    }
}

【问题讨论】:

    标签: java file io bufferedreader reset


    【解决方案1】:

    要按行读取文件,请使用Apache Commons IO Library

    apache commons io

    它有 FileUtils 类。 FileUtils

    它有方法readLines(File f)readLines(File f)

    此方法将读取文件并返回行列表,然后您可以使用 List 做任何您想做的事情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-23
      • 2016-03-14
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      相关资源
      最近更新 更多