简体   繁体   English

如何从文本文件中取出特定行并将其读入数组? (爪哇)

[英]How to take a specific line from a text file and read it into an array? (Java)

I've been trying to code a quiz game in javafx where I store the questions on a text file and then randomize a number then use it to call the line of the same number on the text file and read it into an array.我一直在尝试在 javafx 中编写一个问答游戏,我将问题存储在一个文本文件中,然后随机化一个数字,然后使用它来调用文本文件中相同数字的行并将其读入数组。

After looking online I can only seem to find how to read a text file line by line instead of a specific line.在线查看后,我似乎只能找到如何逐行读取文本文件而不是特定行。 I also use the following code to read the text file but am unsure where to go on from there.我还使用以下代码来读取文本文件,但不确定从那里继续。

File file = new File("/Users/administrator/Desktop/Short Questions.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;

This may help you这可能会帮助你

You need to change file path as per your file location您需要根据文件位置更改文件路径

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test {

public static void main(String[] args) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\everestek22\\Desktop\\Invoice.txt"));
    String[] strArray = 
    bufferedReader.lines().map(String::new).toArray(String[]::new);
    //        String line = bufferedReader.readLine();
    //        while (line != null) {
    //            System.out.println(line);
    //            line = bufferedReader.readLine();
    //            String[] strArray = bufferedReader.lines().map(String::new).toArray(String[]::new);
    //        }
    bufferedReader.close();

    for (String s : strArray) {
        System.out.println(s);
    }

}
}

Don't bother trying to read specific lines from the file, just read all the lines from the file , then lookup your question by index in the resultant list.不要费心尝试从文件中读取特定行,只需读取文件中的所有行,然后在结果列表中按索引查找您的问题。

List<String> questions = Files.readAllLines(
    Paths.get("<your file path>")
);

Then you could choose a question at random:然后你可以随机选择一个问题:

Random random = new Random(42);
int randomQuestionIndex = random.nextInt(questions.size());
String randomQuestion = questions.get(randomQuestionIndex);

Using 42 as the seed to the random number generator makes the random sequence repeatable, which is good for testing.使用42作为随机数生成器的种子使随机序列具有可重复性,这有利于测试。 To have it truly psuedo-random, then remove the seed (eg just new Random() );为了让它真正伪随机,然后删除种子(例如只是new Random() );

If the structure of the data you wish to read is complex, then use a helper library such as Jackson to store and retrieve the data as serialized JSON objects.如果您希望读取的数据结构复杂,则使用帮助程序库(例如Jackson)将数据作为序列化 JSON 对象存储和检索。 If it is even more complex, then a database can be used.如果它更复杂,那么可以使用数据库。

If you have a really large file and you know the position in the file of each specific thing you wish to read, then you can use a random access file for lookup.如果您有一个非常大的文件,并且您知道要读取的每个特定内容在文件中的位置,那么您可以使用 随机访问文件进行查找。 For example, if the all the questions in the file are exactly the same length and you know how many questions are stored there, then a random access file might be used fairly easily.例如,如果文件中的所有问题的长度完全相同,并且您知道其中存储了多少问题,则可以很容易地使用随机访问文件。 But, from your description of what you need to do, this is likely not the case, and the simpler solution of reading everything rather than using a random access file is better.但是,根据您对需要做什么的描述,情况可能并非如此,读取所有内容而不是使用随机访问文件的更简单的解决方案更好。

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

相关问题 如何从Java中的文本文件读取特定行? - How to read a specific line from a text file in Java? 如何在Java的文本文件中读取行的特定部分? - How To Read A Specific Part Of A Line In A Text File In Java? 如何直接读取大文本文件中的特定数据行而不搜索C或java中的每一行 - How to Dirctly read Specific lines of data from the large text file without search every line in C , or java 如何使用Java从文本文件中逐行读取和删除 - how read and delete line by line from a text file using java 如何从Java中的文本文件获取数组大小和元素的输入? - How to take array size and elements' inputs from a text file in java? 如何在Java中逐行读取文本文件并排除某些以特定字符串开头的行 - How to read text file line by line in Java and exclude some line which start with specific string 从txt文件java中读取特定行 - read specific line from txt file java Java:如何从文本文件的每一行中提取第一个单词并将其写入新的文本文件? - Java: How to take the first word in each line from a text file and write it to a new text file? 如何从文本文件Java中读取单个单词(或行)? - How to read a single word (or line) from a text file Java? 如何从Java中的X行读取文本文件? - How to read a text file from X line in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM