This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.StringTokenizer; | |
public class ReadCSV { | |
public static void main(String[] args) { | |
try { | |
File csv = new File("c:\\mdb\\zzz.txt"); // CSVデータファイル | |
BufferedReader br = new BufferedReader(new FileReader(csv)); | |
// 最終行まで読み込む | |
String line = ""; | |
while ((line = br.readLine()) != null) { | |
// 1行をデータの要素に分割 | |
StringTokenizer st = new StringTokenizer(line, ","); | |
while (st.hasMoreTokens()) { | |
// 1行の各要素をタブ区切りで表示 | |
System.out.print(st.nextToken() + "\t"); | |
} | |
System.out.println(); | |
} | |
br.close(); | |
} catch (FileNotFoundException e) { | |
// Fileオブジェクト生成時の例外捕捉 | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// BufferedReaderオブジェクトのクローズ時の例外捕捉 | |
e.printStackTrace(); | |
} | |
} | |
} |
以下のサイトを参考にさせていただきました。
http://www.atmarkit.co.jp/fjava/javatips/063java003.html
0 件のコメント:
コメントを投稿