java 我如何在java中读取记事本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2057048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i read a notepad file in java?
提问by OVERTONE
im trying to create a restaurant system that will create food items and such from a menu.
我正在尝试创建一个餐厅系统,该系统将从菜单中创建食品等。
ill be learning jdbc soon and im sure that would help but for now i think the simplest way is too create my menu in notepad.
我很快就会学习 jdbc,我相信这会有所帮助,但现在我认为最简单的方法是在记事本中创建我的菜单。
whats the best way to line up and read from a notepad file like a menu.
什么是排列和读取记事本文件(如菜单)的最佳方式。
please try speak clearly, im not exactly sure of all terminologies.
请尽量说清楚,我不完全确定所有术语。
this one looks promising but ive no idea whats goin on.
这个看起来很有希望,但我不知道发生了什么。
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////// /////////////////////
im still stuck with this.
我仍然坚持这一点。
ive decided too make a seperate mthod for reading the file.
我也决定采用单独的方法来读取文件。
ive tried every example i can think of. could someone just show me an example of how too define a files classpath.
我尝试了我能想到的每一个例子。有人可以向我展示一个如何定义文件类路径的示例。
if i type menu.txt it just doesnt work.
如果我输入 menu.txt 它就不起作用。
回答by r3zn1k
Have a look at Sun's Java Tutorial
回答by Alasdair
Easiest option is to simply use the Apache Commons IOJAR 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;
最简单的选择是简单地使用Apache Commons IOJAR 并导入 org.apache.commons.io.FileUtils 类。使用这个类有很多可能性,但最明显的是如下;
List<String> lines = FileUtils.readLines(new File("untitled.txt"));
It's that easy.
就这么简单。
"Don't reinvent the wheel."
“不要重新发明轮子。”
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.
IE
Properties
特性
foo="bar"
富=“酒吧”
String Tokens
字符串令牌
foo,bar,fu,baz
foo,bar,fu,baz
Let me know if you require more details with any of the processes I've mentioned.
如果您需要有关我提到的任何流程的更多详细信息,请告诉我。
回答by Viktor Klang
回答by President James K. Polk
"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".
“记事本”文件只是文本文件,因此您只需使用 Reader 实例读取它。由于记事本支持 windows Unicode,您可能需要指定字符集“UTF-16LE”。
回答by Matti Lyra
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).
然后逐行浏览文件,解析每一行并将内容保存到一些内部数据存储(数组,HashMap)。
The while loop in the example you have does just this. The FileReaderclass will take care of the line ending for you, and will return nullwhen 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.
您示例中的 while 循环就是这样做的。该FileReader课程将为您处理行尾,并null在没有更多行可供阅读时返回。您需要在 while 循环中做的是解析每一行并将不同的数据位(课程名称、价格等)彼此分开。
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.
编辑:要解析行,您将执行以下操作。while 循环中的内容取决于您如何格式化菜单文件。以下假设菜单文件包含价格和课程名称(按该顺序),每行以逗号分隔。
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().
在您的最终代码中,您需要将数据字段保存到某种结构中,而不是仅仅从文件中提取它们。另一件要注意的事情是,由于美元符号,价格是一个字符串而不是一个数字。如果您希望对价格进行任何计算,您当然需要使用parseFloat()或将其转换为数字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.
当然,如果您确实使用 csv(逗号分隔值)格式,最好使用 csv 库为您进行解析,而不是自己编写解析器。
回答by Fortega
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();
}

