从Java中的文件读取数据 [英] read data from a file in java
问题描述
我有一个以下格式的文本文件:
I have a text file in the following format:
该文件是.txt文件.我想从该文件中读取课程标题,并打印相应的教科书和教师信息.但是我不确定该遵循什么程序?将信息存储在数组中效率不高!我应该如何进行?注意:我无法更改文件中的信息,因此不应更改!显然,文件将通过以下代码读取:
The file is a .txt file. 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!?
推荐答案
-
首先正确逐行读取文件,然后搜索您输入的课程标题,让我们考虑"Java"
First read the file correctly line by line, and search for your entered course title, lets consider "Java"
现在您点击了标题,并且知道与该标题相关的所有信息都在文件中需要连续3行.
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.
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
}
现在,在这四行上执行字符串操作并根据需要提取并使用数据.
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.
这篇关于从Java中的文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!