我用
编写类似的阅读tsv文件datarow.splite("\t");
但如果tsv文件包含"\t"
,则显示\t
表示正在将\t
作为普通文本
public class Tsv_read{
public static void main(String[] arg) throws Exception {
BufferedReader TSVFile =
new BufferedReader(new FileReader("users.tsv"));
String dataRow = TSVFile.readLine(); // Read first line.
while (dataRow != null){
String[] dataArray = dataRow.split("\t");
for (String item:dataArray) {
System.out.print(item + " ");
}
System.out.println(); // Print the data line.
dataRow = TSVFile.readLine(); // Read next line of data.
}
// Close the file once all data has been read.
TSVFile.close();
// End the printout with a blank line.
System.out.println();
} //main()
} // TSVRead
答案 0 :(得分:5)
不要尝试手动解析TSV,因为有一些极端情况,例如转义/转义,更不用说大文件的性能/内存问题&缺乏灵活性(特别是转换值,选择要读取的列和按什么顺序等)。
试试uniVocity-parser's TSV parser。这是一个简单的例子:
TsvParserSettings settings = new TsvParserSettings(); //you will find MANY options here
TsvParser parser = new TsvParser(settings);
// parses all rows in one go.
List<String[]> allRows = parser.parseAll(YOUR_INPUT_HERE);
披露:我是这个图书馆的作者。它是开源和免费的(Apache V2.0许可证)。
答案 1 :(得分:3)
试试这段代码我希望能帮到你
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Tsv_read{
public static void main(String[] arg) throws Exception {
StringTokenizer st ;
BufferedReader TSVFile = new BufferedReader(new FileReader("users.tsv"));
String dataRow = TSVFile.readLine(); // Read first line.
while (dataRow != null){
st = new StringTokenizer(dataRow,"\t");
List<String>dataArray = new ArrayList<String>() ;
while(st.hasMoreElements()){
dataArray.add(st.nextElement().toString());
}
for (String item:dataArray) {
System.out.print(item + " ");
}
System.out.println(); // Print the data line.
dataRow = TSVFile.readLine(); // Read next line of data.
}
// Close the file once all data has been read.
TSVFile.close();
// End the printout with a blank line.
System.out.println();
} //main()
} // TSVRead
答案 2 :(得分:0)
它正在使用stringtokenizer
while (dataRow != null){
st = new StringTokenizer(dataRow,"\\t");
while(st.hasMoreElements()){
dataArray.add(st.nextElement().toString());
}