Android File Read read(File path, int i)

Here you can find the source of read(File path, int i)

Description

Read file in line i

Parameter

Parameter Description
path a parameter
i number of line

Declaration


private static String read(File path, int i) 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;

import java.util.NoSuchElementException;

import java.util.Scanner;

public class Main {
    /**//from   w w w. java 2s.  c  o m
     * Read file in line i
     * 
     * @param path
     * @param i
     *            number of line
     * @return
     */

    private static String read(File path, int i) {

        try {
            if (i == 1) {
                return new Scanner(path).nextLine();
            } else {
                for (int j = 1; j < i; j++) {
                    new Scanner(path).nextLine();
                }
                return new Scanner(path).nextLine();
            }
        } catch (FileNotFoundException e) {
            return null;
        } catch (NoSuchElementException n) {
            return null;
        }

    }

    public static String read(Scanner scan, int i) {
        try {
            if (i == 1) {
                i++;
                return scan.nextLine();
            } else {
                for (int j = 1; j < i;) {
                    scan.nextLine();
                    j++;
                }
                i++;
                return scan.nextLine();
            }
        } catch (NoSuchElementException n) {
            return null;
        }

    }
}

Related

  1. readFile(File file)
  2. readFile(String filepath)
  3. readFileAsString(String filePath)
  4. readFileToByte(File file)