「java」BufferedReaderを使ってテキストを読むコード
Javaコード:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileUtilOperate {
public void readLinesFromFile(String filename) {
BufferedReader br = null;
try {
// BufferedReaderオブジェクト作成
br = new BufferedReader(new FileReader(filename));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// BufferedReader閉じる
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
FileUtilOperate util = new FileUtilOperate();
util.readLinesFromFile(“startnewsdemo.txt");
}
}