Example usage for java.io FileReader FileReader

List of usage examples for java.io FileReader FileReader

Introduction

In this page you can find the example usage for java.io FileReader FileReader.

Prototype

public FileReader(FileDescriptor fd) 

Source Link

Document

Creates a new FileReader , given the FileDescriptor to read, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:Main.java

public static ArrayList<String> getIPAddress() {
    ArrayList<String> address_list = new ArrayList<String>();

    BufferedReader br = null;//from  w  ww  .  ja  v a  2 s  .  c o  m
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {

            if (line.startsWith("192")) {
                //there exists an ip address
                String[] deviceNetworkInfo = line.split(" ");
                String necessaryInfo[] = new String[6];
                int j = 0;
                for (int i = 0; i < deviceNetworkInfo.length; i++) {
                    if (deviceNetworkInfo[i].length() != 0) {
                        necessaryInfo[j] = deviceNetworkInfo[i];
                        j++;
                    }
                }

                address_list.add(necessaryInfo[0]);

            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //return the value
    return address_list;
}

From source file:Main.java

public static String readTextFile(File f) throws IOException {
    BufferedReader br = null;/*from w w  w  . j a va2 s  . co m*/
    String json = "";
    try {

        String sCurrentLine;
        br = new BufferedReader(new FileReader(f));

        while ((sCurrentLine = br.readLine()) != null) {
            json += sCurrentLine;
        }

    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
        }
    }
    return json;
}

From source file:Main.java

public static boolean canOpenFile(String d) {
    FileReader f;/*www .ja  v a  2 s .  c om*/
    try {
        f = new FileReader(d);
        f.close();
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static Object invokeFunction(String filepath, String funcname, Object... params) {
    try {//  w  ww. j  a v  a 2s . com
        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine jsEngine = sem.getEngineByName("js");
        jsEngine.eval(new FileReader(filepath));
        Invocable invocable = (Invocable) jsEngine;
        return invocable.invokeFunction(funcname, params);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static float[][] readMatrix(String path) throws FileNotFoundException {
    int n = 0, p = 0;
    System.err.println("Load matrix from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.startsWith("%")) {
            System.err.println(line);
        } else {/*  w w  w .  ja  v  a 2s  .  c  om*/
            String[] dim = line.split(" ");
            n = Integer.parseInt(dim[0]);
            p = Integer.parseInt(dim[1]);
            System.err.println("num rows: " + n + "\n" + "num cols: " + p);
            break;
        }
    }
    int count = 0;
    float[][] mat = new float[p][n];
    int row = 0, col = 0;
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }
        Float val = Float.parseFloat(line);
        mat[col][row] = val;
        row++;
        if (row == n) {
            row = 0;
            col++;
        }
        count++;
    }
    if (count != n * p) {
        System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p
                + "\n nun elements: " + count);
        return null;
    }
    System.err.println("Done.");
    return mat;
}

From source file:Main.java

public static List<String> readFileToList(String filepath) {
    List<String> data = new ArrayList<String>();

    try {/*from   ww w .  ja v a  2  s  . c  o m*/
        File file = new File(filepath);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String temp;
        while ((temp = bufferedReader.readLine()) != null) {
            data.add(temp);
        }

        bufferedReader.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return data;
}

From source file:Main.java

public static double[][] readMatrixDouble(String path) throws FileNotFoundException {
    int n = 0, p = 0;
    System.err.println("Load matrix from " + path);
    Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
    while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.startsWith("%")) {
            System.err.println(line);
        } else {/*from  w ww.  j  a va 2 s .  c  o  m*/
            String[] dim = line.split(" ");
            n = Integer.parseInt(dim[0]);
            p = Integer.parseInt(dim[1]);
            System.err.println("num rows: " + n + "\n" + "num cols: " + p);
            break;
        }
    }
    int count = 0;
    double[][] mat = new double[p][n];
    int row = 0, col = 0;
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        if (line.isEmpty()) {
            break;
        }
        Double val = Double.parseDouble(line);
        mat[col][row] = val;
        row++;
        if (row == n) {
            row = 0;
            col++;
        }
        count++;
    }
    if (count != n * p) {
        System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p
                + "\n nun elements: " + count);
        return null;
    }
    System.err.println("Done.");
    return mat;
}

From source file:Main.java

@SuppressWarnings("unused")
public static <T> T parseFromFile(Class<T> cls, String filePath)
        throws JsonSyntaxException, FileNotFoundException {
    return gson.fromJson(new BufferedReader(new FileReader(filePath)), cls);
}

From source file:Main.java

public static String getTotalMemory(Context context) {
    String str1 = "/proc/meminfo";
    String str2;/*from  www  . j  av  a2  s  .  c o m*/
    String[] arrayOfString;
    long initial_memory = 0;
    try {
        FileReader localFileReader = new FileReader(str1);
        BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
        str2 = localBufferedReader.readLine();
        arrayOfString = str2.split("\\s+");
        //            for (String num : arrayOfString) {
        //                Log.i(str2, num + "\t");
        //            }
        initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
        localBufferedReader.close();
    } catch (IOException e) {
    }
    return Formatter.formatFileSize(context, initial_memory);
}

From source file:Main.java

public static String getFile(final String filename) {
    String s = "";
    final File f = new File(filename);

    if (f.exists() && f.canRead()) {
        try {//  w  w w.  j  a  v  a  2s. c  om
            final BufferedReader br = new BufferedReader(new FileReader(f), 256);
            String buffer = null;
            while ((buffer = br.readLine()) != null) {
                s += buffer + "\n";
            }

            br.close();
        } catch (final Exception e) {
            Log.e(TAG, "Error reading file: " + filename, e);
            s = null;
        }
    }
    return s;
}