集册 Java实例教程 从文件中获取以开头的行

从文件中获取以开头的行

欢马劈雪     最近更新时间:2020-01-02 10:19:05

507
从文件获取行开头

// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.BufferedReader;
/** from 
N o w  J a v a  .   c o m**/

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import org.apache.log4j.Logger;


public class Main{

    public static void main(String[] argv) throws Exception{

        File file = new File("Main.java");

        String value = "nowjava.com";

        System.out.println(getLineBeginningWith(file,value));

    }

    private static Logger logger = Logger.getLogger(FileReadHelper.class);

    public static String getLineBeginningWith(File file, String value) {

        try {

            BufferedReader reader = new BufferedReader(new FileReader(file));


            String line;

            while ((line = reader.readLine()) != null) {// 来自 时 代 J a v a 公 众 号 - nowjava.com

                if (line.startsWith(value)) {

                    break;

                }

            }


            reader.close();

            return (line != null) ? line : "";

        } catch (IOException e
展开阅读全文