Java FileInputStream Read readFile(String fileName)

Here you can find the source of readFile(String fileName)

Description

read File

License

Open Source License

Declaration

public static String readFile(String fileName) 

Method Source Code

//package com.java2s;

import java.io.FileInputStream;
import java.io.IOException;

public class Main {
    public static String readFile(String fileName) {
        StringBuffer sbuf = new StringBuffer();
        FileInputStream reader = null;
        try {//from   w  w w . j a va 2s  .  c o  m
            byte[] buf = new byte[2048];

            reader = new FileInputStream(fileName);
            int read = -1;

            while ((read = reader.read(buf)) != -1) {
                sbuf.append(new String(buf, 0, read));
            }
        } catch (Exception ee) {
            ee.printStackTrace();
            return null;
        }

        finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

        return sbuf.toString();
    }

    public static String toString(String str, char separator, int radix) {
        byte[] value = str.getBytes();
        int digits = (int) (Math.round((float) Math.log(256) / Math.log(radix)));
        StringBuffer buf = new StringBuffer(value.length * (digits + 1));
        for (int i = 0; i < value.length; i++) {
            if (i > 0) {
                buf.append(separator);
            }
            int v = (value[i] & 0xFF);
            String val = Integer.toString(v, radix);
            for (int j = 0; j < digits - val.length(); j++) {
                buf.append('0');
            }
            buf.append(val);
        }
        return buf.toString();
    }
}

Related

  1. readFile(String filename)
  2. readFile(String fileName)
  3. readFile(String filename)
  4. readfile(String filename)
  5. readFile(String filename)
  6. readFile(String fileName)
  7. readFile(String filename)
  8. readFile(String fileName)
  9. readFile(String filename)