在Java中读取制表符分隔的文件

在Java中读取制表符分隔的文件

本文介绍了在Java中读取制表符分隔的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来读取Java中的制表符分隔文件:

I have the following piece of code to read a tab-separated file in Java:

while ((str = in.readLine()) != null) {
  if (str.trim().length() == 0) {
          continue;
  }

  String[] values = str.split("\\t");

  System.out.println("Printing file content:");
  System.out.println("First field" + values[0] + "Next field" +  values[1]);
}

但它打印1而不是文件内容。这有什么不对?
。从所述样品文件中的一行中读取如下:

But it's printing 1 instead of the file content. What is wrong here?A line from the sample file reads as follow:

{Amy Grant}{/m/0n8vzn2}{...}


推荐答案

这个有效!但我需要单独访问这些字段。你能plesae告诉我什么是我的代码拧?

This works! But I need to access the fields separately. Could you plesae tell me what is wring in my code?

我怀疑您购买的 IndexOutOfBoundsException异常。你得到的错误很重要,如果忽略它,你就不能指望解决问题。

I suspect you are getting an IndexOutOfBoundsException. The error you are getting is important and you can't hope to solve the problem if you ignore it.

这意味着你只有一个字段集。

This would mean you have only one field set.

String[] values = str.split("\\t", -1); // don't truncate empty fields

System.out.println("Printing file content:");
System.out.println("First field" + values[0] +
   (values.length > 1 ? ", Next field" +  values[1] : " there is no second field"));

这篇关于在Java中读取制表符分隔的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:14