一个文件a.txt。内容是
author:wein
[OptionSetting]
option=YES
setting=NO
用Java读这个文件,要从[OptionSetting]开始依次显示。
这个应该怎么设定呢?
[code="java"]package cn.iwoo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class FileReaderTest {
// /**
// * 开始读取内容标识
// */
// public static final String BEGIN_TAG = "[OptionSetting]";
public static void main(String[] args) throws Exception {
FileReaderTest test = new FileReaderTest();
BufferedReader br = new BufferedReader(new InputStreamReader(test.getFile("File.txt")));
boolean hasBegin = false; // 是否开始读取内容
while (br.ready()) {
String line = br.readLine();
if (hasBegin) {
System.out.println(line);
}
// if (line.trim().equals(BEGIN_TAG)) {
// hasBegin = true;
// }
if (line.trim().equals("")) {
hasBegin = true;
}
}
}
public InputStream getFile(String fileName) throws Exception {
return FileReaderTest.class.getResourceAsStream(fileName);
}
}[/code]
对于文件读取, 我一贯用BufferedReader.readLine()方法, 一次读一行, 蛮好用的.
你可以一个一个字符的读取,然后再看读到的是不是“\n”就可以了啊。