タブ区切りテキストをファイルから読み込む
ファイルから一行ずつ読み込み、各行においてタブで区切られた2番目の要素を出力するプログラム
import java.io.*;
public class Foo {
public void readFromFile(String filename)
{
try
{
String line = null;
int counter =0;
BufferedReader br = new BufferedReader(
new FileReader(filename));
while ((line = br.readLine()) != null)
{
String[] data = line.split("\t");
/* data is an array of tab-separated strings */
/* Output only the 2nd column */
System.out.println( data[1] );
/* Output the 2nd column as Integer */
System.out.println( Integer.parseInt(data[1]) );
counter++;
}
br.close();
System.out.println("File was " + counter + " lines.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Foo F = new Foo();
F.readFromFile("FILENAME");
}
}