如何从java读取tsv文件并以表格格式显示

Mad*_*lla 2 java

我写的像读取tsv文件一样

datarow.splite("\t");
Run Code Online (Sandbox Code Playgroud)

但如果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
Run Code Online (Sandbox Code Playgroud)

Jer*_*kes 6

不要试图手动解析TSV,因为有一些极端情况,例如转义/转义,更不用说大文件的性能/内存问题和缺乏灵活性(特别是转换值,选择要读取的列和按什么顺序等).

尝试使用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);
Run Code Online (Sandbox Code Playgroud)

披露:我是这个图书馆的作者.它是开源和免费的(Apache V2.0许可证).