Personal tools
You are here: Home documents lectures Java Sample Programs タブ区切りテキストをファイルから読み込む
Document Actions

タブ区切りテキストをファイルから読み込む

by member last modified 2007-03-19 18:47
ファイルから一行ずつ読み込み、各行においてタブで区切られた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");
}
}

Powered by Plone, the Open Source Content Management System

This site conforms to the following standards: