[英]Read CSV file in Java and extracting certain data from the read file
[英]read data from a file in java
我有以下格式的文本文件:
该文件是.txt文件。 我想从该文件中读取课程标题,并打印相应的教科书和教师信息。 但是我不确定该遵循什么程序? 将信息存储在数组中效率不高! 我应该如何进行? 注意:我无法更改文件中的信息,因此不应更改! 显然,该文件将通过以下代码读取:
File newFile=new File("C:/details");
但是我应该如何根据标签的课程名称,课本和讲师从该文件中提取数据!
使用String Tokenizer并分隔每个字符串,然后将它们存储在Linked List或Array List中。 为每个标题(如课程标题,讲师等)设置单独的列表,然后打印它们
使用扫描仪类
Scanner s=new Scanner(new File("C:/Details.txt"));
while(s.hasNext())
{
System.out.println(s.nextLine());
}
如果您想按单词工作,则使用字符串标记器
看这篇文章
//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);
您可以使用FileUtils.readFileToString(new File(“” C:/details.txt“);
现在,您可以根据自己的意愿提取所需的数据
首先正确地逐行阅读文件,然后搜索您输入的课程标题,让我们考虑“ Java”
现在单击标题,您知道文件中需要连续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 }
现在,在这四行上执行字符串操作,并根据需要提取并使用数据。
我在家,所以我不能给你确切的代码。 但是,如果您遵循此步骤,则可以解决您的问题。 让我知道您在执行此操作时是否遇到任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.