java 如何从java中读取tsv文件并以表格格式显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26460248/
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 to read tsv file from java and display in table format
提问by Madhavi Talla
I write the like reading tsv file with
我写了类似的阅读 tsv 文件
datarow.splite("\t");
but if the tsv file contain "\t"
it displaying \t
means it is taking \t
as normal text
但如果 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
回答by Jeronimo Backes
Don't try to parse TSV by hand as there are a few corner cases such as escaping/unescaping, not to mention performance/memory issues with large files & lack of flexibility (especially converting values, choosing what columns to read and in what order, etc).
不要尝试手动解析 TSV,因为有一些极端情况,例如转义/非转义,更不用说大文件的性能/内存问题和缺乏灵活性(尤其是转换值、选择要读取的列和顺序) , 等等)。
Try uniVocity-parser's TSV parser. Here's a simple example:
试试uniVocity-parser 的 TSV 解析器。这是一个简单的例子:
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);
Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).
披露:我是这个图书馆的作者。它是开源且免费的(Apache V2.0 许可)。
回答by esprittn
Try this code I hope that helps you
试试这个代码我希望对你有帮助
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
回答by Madhavi Talla
it is working with stringtokenizer
它正在使用 stringtokenizer
while (dataRow != null){
st = new StringTokenizer(dataRow,"\t");
while(st.hasMoreElements()){
dataArray.add(st.nextElement().toString());
}