Android File Read loadFile(String str)

Here you can find the source of loadFile(String str)

Description

load file into char arrray

License

Open Source License

Parameter

Parameter Description
str file name

Return

char array which is representation of file.

Declaration

public static char[] loadFile(String str) 

Method Source Code

//package com.java2s;
/*// w w w.  jav  a2s  .  c om
 * SenUtils.java - utilities for Sen.
 * 
 * Copyright (C) 2001, 2002 Takashi Okamoto <tora@debian.org>
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *  
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.io.InputStreamReader;

public class Main {
    /**
     * load file into char arrray
     * 
     * @param str
     *            file name
     * @return char array which is representation of file.
     */
    public static char[] loadFile(String str) {
        char c[] = null;
        BufferedReader reader = null;
        try {

            File f = new File(str);
            reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(f)));
            int len = (int) f.length();
            c = new char[len];
            for (int i = 0; i < len; i++) {
                int tmp = reader.read();
                c[i] = (char) tmp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        return c;
    }
}

Related

  1. readFromFile(String filename)
  2. readInstallationFile(File installation)
  3. readStringFromFile(File file)
  4. readTemplate(String filePath)
  5. readTextFile(File file)
  6. getFileContent(File file)
  7. LoadFile(String fileName, Resources resources)
  8. readFileToByteArray(String pathToFile)