简体   繁体   English

从Java中的文件读取数据

[英]read data from a file in java

I have a text file in the following format: 我有以下格式的文本文件:

Details.txt Details.txt

The file is a .txt file. 该文件是.txt文件。 I want to read course title from this file and print corresponding textbook and instructor information. 我想从该文件中读取课程标题,并打印相应的教科书和教师信息。 But i am not sure about what process to follow ? 但是我不确定该遵循什么程序? storing the information in an array won't be efficient! 将信息存储在数组中效率不高! How should i proceed ? 我应该如何进行? NOTE: I can't change the info in the file, it should not be changed!! 注意:我无法更改文件中的信息,因此不应更改! obviously the file will be read by the following code: 显然,该文件将通过以下代码读取:

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

but how should i extract the data from this file according to the labels course title, textbook and instructor!? 但是我应该如何根据标签的课程名称,课本和讲师从该文件中提取数据!

Use String Tokenizer and separate each string and then store them in a Linked List or Array List. 使用String Tokenizer并分隔每个字符串,然后将它们存储在Linked List或Array List中。 Have Separate List for each title like course title, instructor etc. and then print them 为每个标题(如课程标题,讲师等)设置单独的列表,然后打印它们

Use Scanner class 使用扫描仪类

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

if you want in work by word then use String Tokenizer 如果您想按单词工作,则使用字符串标记器

see this article 看这篇文章

//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);

you can use FileUtils.readFileToString(new File(""C:/details.txt"); 您可以使用FileUtils.readFileToString(new File(“” C:/details.txt“);

Now you can extract the required data based on your wish 现在,您可以根据自己的意愿提取所需的数据

  1. First read the file correctly line by line, and search for your entered course title, lets consider "Java" 首先正确地逐行阅读文件,然后搜索您输入的课程标题,让我们考虑“ Java”

  2. Now you hit your title and you know you need 3 consecutive lines from your file as all information related to that title are there. 现在单击标题,您知道文件中需要连续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. Now on those four lines do string operations and extract your data as you need and use it. 现在,在这四行上执行字符串操作,并根据需要提取并使用数据。

I am home so I can't give you exact code. 我在家,所以我不能给你确切的代码。 But if you follow this it will solve your issue. 但是,如果您遵循此步骤,则可以解决您的问题。 Let me know if any problem you got while doing this. 让我知道您在执行此操作时是否遇到任何问题。

Follow this to get some info on String operations 按照此操作获取有关字符串操作的一些信息

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

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