Example usage for java.io FileReader read

List of usage examples for java.io FileReader read

Introduction

In this page you can find the example usage for java.io FileReader read.

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:net.ytbolg.mcxa.Config.java

static String ReadFile(String path) throws FileNotFoundException, IOException {

    File file = new File(path);
    FileReader r = new FileReader(file);
    char c[] = new char[(int) file.length()];
    r.read(c);
    System.out.println(String.valueOf(c));
    return String.valueOf(c);
}

From source file:Main.java

/**
 * Read the contents of a file and place them in
 * a string object./*from  w w w . j av a 2 s .c om*/
 *
 * @param file path to file.
 * @return String contents of the file.
 */
public static String fileContentsToString(String file) {
    String contents = "";

    File f = null;
    try {
        f = new File(file);

        if (f.exists()) {
            FileReader fr = null;
            try {
                fr = new FileReader(f);
                char[] template = new char[(int) f.length()];
                fr.read(template);
                contents = new String(template);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    fr.close();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contents;
}

From source file:Main.java

public static String xmlFileToString(String path) {
    FileReader reader = null;
    StringBuilder sb = new StringBuilder();
    char[] buf = new char[1024];
    try {//  w ww  .j a v  a  2 s  .co  m
        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 int countSubstringOccurrence(File file, String substring) throws IOException {
    int count = 0;
    FileReader input = null;
    try {/* ww w .  j a  v  a  2 s . c  om*/
        int currentSubstringIndex = 0;
        char[] buffer = new char[4096];

        input = new FileReader(file);
        int numRead = input.read(buffer);
        while (numRead != -1) {

            for (char c : buffer) {
                if (c == substring.charAt(currentSubstringIndex)) {
                    currentSubstringIndex++;
                    if (currentSubstringIndex == substring.length()) {
                        count++;
                        currentSubstringIndex = 0;
                    }
                } else {
                    currentSubstringIndex = 0;
                }
            }
            numRead = input.read(buffer);
        }
    } finally {
        closeQuietly(input);
    }
    return count;
}

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. ja  v a  2 s.  com*/
        do {
            contents.append(buffer, 0, read);
            read = in.read(buffer);
        } while (read >= 0);
        return contents.toString();
    } finally {
        in.close();
    }
}

From source file:Main.java

public static String readFileContent(String filename) throws IOException {
    FileReader in = new FileReader(filename);
    StringBuilder contents = new StringBuilder();
    char[] buffer = new char[4096];
    int read = 0;
    try {//from   w ww.  ja  v a  2 s . c o m
        do {
            contents.append(buffer, 0, read);
            read = in.read(buffer);
        } while (read >= 0);
        return contents.toString();
    } finally {
        in.close();
    }
}

From source file:net.ytbolg.mcxa.Launcher.GameInfoGet.java

public static ArrayList getLibs(String version) throws FileNotFoundException, IOException, JSONException {
    //   System.out.println("4");//   System.out.println("getLibs");
    File file = new File(GameInfo.GameDir + tpf + "versions" + tpf + version + tpf + version + ".json");
    FileReader r = new FileReader(file);
    char c[] = new char[(int) file.length()];
    r.read(c);
    //   RandomAccessFile re = new RandomAccessFile(dir + tpf + "versions" + tpf + version + tpf + version + ".json", "rwd");
    //  FileReader r = new FileReader();
    //  BufferedReader br = new BufferedReader(r);
    //   byte c[] = new byte[(int) re.length() - 1];
    //   String tmp = "";
    String read = String.valueOf(c);
    //    System.out.println("start read libs");
    //    re.readFully(c);
    /* for(int xxxx=0;xxxx<c.length;xxxx++){
        read=read+Byte.toString(c[xxxx]);
        }*//*from w  ww .  j  ava 2 s. c om*/
    //  read = Arrays.toString(c);

    //  System.out.println("read libs success");
    //  System.out.println(read);
    JSONArray ar = new JSONArray(new JSONObject(read).getString("libraries"));
    ArrayList al = new ArrayList();
    for (int i = 0; i < ar.length(); i++) {
        al.add(ar.getJSONObject(i));
    }
    return al;
}

From source file:importer.handler.post.stages.Splitter.java

private static String readConfig(String fName) throws IOException {
    File f = new File(fName);
    FileReader fr = new FileReader(f);
    char[] data = new char[(int) f.length()];
    fr.read(data);
    // use platform encoding - pretty simple
    return new String(data);
}

From source file:com.ianzepp.logging.jms.service.BasicDaoTest.java

/**
 * TODO Method description for <code>readTableMigration()</code>
 * /*from   ww  w .  ja v a2 s  .  c  o  m*/
 * @param tableName
 * @return
 */
private static String readFile(String filePath) {
    StringBuffer fileData = new StringBuffer(4096);

    try {
        FileReader reader = new FileReader(filePath);
        char[] charBuffer = new char[2048];
        int readSize = -1;

        while ((readSize = reader.read(charBuffer)) >= 0) {
            fileData.append(charBuffer, 0, readSize);
        }

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

    return fileData.toString();
}

From source file:org.schreibubi.JCombinationsTools.templateEngine.TemplateEngine.java

/**
 * @param symbolTable//from w w  w  . java2 s.c om
 *            symbolTable containing the variables
 * @param generateSeparateFiles
 *            If true, then each instance of a template is put into a separate file. The name of the file is
 *            determined by the variable name passed in name. If false everything is put into one file, which name
 *            is passed in name.
 * @param outputFilenameOrVariable
 *            Name of the output file if generateSeparateFiles is false or the variable name which contains the
 *            filename if it is true.
 * @param multiTemplate
 *            template name is taken from a variable
 * @param templateFilenameOrVariable
 *            Name of the template file if multiTemplate is false or the variable name which contains the template
 *            name to use.
 * @param inputDir
 *            TODO
 * @param outputDir
 *            Name of an output directory to use.
 * @param headerFile
 *            Filename of the header to prepend
 * @param footerFile
 *            Filename of the footer to append
 * @return List of files generated
 * @throws Exception
 */
public static VArrayList<String> exec(VArrayList<VHashMap<Symbol>> symbolTableLines,
        boolean generateSeparateFiles, String outputFilenameOrVariable, boolean multiTemplate,
        String templateFilenameOrVariable, File inputDir, File outputDir, File headerFile, File footerFile)
        throws Exception {
    VArrayList<String> fns = new VArrayList<String>();
    char footer[] = null;
    if (footerFile != null) {
        if (!footerFile.exists()) {
            throw new Exception(footerFile.toString() + " does not exist");
        }
        FileReader fr = new FileReader(footerFile);
        footer = new char[(int) footerFile.length()];
        fr.read(footer);
        fr.close();
    }
    char header[] = null;
    if (headerFile != null) {
        if (!headerFile.exists()) {
            throw new Exception(headerFile.toString() + " does not exist");
        }
        FileReader fr = new FileReader(headerFile);
        header = new char[(int) headerFile.length()];
        fr.read(header);
        fr.close();
    }

    TemplateTreeWalker TemplateWalker = null;
    TemplateParser TemplateParser = null;
    if (!multiTemplate) {
        File templateFile = new File(templateFilenameOrVariable);
        if (!templateFile.exists()) {
            throw new Exception(templateFilenameOrVariable + " does not exist");
        }

        TemplateLexer TemplateLexer = new TemplateLexer(new BufferedReader(new FileReader(templateFile)));
        TemplateParser = new TemplateParser(TemplateLexer);
        TemplateParser.statements();

        TemplateWalker = new TemplateTreeWalker();
    }

    PrintWriter pw = null;
    if (!generateSeparateFiles) {
        File s = new File(outputDir, outputFilenameOrVariable);
        pw = new PrintWriter(new FileWriter(s));
        if (header != null) {
            pw.write(header);
        }
        fns.add(s.getPath());
    }

    for (ListIterator<VHashMap<Symbol>> i = symbolTableLines.listIterator(); i.hasNext();) {
        FileWriter fw = null;
        /* get variables used to fill out the actual template */
        VHashMap<Symbol> sT = i.next();
        if (generateSeparateFiles) {
            String n = evalTemplateExpression(outputFilenameOrVariable, sT);
            File s = new File(outputDir, n);
            fw = new FileWriter(s);
            pw = new PrintWriter(fw);
            if (header != null) {
                pw.write(header);
            }
            fns.add(s.getPath());
        }

        /*
         * if we have multiple templates in one run reread the template
         */
        if (multiTemplate) {
            String multiTemplateFilename = evalTemplateExpression(templateFilenameOrVariable, sT);
            File templateFile = FileNameLookupSingleton.getInstance().lookup(inputDir, multiTemplateFilename);
            if (!templateFile.exists()) {
                throw new Exception(multiTemplateFilename + " does not exist");
            }

            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(multiTemplateFilename)));
            TemplateLexer TemplateLexer = new TemplateLexer(bufferedReader);
            TemplateParser = new TemplateParser(TemplateLexer);
            TemplateParser.statements();
            bufferedReader.close();
            TemplateWalker = new TemplateTreeWalker();
        }

        TemplateWalker.statements(TemplateParser.getAST(), sT, pw);
        if (generateSeparateFiles) {
            if (footer != null) {
                pw.write(footer);
            }
            pw.close();
            fw.close();
        }
    }

    if (!generateSeparateFiles) {
        if (footer != null) {
            pw.write(footer);
        }
        pw.close();
    }
    return fns;
}