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

/**
 * Read a text file/*from  w ww . j  a v  a2  s.  c  om*/
 * 
 * @param file
 *            Which is read
 * 
 * @throws IOException
 *             Input/Output exceptions
 * 
 * @return contents of file
 */
public static String readTextFile(File file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    StringBuilder text = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        text.append(line);
        text.append("\n");
    }
    reader.close();
    return text.toString();
}

From source file:Main.java

public static String processFile(BufferedReaderProcessor p) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))) {
        return p.process(br);
    }//from  www  . j  a  va2  s .  c  om

}

From source file:Main.java

public static String readFile(String filePath) {
    File file = new File(filePath);
    StringBuilder fileContent = new StringBuilder("");
    if (file == null || !file.isFile()) {
        return fileContent.toString();
    }/*w  w w. j  ava2  s .  c om*/
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (!fileContent.toString().equals("")) {
                fileContent.append("\r\n");
            }
            fileContent.append(line);
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
    return fileContent.toString();
}

From source file:Main.java

public static String getDtdAsString(String uri) throws IOException {
    Reader in = null;//w  ww  .ja v  a  2 s.  co m
    if ((uri.startsWith("http")) || (uri.startsWith("ftp")) || (uri.startsWith("file:")))
        in = new InputStreamReader(new URL(uri).openStream());
    else {
        in = new FileReader(uri);
    }
    StringWriter out = new StringWriter();
    char[] buffer = new char[4096];
    for (int count = in.read(buffer); count != -1; count = in.read(buffer)) {
        out.write(buffer, 0, count);
    }
    return out.getBuffer().toString();
}

From source file:Main.java

public static String readDeviceUUID(String devicePath) {
    String uuid = null;//from w  ww  . j a v a  2 s  .co  m
    File root = new File(devicePath);
    if (root.isDirectory()) {
        File uFile = new File(devicePath, ".uuid");
        FileReader reader = null;
        try {
            char[] buffer = new char[36];
            reader = new FileReader(uFile);
            reader.read(buffer, 0, buffer.length);
            uuid = new String(buffer);
            Log.d(TAG, "readDeviceUUID uuid:" + uuid);
            return uuid;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return uuid;
}

From source file:Main.java

public static String readTextFile(File file) {
    if (!file.exists()) {
        return null;
    }/*from  ww w . ja  v a 2s. c  o m*/

    StringBuilder sb = new StringBuilder();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

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

    return sb.toString();
}

From source file:Main.java

/**
 * Read a file and return as a String//ww  w  . j  a  v a 2 s.co m
 *
 * @param file the file to read
 * @return the string representing the contents of the file
 * @throws IOException when reading the file fails
 * @since 0.11.0
 */
public static String readFile(File file) throws IOException {
    Reader reader = new FileReader(file);
    BufferedReader br = new BufferedReader(reader);
    try {
        StringBuilder sb = new StringBuilder();
        for (;;) {
            String line = br.readLine();
            if (line == null) {
                return sb.toString();
            } else {
                sb.append(line);
            }
            sb.append(System.getProperty("line.separator"));
        }
    } finally {
        br.close();
    }
}

From source file:Main.java

@TargetApi(4)
public static String getCpuInfo() {
    StringBuffer sb = new StringBuffer();
    sb.append("abi: ").append(Build.CPU_ABI).append("\n");
    if (new File("/proc/cpuinfo").exists()) {
        try {//from www. j av  a2 s.  co  m
            BufferedReader br = new BufferedReader(new FileReader(new File("/proc/cpuinfo")));
            String aLine;
            while ((aLine = br.readLine()) != null) {
                sb.append(aLine + "\n");
            }
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Reads content of file, and returns result as string
 *
 * @param filename path to file/*from ww w .jav  a  2s .  c  om*/
 * @return Contents of file
 * @throws IOException
 */
public static String fileToString(String filename) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    StringBuilder builder = new StringBuilder();
    String line;

    // For every line in the file, append it to the string builder
    while ((line = reader.readLine()) != null) {
        builder.append(line);
    }
    reader.close();

    return builder.toString();
}

From source file:Main.java

public static String readFile(final String path, final String fileName) {
    final File file = new File(path, fileName);
    final StringBuilder builder = new StringBuilder();
    BufferedReader br = null;// w ww  .ja  va  2s .  c  o m
    try {
        br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            builder.append(line);
            builder.append('\n');
        }
    } catch (final Exception ex) {
        ex.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (final IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return builder.toString();
}