yypp-1119 2008-10-07 09:44
浏览 331
已采纳

java读文件 遇到第一个空行以后才开始显示内容 怎么做啊

一个文件a.txt。内容是
author:wein

[OptionSetting]
option=YES
setting=NO

用Java读这个文件,要从[OptionSetting]开始依次显示。
这个应该怎么设定呢?

  • 写回答

2条回答 默认 最新

  • lggegegmail 2008-10-07 11:32
    关注

    [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()方法, 一次读一行, 蛮好用的.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?