Android Text File Read readFileAsString(File file)

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

Description

read File As String

License

Apache License

Declaration

public static String readFileAsString(File file) 

Method Source Code

/*/*from  ww  w. jav  a 2  s .  c  om*/
Copyright (c) 2012 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.FileChannel.*;
import java.util.*;
import java.util.zip.*;

public class Main{
    private static HashMap<String, String> serial;
    public static final String SERIALPREFIX = "SER:";
    public static String readFileAsString(File file, String charset) {
        StringBuffer fStr = new StringBuffer();
        try {
            InputStream is = new FileInputStream(file);
            if (file.getName().endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, charset));
            String line = null;
            while ((line = reader.readLine()) != null) {
                fStr.append(line).append("\n");
            }
            reader.close();
        } catch (FileNotFoundException fe) {
            return null;
        } catch (Exception e) {
            throw new Error(e);
        }
        return fStr.toString();
    }
    public static String readFileAsString(File file) {
        StringBuffer fStr = new StringBuffer();
        try {
            InputStream is = new FileInputStream(file);
            if (file.getName().endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is));
            String line = null;
            while ((line = reader.readLine()) != null) {
                fStr.append(line).append("\n");
            }
            reader.close();
        } catch (FileNotFoundException fe) {
            System.err.println("No file: " + file);
            return null;
        } catch (Exception e) {
            throw new Error(e);
        }
        return fStr.toString();
    }
    public static String readFileAsString(String filename) {
        if (filename.startsWith(SERIALPREFIX)) {
            filename = serialFileName(filename);
        }
        return readFileAsString(new File(filename));
    }
    public static String serialFileName(String serId) {
        if (serId.startsWith(SERIALPREFIX)) {
            serId = serId.substring(SERIALPREFIX.length());
        }
        return serialProperties().get(serId);
    }
    private static HashMap<String, String> serialProperties() {
        if (serial == null) {
            Properties serialP = PropertyLoader
                    .loadProperties("serialization");
            HashMap<String, String> serialM = new HashMap<String, String>();
            for (Map.Entry<Object, Object> e : serialP.entrySet()) {
                if (e.getKey() instanceof String
                        && e.getValue() instanceof String) {
                    serialM.put((String) e.getKey(), (String) e.getValue());
                } else {
                    System.err.println("Bad property: " + e);
                }
            }
            //do replacement on the properties
            serial = new HashMap<String, String>();
            for (Map.Entry<String, String> e : serialM.entrySet()) {
                String path = e.getValue();
                for (Map.Entry<String, String> oe : serialM.entrySet()) {
                    if (e.getKey() != oe.getKey()) {
                        path = path.replace("${" + oe.getKey() + "}",
                                oe.getValue());
                    }
                }
                serial.put(e.getKey(), path);
            }
        }
        return serial;
    }
}

Related

  1. readFile(String fileName)
  2. readFile(String fileName)
  3. readFile(String filePath)
  4. readFile(String filename)
  5. readFile(String path)
  6. readFileAsString(File file, String charset)
  7. readFileAsString(String filename)
  8. readFileLineByLine(File file)
  9. readFree(File file)