Android RandomAccessFile Read readFromFile(final String path)

Here you can find the source of readFromFile(final String path)

Description

Reads a file into a string.

License

Open Source License

Parameter

Parameter Description
path the path of the file

Exception

Parameter Description
IOException if a problem occurs while reading the

Return

the text read from the file

Declaration

public static String readFromFile(final String path) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.io.RandomAccessFile;

public class Main {
    /**//from w w  w  .  j a  v  a 2s.c  om
     * Reads a file into a string.
     * 
     * @param path the path of the file
     * @return the text read from the file
     * @throws IOException if a problem occurs while reading the
     */
    public static String readFromFile(final String path) throws IOException {
        final RandomAccessFile access = new RandomAccessFile(path, "r");

        final StringBuffer result = new StringBuffer();
        try {
            while (access.getFilePointer() < access.length()) {
                result.append(access.readLine() + "\n");
            }
        } finally {
            access.close();
        }

        return result.toString();
    }
}

Related

  1. writeIntLittleEndian(RandomAccessFile file, int value)
  2. readString(RandomAccessFile file, int size)
  3. fillPadding(RandomAccessFile file, int size)
  4. readIntLittleEndian(RandomAccessFile file)