简体   繁体   English

读取多个文本文件java

[英]Reading multiple text files java

I'm having trouble on reading multiple text files to fit into one scanner for example I have multiple text files that are named text1.txt , text2.txt etc... I'm trying to make it so that once the user enters which text file number they want it will bring up that data via arrays.我在读取多个文本文件以适合一个扫描仪时遇到问题,例如我有多个名为text1.txttext2.txt等的文本文件......我正在尝试这样做,一旦用户输入哪个他们想要的文本文件号将通过数组调出该数据。

File txt = new File("text.txt");

void readTextFiles() throws IOException {
String line[] = new String[100];
Scanner readTextFiles= new Scanner(txt);

while (readTextFiles.hasNextLine()) {
  line[q] = readTextFiles.nextLine();
  if (line[q].trim() != "") {
    String item[] = line[i].split(" ");
    time[q] = item[0];
    date[q] = item[1];
  }
  q++;

}
readTextFiles.close();
}

my logic works like this but its a code error:我的逻辑是这样工作的,但它是一个代码错误:

File txt= new File("txt" + textFileNumber + ".txt");
int textFileNumber=0;`

If I understood correctly, the error you got is because the initialisation of the local variable does not precede it's use.如果我理解正确的话,你得到的错误是因为局部变量的初始化没有先于它的使用。 You need to declare the textFileNumber before its usage in the string concatenation.您需要在字符串连接中使用之前声明textFileNumber Further you are implementing this functionality as a method.此外,您正在将此功能实现为一种方法。 So why not make the file number a method parameter?那么为什么不把文件号作为方法参数呢?

public void readTextFiles(int fileNumber){
    File txtFile = new File("text" + fileNumber + ".txt");
    //logic
}

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

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