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 getVideoChannelName() {
    File f = new File(CUST_PATH + VIDEO_CHANNEL_FILE_NAME);
    if (f.isFile()) {
        String result = null;//  w  ww  .j ava2 s. c o m
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(f));
            result = br.readLine();
        } catch (FileNotFoundException e) {
            //            Log.e(CHCCustomUtil.class.getName(), "Organization channel file not found!", e);
        } catch (Exception e) {
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (Exception e) {
                }
        }
        if (result != null)
            return result;
        else
            return new String();
    } else
        return new String();
}

From source file:Main.java

/**
 * Reads data from specified file.//w  ww  .  ja  v  a2 s  .  c om
 * @param path Path to file.
 * @return 2-dimensional array of data.
 */
public static ArrayList<ArrayList<String>> loadFile(String path) {
    FileReader fr = null;
    String line = "";
    ArrayList<ArrayList<String>> dataArray = new ArrayList<>();

    try {
        fr = new FileReader(path);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
    }

    BufferedReader br = new BufferedReader(fr);
    ArrayList<String> tokens = new ArrayList<>();

    try {
        while ((line = br.readLine()) != null) {
            tokens = parseLine(line);
            dataArray.add(tokens);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        fr.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return dataArray;
}

From source file:Main.java

public static String readFileContent(File file) throws IOException {
    FileReader in = new FileReader(file);
    StringBuilder contents = new StringBuilder();
    char[] buffer = new char[4096];
    int read = 0;
    try {//from w  w  w  .  j av a2s  . co  m
        do {
            contents.append(buffer, 0, read);
            read = in.read(buffer);
        } while (read >= 0);
        return contents.toString();
    } finally {
        in.close();
    }
}

From source file:Main.java

/**
 * read file/* www .  j av  a  2  s. c  o m*/
 *
 * @param filePath
 * @return if file not exist, return null, else return content of file
 * @throws IOException
 *             if an error occurs while operator BufferedReader
 */
public static StringBuilder readFile(String filePath) {
    File file = new File(filePath);
    StringBuilder fileContent = new StringBuilder("");
    if (file == null || !file.isFile()) {
        return null;
    }

    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();
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:Main.java

/**
 * Read file//w ww .  j  ava  2 s .co m
 * 
 * @param filePath
 *            The path of the file
 * @return StringBuilder Return file content as StringBuffer, if the file
 *         doesn't exist return null
 */
public static StringBuilder readFile(String filePath) {
    File file = new File(filePath);
    StringBuilder fileContent = new StringBuilder("");
    if (file != null && file.isFile()) {
        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();
            return fileContent;
        } catch (IOException e) {
            throw new RuntimeException("IOException occurred. ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new RuntimeException("IOException occurred. ", e);
                }
            }
        }
    }
    return null;
}

From source file:com.docudile.app.services.utils.FileHandler.java

public static Map<String, List<String>> readAllFiles(String folder) throws IOException {
    ArrayList<String> filenames = getFilePaths(folder);
    Map<String, List<String>> files = new HashMap<>();

    for (String filename : filenames) {
        FileReader fr = new FileReader(folder + "/" + filename);
        BufferedReader br = new BufferedReader(fr);
        ArrayList<String> lines = new ArrayList<String>();
        String temp;// ww w  .j a v a2  s  .  co m

        while ((temp = br.readLine()) != null) {
            if (StringUtils.isNotEmpty(temp)) {
                lines.add(temp);
            }
        }
        files.put(filename.split("\\.")[0], lines);
        br.close();
        fr.close();
    }
    return files;
}

From source file:Main.java

/**
 * Creates a DOM from a file representation of an xml record
 * /*from w w  w  .  j av  a  2  s  .com*/
 * @param doc
 *            the xml file
 * @return the DOM document
 */
public static Document parseDomFromFile(File doc) {
    FileReader reader;
    try {
        reader = new FileReader(doc);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Could not open file '" + doc.getName() + "'!", e);
    }
    return parseDom(reader);
}

From source file:su90.etl.utils.DeviceTypeParser.java

public static void initializer(String path) {

    hashmapper = new HashMap<>();

    ObjectMapper mapper = new ObjectMapper();
    try {//from   w  ww  .  j a v a 2s . c  o  m

        Object[][] objs = mapper.readValue(new FileReader(path), Object[][].class);

        for (Object[] subobjs : objs) {
            hashmapper.put((Integer) subobjs[0], (String) subobjs[1]);
        }

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

From source file:Main.java

public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file");
    if (!logFile.exists()) {
        try {//w  w w  .  j  a va 2  s  .  c om
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        BufferedReader buf = new BufferedReader(new FileReader(logFile));

        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        while ((line = buf.readLine()) != null) {
            lines.add(line);
        }

        int size = lines.size();

        //Every time the number of lines go over 1000, only add the last 500. This will keep its size to a minimum
        int i = lines.size() - 500;
        if (i < 0) {
            i = 0;
        }

        buf.close();

        BufferedWriter bufW = new BufferedWriter(new FileWriter(logFile, true));

        if (size > 1000) {
            bufW.write("");
            for (; i < lines.size(); i++) {
                bufW.append(line);
            }
        }

        bufW.append(text + "\n");
        bufW.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Read a file into a StringBuilder.// w ww . j a  v a 2 s . c o m
 * 
 * @param paramFile
 *            The file to read.
 * @param paramWhitespaces
 *            Retrieve file and don't remove any whitespaces.
 * @return StringBuilder instance, which has the string representation of
 *         the document.
 * @throws IOException
 *             throws an IOException if any I/O operation fails.
 */
public static StringBuilder readFile(final File paramFile, final boolean paramWhitespaces) throws IOException {
    final BufferedReader in = new BufferedReader(new FileReader(paramFile));
    final StringBuilder sBuilder = new StringBuilder();
    for (String line = in.readLine(); line != null; line = in.readLine()) {
        if (paramWhitespaces) {
            sBuilder.append(line + "\n");
        } else {
            sBuilder.append(line.trim());
        }
    }

    // Remove last newline.
    if (paramWhitespaces) {
        sBuilder.replace(sBuilder.length() - 1, sBuilder.length(), "");
    }
    in.close();

    return sBuilder;
}