如何在java中读取记事本文件?

发布于 2024-08-18 02:38:33 字数 1431 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

留蓝 2024-08-25 02:38:33

最简单的选择是简单地使用 Apache Commons IO JAR 并导入 org.apache.commons.io .FileUtils 类。使用此类时有很多可能性,但最明显的是如下;

List<String> lines = FileUtils.readLines(new File("untitled.txt"));

就是这么简单。

“不要重新发明轮子。”

我可以问一下您将从该文件中读取哪种内容/数据,因为可能还有其他(甚至更简单)的可能性?

属性

foo="酒吧"

字符串标记

foo、bar、fu、baz

如果您需要我提到的任何流程的更多详细信息,请告诉我。

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;

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.

Properties

foo="bar"

String Tokens

foo,bar,fu,baz

Let me know if you require more details with any of the processes I've mentioned.

孤独岁月 2024-08-25 02:38:33

“记事本”文件只是文本文件,因此您只需使用 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".

别念他 2024-08-25 02:38:33
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();
}
笑饮青盏花 2024-08-25 02:38:33

基本思想是创建一个文件读取器对象

FileReader fr = new FileReader('file.txt');

,然后逐行遍历文件,解析每一行并将内容保存到某些内部数据存储(数组、HashMap)中。

您的示例中的 while 循环就是这样做的。 FileReader 类将为您处理行结尾,并在没有更多行可供读取时返回 null。您需要在 while 循环内执行的操作是解析每一行并将不同的数据位(课程名称、价格等)彼此分开。

编辑: 要解析这些行,您可以执行如下操作。 while 循环内的内容取决于菜单文件的格式。以下内容假设菜单文件包含价格和课程名称(按顺序),每行用逗号分隔。

12.95$,Penne ala Arabiata
8.15$,Fish Soup

请注意,如果这样做,则不能在价格中使用逗号。当然,您可以使用分号代替逗号作为数据字段之间的分隔符。数据字段的数量当然也取决于您。

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
}

在最终代码中,您需要将数据字段保存到某种结构中,而不是仅仅从文件中提取它们。另一件需要注意的事情是,由于美元符号,价格是一个字符串而不是数字。如果您希望对价格进行任何计算,您当然需要使用 parseFloat()parseDouble() 将其转换为数字。

当然,如果您确实使用 csv(逗号分隔值)格式,最好使用 csv 库来为您进行解析,而不是自己编写解析器。

http://opencsv.sourceforge.net/

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.

http://opencsv.sourceforge.net/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文