【问题标题】:Trouble reading from text file with IDs on each line从每行带有 ID 的文本文件中读取时出现问题
【发布时间】:2018-04-06 16:25:17
【问题描述】:

我想读取一个名为“q.txt”的文本文件,并根据 ID(主键)选择与其关联的字符串。 例如0014,<Random string here>

到目前为止,我已经创建了一个获取#### ID 的方法。现在我需要扫描文件并找到相应的字符串并将其输出到文本视图。

我遇到的问题是 FileNotFoundException 和 IOException。那么我应该把文本文件放在哪里,这样我就不需要添加完整的路径(例如 D:\Projects\...\q.txt),而只需添加“q.txt”?

我如何遍历文件以便随机选择一个问题?

如果使用数组,我还需要它是动态的。

【问题讨论】:

  • 向我们展示您在代码中尝试过的内容。您可以将该文件放在与您的主 .java 文件相同的目录中。
  • 除了 randomID() 方法,我没有其他代码。我尝试了多种从文件中读取和输出行的方法,但都尝试过但失败了,所以我删除了这些行。
  • 向我们展示您的所有代码,包括您的以前的尝试。如果您不尝试并为我们提供出发点,我们将无能为力。
  • 听起来像是某种家庭作业帮助。编辑您的问题并粘贴带有特定错误的代码。否则我认为你不会得到合格的答案
  • 你有几个问题,我建议你以后分开问。

标签: java file text


【解决方案1】:

鉴于您的文件内容如下:

id,question

0001,Some Question?
....
0014,Foo Bar?
...
9999,Last question?

然后您必须将文件的每一行解析为两个单独的字段。一个用于存储 id,另一个用于存储问题。您已经通过 randomID() 方法获得了随机 ID,因此在您的 onClick() 方法中,调用 randomID() 以获取随机 ID 并将其传递给您的 loadDatabase() 方法以在文件中搜索该随机 ID。 请注意,随机 id 必须存在于您的文件中

找到随机 id 后,将问题设置为 TextView

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onClick(View view) {
    String id = randomID();
    loadDataBase(id);
}

public String randomID() {//generates an ID for the next question ####
    String id;
    int questionID;
    int challenge;
    Random ran = new Random();

    int randomChallenge = ran.nextInt(5);

    if (randomChallenge == 0) {
        challenge = 2;
        questionID = ran.nextInt(12);
    } else {
        challenge = 0;
        questionID = ran.nextInt(103);
    }
    id = Integer.toString(challenge) + String.format("%03d", questionID);

    return id;
}

public void loadDatabase(String id) {
    BufferedReader br;

    try {
        br = new BufferedReader(new FileReader("q.txt"));

        while ((br.readLine()) != null) {
            String lineParsed = br.readLine().split(',');
            if(lineParsed[0] == id) {
                someTextView.setText(lineParsed[1]);
            }
        }
        br.close();


    } catch (FileNotFoundException e) {
        System.err.println("File not found: " e.getMessage());
    } catch (IOException e) {
        System.err.println("IOException: " e.getMessage());
    }

}

【讨论】:

    【解决方案2】:

    试试这个: 1.对于文件的路径。

    String basePath = new File("").getAbsolutePath();//this will get the path of the class of your program.
    try{
    BufferedReader strReader = new BufferedReader(new FileReader(basePath+"\\nameOfYourFile.extension"));//this will find your file
    }catch(FileNotFoundException | IOException e){
    //e
    }
    

    2。如果您想随机选择一行:

    String str=null;
    int rng=(int)(Math.random()*NumberOfTheLinesInYourFile);//generate a random number
    for(int i =0; i<rng;i++){
    str=strReader.readLine();//pick the line at rng variable;
    }
    System.out.println(srt);
    

    输出: 文件的1个随机行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多