Java InputStreamReader Read readFileOneLine(String filename, String keyInLine)

Here you can find the source of readFileOneLine(String filename, String keyInLine)

Description

read File One Line

License

Open Source License

Declaration

public static String readFileOneLine(String filename, String keyInLine) throws Exception 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.InputStreamReader;

public class Main {
    public static String readFileOneLine(String filename, String keyInLine) throws Exception {
        File f = new File(filename);
        if (!f.exists())
            return null;

        FileInputStream fis = null;
        InputStreamReader fsr = null;
        BufferedReader reader1 = null;

        String line;// www  .  j a  v  a  2  s  .  c o m
        try {
            fis = new FileInputStream(filename);
            fsr = new InputStreamReader(fis, "UTF-8");
            reader1 = new BufferedReader(fsr);

            while ((line = reader1.readLine()) != null) {
                if (line.indexOf(keyInLine) != -1) {
                    break;
                }
            }
        } finally {
            try {
                if (reader1 != null)
                    reader1.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (fsr != null)
                    fsr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (fis != null)
                    fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return line;
    }
}

Related

  1. readFileFromResource(String filename)
  2. readFileIntoLines(File file)
  3. readFileIntoLinesOfLongs(File file)
  4. readFileLineByLine(final File filePath)
  5. readFileList(File file)
  6. readFileOrResource(String source)
  7. readFiles(String[] files, String encoding)
  8. readFileSB(File f)
  9. readFileToLines(File file)