0

我有以下格式的文本文件:

详情.txt

该文件是一个 .txt 文件。我想从此文件中读取课程标题并打印相应的教科书和讲师信息。但我不确定要遵循什么流程?将信息存储在数组中效率不高!我应该如何进行?注意:我不能更改文件中的信息,不应该更改!!显然该文件将被以下代码读取:

File newFile=new File("C:/details");

但是我应该如何根据标签课程标题、教科书和讲师从这个文件中提取数据!?

4

5 回答 5

0

使用 String Tokenizer 并分离每个字符串,然后将它们存储在 Linked List 或 Array List 中。为每个标题(如课程标题、讲师等)设置单独的列表,然后打印它们

于 2012-12-15T04:15:18.070 回答
0

使用 Scanner 类

Scanner s=new Scanner(new File("C:/Details.txt"));
while(s.hasNext())
{
  System.out.println(s.nextLine());
}

如果您想按单词工作,请使用 String Tokenizer

看这篇文章

于 2012-12-15T04:26:21.257 回答
0
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text
tv.setText(text);
于 2012-12-15T04:48:33.027 回答
0

你可以使用 FileUtils.readFileToString(new File(""C:/details.txt");

现在您可以根据自己的意愿提取所需的数据

于 2012-12-15T04:53:16.687 回答
0
  1. 首先逐行正确读取文件,然后搜索您输入的课程名称,让我们考虑“Java”

  2. 现在你点击了你的标题,你知道你需要文件中的 3 行连续的行,因为与该标题相关的所有信息都在那里。

    if(str.startsWith(title)); {  // for title = "Java"
      line1 = 1st line  // contains ISBN and First Name
      line2 = 2nd line  // Title and Last Name
      line3 = 3rd line  // Author and Department
      line4 = 4th line  // Email
      break;  // this will take you out of while loop
    }
    
  3. 现在在这四行上进行字符串操作并根据需要提取数据并使用它。

我在家,所以我不能给你确切的代码。但是,如果您遵循此操作,它将解决您的问题。如果您在执行此操作时遇到任何问题,请告诉我。

按照此获取有关字符串操作的一些信息

于 2012-12-16T18:28:39.627 回答