Java Text File Read Line readLines(File file)

Here you can find the source of readLines(File file)

Description

read Lines

License

Apache License

Declaration

public static ArrayList<String> readLines(File file) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.util.ArrayList;

public class Main {
    private static final String NEWLINE = System.lineSeparator();

    public static ArrayList<String> readLines(File file) throws FileNotFoundException, IOException {
        ArrayList<String> lines = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String ln;//  w  ww.ja  v a 2  s. c  om
            while ((ln = br.readLine()) != null)
                lines.add(ln);
        }
        return lines;
    }

    public static String readLine(RandomAccessFile wiki) throws IOException {
        long start = wiki.getFilePointer();
        wiki.readLine();
        long end = wiki.getFilePointer();
        wiki.seek(start);
        byte[] b = new byte[(int) (end - start)];
        wiki.read(b);
        return new String(b).replace(System.lineSeparator(), "");
    }

    public static String read(String file) throws FileNotFoundException, IOException {
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            return read(br);
        }
    }

    public static String read(BufferedReader br) throws IOException {
        StringBuffer sbuff = new StringBuffer();
        String ln;
        while ((ln = br.readLine()) != null)
            sbuff.append(ln).append(NEWLINE);
        return sbuff.toString();
    }
}

Related

  1. readLines(File f)
  2. readLines(File f)
  3. readLines(File file)
  4. readLines(File file)
  5. readLines(File file)
  6. readLines(File file)
  7. readLines(File file)
  8. readLines(File file)
  9. readLines(File file)