Easiest option is to simply use the Apache Commons IO JAR and import the org.apache.commons.io.FileUtils class. There are many possibilities when using this class, but the most obvious would be as follows;
“记事本”文件只是文本文件,因此您只需使用 Reader 实例读取它即可。由于记事本支持 Windows Unicode,因此您可能需要指定“UTF-16LE”字符集。
"Notepad" files are just text files, so you just read it in with a a Reader instance. Since Notepad supports windows Unicode, you may need to specify a charset of "UTF-16LE".
String filename = "myfile.txt";
BufferedReader reader = new BufferedReader(new FileReader(filename));
try{
String line;
//as long as there are lines in the file, print them
while((line = reader.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String filename = "myfile.txt";
BufferedReader reader = new BufferedReader(new FileReader(filename));
try{
String line;
//as long as there are lines in the file, print them
while((line = reader.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String line = "";
// read lines from file
while ((line = fr.readLine()) != null) {
// parse each line
tokens = line.split(",");
String price = tokens[0];
String courseName = tokens[1];
// extract all other information
}
The basic idea is that you create a file reader object
FileReader fr = new FileReader('file.txt');
and then go over the file line by line parsing each line and saving the stuff to some internal data storage (Array, HashMap).
The while loop in the example you have does just this. The FileReader class will take care of the line ending for you, and will return null when there's no more lines to be read. What you need to do inside the while loop is to parse each line and separate the different bits of data (course name, price etc.) from each other.
EDIT: To parse the lines you would do something like the following. What is inside the while loop depends on how you format the menu files. The following works on the assumption that the menu files contains the price and the name of the course (in that order) separated by a comma on each line.
12.95$,Penne ala Arabiata
8.15$,Fish Soup
Notice that you can't use a comma in the price if you do this. You can of course use a semicolon as the separator between the data fields instead of a comma. The number of data fields is of course also up to you.
String line = "";
// read lines from file
while ((line = fr.readLine()) != null) {
// parse each line
tokens = line.split(",");
String price = tokens[0];
String courseName = tokens[1];
// extract all other information
}
In your final code you'll want to save the data fields into some structure instead of just extracting them from the file. Another thing to note is that the price is a String NOT a number because of the dollar sign. Should you wish to do any calculations with the prices you'll of course need the convert it to a number with parseFloat() or parseDouble().
And of course if you do use the csv (comma separated values) format, it's better to go for a csv library to do the parsing for you instead of writing the parser yourself.
发布评论
评论(6)
查看 Sun 的 Java 教程
Have a look at Sun's Java Tutorial
最简单的选择是简单地使用 Apache Commons IO JAR 并导入 org.apache.commons.io .FileUtils 类。使用此类时有很多可能性,但最明显的是如下;
就是这么简单。
我可以问一下您将从该文件中读取哪种内容/数据,因为可能还有其他(甚至更简单)的可能性?
即
属性
字符串标记
如果您需要我提到的任何流程的更多详细信息,请告诉我。
Easiest option is to simply use the Apache Commons IO JAR and import the org.apache.commons.io.FileUtils class. There are many possibilities when using this class, but the most obvious would be as follows;
It's that easy.
Can I ask what sort of content/data you will be reading from this file as there may be other (even simpler) possibilities?
i.e.
Properties
String Tokens
Let me know if you require more details with any of the processes I've mentioned.
http://java.lang. sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()
“记事本”文件只是文本文件,因此您只需使用 Reader 实例读取它即可。由于记事本支持 Windows Unicode,因此您可能需要指定“UTF-16LE”字符集。
"Notepad" files are just text files, so you just read it in with a a Reader instance. Since Notepad supports windows Unicode, you may need to specify a charset of "UTF-16LE".
基本思想是创建一个文件读取器对象
,然后逐行遍历文件,解析每一行并将内容保存到某些内部数据存储(数组、HashMap)中。
您的示例中的 while 循环就是这样做的。
FileReader
类将为您处理行结尾,并在没有更多行可供读取时返回null
。您需要在 while 循环内执行的操作是解析每一行并将不同的数据位(课程名称、价格等)彼此分开。编辑: 要解析这些行,您可以执行如下操作。 while 循环内的内容取决于菜单文件的格式。以下内容假设菜单文件包含价格和课程名称(按顺序),每行用逗号分隔。
请注意,如果这样做,则不能在价格中使用逗号。当然,您可以使用分号代替逗号作为数据字段之间的分隔符。数据字段的数量当然也取决于您。
在最终代码中,您需要将数据字段保存到某种结构中,而不是仅仅从文件中提取它们。另一件需要注意的事情是,由于美元符号,价格是一个字符串而不是数字。如果您希望对价格进行任何计算,您当然需要使用
parseFloat()
或parseDouble()
将其转换为数字。当然,如果您确实使用 csv(逗号分隔值)格式,最好使用 csv 库来为您进行解析,而不是自己编写解析器。
http://opencsv.sourceforge.net/
The basic idea is that you create a file reader object
and then go over the file line by line parsing each line and saving the stuff to some internal data storage (Array, HashMap).
The while loop in the example you have does just this. The
FileReader
class will take care of the line ending for you, and will returnnull
when there's no more lines to be read. What you need to do inside the while loop is to parse each line and separate the different bits of data (course name, price etc.) from each other.EDIT: To parse the lines you would do something like the following. What is inside the while loop depends on how you format the menu files. The following works on the assumption that the menu files contains the price and the name of the course (in that order) separated by a comma on each line.
Notice that you can't use a comma in the price if you do this. You can of course use a semicolon as the separator between the data fields instead of a comma. The number of data fields is of course also up to you.
In your final code you'll want to save the data fields into some structure instead of just extracting them from the file. Another thing to note is that the price is a String NOT a number because of the dollar sign. Should you wish to do any calculations with the prices you'll of course need the convert it to a number with
parseFloat()
orparseDouble()
.And of course if you do use the csv (comma separated values) format, it's better to go for a csv library to do the parsing for you instead of writing the parser yourself.
http://opencsv.sourceforge.net/