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 String readTextContent(String filePath) {
    BufferedReader reader = null;
    try {//from  w  ww  .  ja  v a  2 s. c o m
        reader = new BufferedReader(new FileReader(filePath));
        StringBuilder sb = new StringBuilder();
        while (reader.ready())
            sb.append(reader.readLine());
        return sb.toString();
    } catch (FileNotFoundException e) {
        //            e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static Object getValue(String filepath, String key) {
    try {//from  ww  w . java 2 s .  c  o  m
        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine jsEngine = sem.getEngineByName("js");
        jsEngine.eval(new FileReader(filepath));
        return jsEngine.get(key);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String[] getMounts(final String path) {
    try {/*from  ww w  . j av  a 2  s . com*/
        BufferedReader br = new BufferedReader(new FileReader("/proc/mounts"), 256);
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.contains(path)) {
                return line.split(" ");
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "/proc/mounts does not exist");
    } catch (IOException e) {
        Log.d(TAG, "Error reading /proc/mounts");
    }
    return null;
}

From source file:SumFile.java

static void sumfile(String filename) throws IOException {
    Reader r = new BufferedReader(new FileReader(filename));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();/*from  w  w  w  . j  ava 2  s.  co  m*/
    double sum = 0;
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        if (stok.ttype == StreamTokenizer.TT_NUMBER)
            sum += stok.nval;
        else
            System.out.println("Nonnumber: " + stok.sval);
        stok.nextToken();
    }
    System.out.println("The file sum is " + sum);
}

From source file:Main.java

/**
 *
 * @param filename//from   w w  w  . j a v  a  2s  . c  om
 */
public static String readTimestampFromFile(String filename) {
    File file = new File(filename);
    try {
        if (file.exists()) {
            BufferedReader br = new BufferedReader(new FileReader(file));
            return br.readLine();
        }
    } catch (Exception e) {
        return null;
    }

    return null;
}

From source file:Main.java

public static String[] getMounts(CharSequence path) {
    BufferedReader bufferedReader = null;
    try {//from  w ww. j a va2s .com
        bufferedReader = new BufferedReader(new FileReader("/proc/mounts"), 256);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (line.contains(path)) {
                return line.split(" ");
            }
        }
    } catch (FileNotFoundException ignored) {
        Log.d("TAG", "/proc/mounts does not exist");
    } catch (IOException ignored) {
        Log.d("TAG", "Error reading /proc/mounts");
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ignored) {
                // ignored
            }
        }
    }
    return null;
}

From source file:Main.java

public static String contentFromFixture(String fixtureName) {
    BufferedReader br;//from w  ww  . ja  v  a  2 s  .  c om
    StringBuilder sb = new StringBuilder();
    try {
        br = new BufferedReader(
                new FileReader("/Users/chang/eclipse/workspace/BasicBlock/src/fixtures/" + fixtureName));

        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static String readTextFile(String path) {
    String everything = "";
    BufferedReader br = null;/*  w  w  w  .ja  v a2 s.c  om*/
    try {
        br = new BufferedReader(new FileReader(path));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = br.readLine();
        }
        everything = sb.toString();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
    return everything;
}

From source file:Main.java

public static long getTotalMemoryPreJB() {
    String str1 = "/proc/meminfo";
    String str2;/*from   w ww  . ja  va  2 s  .c  om*/
    String[] arrayOfString;
    long initial_memory = 0;
    try {
        FileReader localFileReader = new FileReader(str1);
        BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
        str2 = localBufferedReader.readLine();//meminfo
        arrayOfString = str2.split("\\s+");
        initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
        localBufferedReader.close();
        return initial_memory;
    } catch (IOException e) {
        return -1;
    }
}

From source file:Main.java

/**
 * Read content of file on disk/*  w  w w .java2 s  .  c om*/
 *
 * @param filename The absolute filename
 * @return
 */
static public String readAll(String filename) {
    try {
        FileReader fileReader = new FileReader(filename);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuilder stringBuilder = new StringBuilder();

        String currentLine;
        while ((currentLine = bufferedReader.readLine()) != null)
            stringBuilder.append(currentLine + "\n");
        return stringBuilder.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}