Android File Read readFileToString(String pathToFile)

Here you can find the source of readFileToString(String pathToFile)

Description

read File To String

License

Open Source License

Declaration

public static String readFileToString(String pathToFile) 

Method Source Code

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

import java.io.File;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Main {

    public static String readFileToString(String pathToFile) {
        byte[] buffer = readFileToByteArray(pathToFile);

        if (buffer != null) {
            return new String(buffer);
        }/*from  w ww. ja v  a  2 s  .  c o  m*/

        return null;
    }

    public static byte[] readFileToByteArray(String pathToFile) {
        byte[] result = null;

        try {
            File file = new File(pathToFile);

            if (file.length() <= Integer.MAX_VALUE) {
                result = new byte[(int) file.length()];
                @SuppressWarnings("resource")
                RandomAccessFile raf = new RandomAccessFile(file, "r");

                raf.readFully(result);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. readFile(File file)
  2. readFile(String filepath)
  3. readFileAsString(String filePath)
  4. readFileToByte(File file)
  5. readFileToString(String fileName)
  6. readFromFile(String filename)
  7. readInstallationFile(File installation)
  8. readStringFromFile(File file)
  9. readTemplate(String filePath)