reading tab delimited textfile java(读取制表符分隔的文本文件 java)
问题描述
10
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
ddd ddd ddd
我有一个文本文件,我试图用制表符分隔符读取.每当我读取文件时,我在 10 之后得到一个 arrayindexoutofbound 错误.我在网上搜索,发现我必须在 后面添加一个 -1,但我仍然得到同样的错误.
I have a textfile that Im trying to read with tab delimiters. whenever i read the file, i get an arrayindexoutofbound error after the 10. i search online and found that i have to add a -1 behind the but i still get the same error.
try{
Scanner scan = new Scanner(new File("1.txt"));
String line="";
int readline = Integer.parseInt(scan.nextLine());//
while (scan.hasNextLine())
{
line = scan.nextLine();
if(line.equals("ccc"))
{
break;
}
String[] split=line.split(" ");
array.add(split);
}
推荐答案
如果你使用Scanner
这里不需要split
,你可以使用next()
这里如下
If you are use Scanner
here no need to split
, you can use next()
here as follows
Scanner sc=new Scanner(new FileReader("D:\test.txt"));
while (sc.hasNextLine()){
System.out.println(sc.next());
}
这篇关于读取制表符分隔的文本文件 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:读取制表符分隔的文本文件 java


基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01