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 getXMLString(String filePath) {
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;//from   w ww  . j a v  a  2s  .  c  om
    String line;
    try {
        br = new BufferedReader(new FileReader(filePath));
        while (true) {
            line = br.readLine();
            if (line == null) {
                break;
            }
            sb.append(line + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static String getStringByFile(File f) throws IOException {
    StringBuilder builder = new StringBuilder();
    BufferedReader br = new BufferedReader(new FileReader(f));
    String line;/*from   w  w w  . ja  va 2 s  .com*/
    while ((line = br.readLine()) != null) {
        builder.append(line);
    }
    br.close();
    return builder.toString();
}

From source file:SumLine.java

static void sumLines(String filename) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
    lnr.setLineNumber(1);/*  ww  w.j ava2s .  com*/
    StreamTokenizer stok = new StreamTokenizer(lnr);
    stok.parseNumbers();
    stok.eolIsSignificant(true);
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        int lineno = lnr.getLineNumber();
        double sum = 0;
        while (stok.ttype != StreamTokenizer.TT_EOL) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER)
                sum += stok.nval;
            stok.nextToken();
        }
        System.out.println("Sum of line " + lineno + " is " + sum);
        stok.nextToken();
    }
}

From source file:Main.java

public static String getProcessName(int pid) {
    BufferedReader reader = null;
    try {//from  w w w.j a va 2  s .c om
        reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
        String processName = reader.readLine();
        if (!TextUtils.isEmpty(processName)) {
            processName = processName.trim();
        }
        return processName;
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String xmlFileToString(String path) {
    FileReader reader = null;//from  ww  w .ja v a  2s . c  om
    StringBuilder sb = new StringBuilder();
    char[] buf = new char[1024];
    try {
        reader = new FileReader(path);
        int temp = 0;
        while ((temp = reader.read(buf)) != -1) {
            sb.append(String.valueOf(buf, 0, temp));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String readFile(File file) throws IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);/* w w  w  .  j  a  v a2s  .c om*/
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString();
}

From source file:Main.java

public static String fileToString(String filePath) throws Exception {
    File f = new File(filePath);
    BufferedReader br = new BufferedReader(new FileReader(f));
    try {//  ww w .j  a  va 2 s.c om
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }
        return sb.toString();
    } finally {
        br.close();
    }
}

From source file:Main.java

public static String getEncoding(String f) {
    String encoding = "UTF-8";
    File file = new File(f);
    try {// ww w .  j av  a  2 s . c o  m
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String xml = reader.readLine();
        reader.close();
        xml = xml.toLowerCase();
        int pos = xml.indexOf("encoding");
        if (pos < 0) {
            return encoding;
        }
        xml = xml.substring(pos + "encoding".length());
        pos = xml.indexOf("\"");
        xml = xml.substring(pos + 1);
        pos = xml.indexOf("\"");
        xml = xml.substring(0, pos).trim();
        return xml.toUpperCase();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return encoding;
}

From source file:Main.java

public static String loadFileAsString(String filePath) throws java.io.IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);/*  w  w  w  .  java 2s. c  om*/
    }
    reader.close();
    return fileData.toString();
}

From source file:Main.java

public static String readFromSD(String fileName) throws Exception {
    File file = new File(Environment.getExternalStorageDirectory(), fileName);

    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String line = null;//from   w  w  w.  j a va2 s. c  o m
    StringBuilder sb = new StringBuilder();

    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    return sb.toString();
}