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 int getFileLines(File file) {
    if (!file.canRead() || !file.isFile()) {
        Log.w(LOG_TAG, CLASS + ": getLinesInFile: invalid file.");
        return -1;
    }/*from  ww  w  . ja  v  a 2  s .c o m*/

    int lines = 0;
    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        while (reader.readLine() != null)
            lines++;
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return lines;
}

From source file:Main.java

public static String getRandomNumByLine(int i) {
    File file = new File("/storage/emulated/0/uber_random");
    LineNumberReader lineNumberReader = null;
    StringBuilder builder = new StringBuilder();
    try {//from w w w. j a v a 2 s  . c om
        if (file.exists()) {
            lineNumberReader = new LineNumberReader(new FileReader(file));
            String tmp = null;
            while ((tmp = lineNumberReader.readLine()) != null) {
                builder.append(tmp + "\n");
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != lineNumberReader) {
            try {
                lineNumberReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    String fileString = builder.toString();
    Log.d("TAG", "Read num is --->" + fileString);
    String[] split = fileString.split("\\n");
    if (split != null && (split.length >= i)) {
        String value = split[i];
        Log.d("TAG", "Read sub is --->" + value);
        return value;
    }
    return null;
}

From source file:authorship.verification.ReadJSON.java

public static String readJson(String file) {
    JSONParser parser = new JSONParser();
    try {//from w w  w.j ava2  s .  c  o m
        FileReader fileReader = new FileReader(file);
        JSONObject json = (JSONObject) parser.parse(fileReader);

        language = (String) json.get("language");
        //System.out.println("Language: " + language); 

        JSONArray filenames = (JSONArray) json.get("problems");
        Iterator i = filenames.iterator();
        /*System.out.println("Filenames: "); 
        while (i.hasNext()) { 
        System.out.println(" " + i.next()); 
        } */
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return language;
}

From source file:Main.java

/**
 * /*  ww w.ja  va 2s  .  c  om*/
 * @param aFile
 * @return
 */
public static String getLastLines(String filename, int number) {
    File aFile = new File(filename);
    StringBuilder contents = new StringBuilder();
    LinkedList<String> ll = new LinkedList<String>();

    try {
        BufferedReader input = new BufferedReader(new FileReader(aFile));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                ll.add(line);
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        Log.e(TAG, ex.getMessage());
    }

    if ((ll.size() - number) <= 0) {
        Log.e(TAG, "Requested number of lines exceeds lines of file");
        return "Requested number of lines exceeds lines of file";
    }

    for (int i = (ll.size() - 1); i >= (ll.size() - number); i--) {
        contents.append(ll.get(i - 1));
        contents.append("\n");
    }

    return contents.toString();
}

From source file:Main.java

/** Forms an <code>InputSource</code> for parsing XML documents
 *  from a file. Assumes a default character encoding. Returns
 *  <code>null</code> if any of the exceptions is thrown.
 *
 * @param fileName The name of the file.
 * @return An <code>InputSource</code> representation.
 *///from  www. j av  a2  s .  c o  m
public static InputSource getInputSource(String fileName) {
    InputSource retVal = null;
    try {
        retVal = new InputSource(new BufferedReader(new FileReader(fileName)));
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
    return retVal;
}

From source file:Main.java

public static boolean reloadMapperXml(String spath, String tPath, String name) {
    try {/*w ww. j  a v a  2  s.co m*/
        String linetext = "";
        String resultMapText = "";
        StringBuffer sbf = new StringBuffer();
        File f = new File(spath + name);
        BufferedReader br = new BufferedReader(new FileReader(f));
        int lineNum = 1;
        while ((linetext = br.readLine()) != null) {
            sbf.append(linetext);
            sbf.append("\r\n");
            if (lineNum == 4) {
                resultMapText = linetext;
            }
            lineNum++;
        }
        br.close();
        String text = sbf.toString();

        if (text.contains("queryPage")) {
            return false;
        }

        int si = text.indexOf("<insert id=\"insert\"");
        int ei = text.indexOf("</insert>");
        String noinsert = text.substring(si, ei + 9);
        text = text.replace(noinsert, "");

        int su = text.indexOf("<update id=\"updateByPrimaryKey\"");
        int eu = text.indexOf("</update>", su);
        if (su > 0 && eu > 0) {
            String noupdate = text.substring(su, eu + 9);
            text = text.replace(noupdate, "");
        }

        text = text.replace("selectByPrimaryKey", "selectById");
        text = text.replace("deleteByPrimaryKey", "deleteById");
        text = text.replace("insertSelective", "insert");
        text = text.replace("updateByPrimaryKeySelective", "updateById");

        int sp = text.indexOf("<select");
        int ep = text.indexOf("</select>");

        String pageSql = "";
        if (sp > 0 && ep > 0) {
            pageSql = text.substring(sp, ep + 9);
        }
        pageSql = pageSql.replace("selectById", "queryPage");
        pageSql = pageSql.substring(0, pageSql.indexOf("where"));
        pageSql = pageSql + "</select>";

        int sc = resultMapText.indexOf("type=");
        if (sc > 0) {
            resultMapText = resultMapText.substring(sc + 6);
        }

        int ec = resultMapText.lastIndexOf("\"");
        if (ec > 0) {
            resultMapText = resultMapText.substring(0, ec);
        }

        pageSql = pageSql.replace("java.lang.Integer", resultMapText);

        text = text.replace("</mapper>", "");
        StringBuffer newSql = new StringBuffer(text);
        newSql.append(pageSql);
        newSql.append("</mapper>");

        saveFile(tPath, name, newSql.toString());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:chesspresso.game.view.HTMLGameBrowser.java

public static void main(String[] args) {
    if (args.length < 1) {
        args = new String[] { "fischer_plain.pgn" };

        /*//from  ww  w .ja  v a  2 s .c om
        System.out.println("Usage: java " + HTMLGameBrowser.class.getName() +
         " <PGN filename>");
        System.exit(0); */
    }

    boolean debugMode = true;
    HtmlGenerationOptions htmlGenerationOptions = new HtmlGenerationOptions();
    //htmlGenerationOptions = makeBootstrap2HtmlGenerationOptions();
    //htmlGenerationOptions.imagePrefix = "ugly/";

    try {
        chesspresso.pgn.PGNReader pgn = new chesspresso.pgn.PGNReader(new FileReader(args[0]), "game");
        final Game game = pgn.parseGame();

        final HTMLGameBrowser html = new HTMLGameBrowser();
        html.produceHtml(System.out, game, htmlGenerationOptions, debugMode);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static String getCpuInfo() {
    String str1 = "/proc/cpuinfo";
    String str2 = "";
    String[] cpuInfo = { "", "" };
    String[] arrayOfString;/*from  www. ja va  2  s .co  m*/
    try {
        FileReader fr = new FileReader(str1);
        BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
        str2 = localBufferedReader.readLine();
        arrayOfString = str2.split("\\s+");
        for (int i = 2; i < arrayOfString.length; i++) {
            cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
        }
        str2 = localBufferedReader.readLine();
        arrayOfString = str2.split("\\s+");
        cpuInfo[1] += arrayOfString[2];
        localBufferedReader.close();
    } catch (IOException e) {
    }

    StringBuilder builder = new StringBuilder();
    for (String s : cpuInfo) {
        builder.append(s.toLowerCase() + ",");
    }
    return builder.toString();
}

From source file:Main.java

public static Document fileAsDocument(final File file)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource(new FileReader(file));
    return builder.parse(is);

}

From source file:gov.nih.nci.caintegrator.application.study.ExternalLinksLoader.java

static void loadLinks(ExternalLinkList externalLinkList) throws ValidationException, IOException {
    CSVReader reader = new CSVReader(new FileReader(externalLinkList.getFile()));
    try {// w ww  .jav a 2 s.c  o  m
        loadLinks(externalLinkList, reader);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}