java - 在 Java 中读取格式化文本
问题描述
我正在尝试用 Java 读取格式化文件,我曾经在 C 中做得很好,但这里没有线索。示例行是:
一个“0”公元前
我想将 A 和 0 作为两个单独的字符串,将 [B, C] 作为字符串 ArrayList 中的两个字符串。
无论如何都可以修改行格式,例如添加逗号
A'0' B、C、D...
关于如何拆分这个的任何想法?在 C 中工作时,我曾经使用 fseek、fread 等来完成它
解决方案
请尝试以下代码: 这里的想法是使用 Java“扫描仪”类。这将逐行读取文件,直到到达文件末尾。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class fileReader {
public static void main(String[] args) {
try {
File oFile = new File("myfile.txt");
Scanner oScanner= new Scanner(oFile );
while (oScanner.hasNextLine()) {
String sLine = oScanner.nextLine(); //Next line will point to the next line on the file
System.out.println(sLine ); //And any other operations on the line you would like to perform.
}
oScanner.close();
} catch (FileNotFoundException e) {
System.out.println("Error Occurred");
e.printStackTrace();
}
}
}
推荐阅读
- html - 如果我更改属性,jquery 变量不会更新
- list - 在 Haskell 中获取非空列表或 `[[]]`(默认值)的优雅方式
- node.js - 在 Firebase 云功能中使用 MQTT 客户端
- python - 循环遍历 csv 文件时变量不更新
- batch-file - 如何重命名以“图像”开头的多个文件?
- javascript - 为什么异步函数会在请求正文中传递一个空对象?
- excel - 将日期格式更改为“dd/mm/yyyy”到 Excel 工作簿中的所有日期列(在同一列中)
- next.js - 为什么 Next.js 在 app.js 前加上下划线?
- node.js - 无法为 mongoDB 设置路径
- reactjs - 如何在反应中用 axios 开玩笑地测试异步操作?- 第2部分