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:Main.java

public static void main(String[] argv) throws Exception {
    FileReader fr = new FileReader("text.txt");
    int count;//from   w ww .  ja va 2s. c  o m
    char chrs[] = new char[80];

    do {
        count = fr.read(chrs);
        for (int i = 0; i < count; i++)
            System.out.print(chrs[i]);
    } while (count != -1);

}

From source file:Test.java

public static void main(String[] args) throws Exception {

    final URL test = Test.class.getResource("/");
    System.out.println(test);/* w  ww  .j a  v  a  2  s . c  om*/

    final URL url = Test.class.getResource("/resources/test.txt");
    System.out.println(url);

    if (url == null) {
        System.out.println("URL is null");
    } else {
        final File file = new File(url.toURI());
        final FileReader reader = new FileReader(file);
        final char[] buff = new char[20];
        final int l = reader.read(buff);
        System.out.println(new String(buff, 0, l));
        reader.close();
    }

}

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

/** test and commandline utility */
public static void main(String[] args) {
    if (args.length >= 1) {
        try {/*from   www. jav a 2s .  c  o m*/
            int i = 0;
            int fileIndex = 0;
            // see if the user supplied a conf file
            String textConf = Discriminator.defaultConf;
            while (i < args.length) {
                if (args[i].equals("-c") && i < args.length - 1) {
                    textConf = readConfig(args[i + 1]);
                    i += 2;
                } else {
                    fileIndex = i;
                    i++;
                }
            }
            File f = new File(args[fileIndex]);
            char[] data = new char[(int) f.length()];
            FileReader fr = new FileReader(f);
            fr.read(data);
            JSONObject config = (JSONObject) JSONValue.parse(textConf);
            Splitter split = new Splitter(config);
            Map<String, String> map = split.split(new String(data));
            Set<String> keys = map.keySet();
            String rawFileName = args[fileIndex];
            int pos = rawFileName.lastIndexOf(".");
            if (pos != -1)
                rawFileName = rawFileName.substring(0, pos);
            Iterator<String> iter = keys.iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                String fName = rawFileName + "-" + key + ".xml";
                File g = new File(fName);
                if (g.exists())
                    g.delete();
                FileOutputStream fos = new FileOutputStream(g);
                fos.write(map.get(key).getBytes("UTF-8"));
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    } else
        System.out.println("usage: java -jar split.jar [-c json-config] <tei-xml>\n");
}

From source file:com.sxj.spring.modules.mapper.JsonMapper.java

public static void main(String... args) throws JsonProcessingException, IOException {
    JsonMapper mapper = JsonMapper.nonEmptyMapper();
    CJ30 cj30 = new CJ30();
    Map<String, String> name = cj30.getName();
    name.put("2", "");
    name.put("3", "?");
    name.put("4", "");
    name.put("5", "");
    name.put("7", "");
    name.put("8", "?");
    Map<String, Map<String, Data>> data = cj30.getData();
    Map<String, Data> subject1 = new HashMap<String, Data>();
    Data d1 = new Data();
    d1.setDate("01/14");
    d1.setMin(41700);/*from   w w w  .j a va 2s.  c om*/
    d1.setMax(41780);
    d1.setAverage(41740);
    subject1.put("1421164800", d1);

    Data d2 = new Data();
    d2.setDate("01/15");
    d2.setMin(41550);
    d2.setMax(41620);
    d2.setAverage(41585);
    subject1.put("1421251200", d2);
    data.put("2", subject1);

    Map<String, Data> subject2 = new HashMap<String, Data>();
    Data d3 = new Data();
    d3.setDate("01/14");
    d3.setMin(12450);
    d3.setMax(12490);
    d3.setAverage(12470);
    subject2.put("1421164800", d3);

    Data d4 = new Data();
    d4.setDate("01/15");
    d4.setMin(12730);
    d4.setMax(12770);
    d4.setAverage(12750);
    subject2.put("1421251200", d4);
    data.put("3", subject2);
    String json = mapper.toJson(cj30);
    System.out.println(json);

    FileReader reader = new FileReader(new File("E:\\cj30.js"));
    char[] buffer = new char[1024];
    int read = 0;
    StringBuilder sb = new StringBuilder();
    while ((read = reader.read(buffer)) > 0) {
        sb.append(buffer, 0, read);
    }
    CJ30 fromJson = mapper.fromJson(sb.toString(), CJ30.class);
    System.out.println(fromJson.getName());
}

From source file:Main.java

public static String read(File src) throws IOException {
    long length = src.length();

    // Probably not the best way to do it, but I'm lazy.
    char[] chars = new char[(int) length];
    FileReader fileReader = new FileReader(src);
    fileReader.read(chars);
    fileReader.close();//from  ww w  .  jav  a  2 s .c  o m
    return new String(chars);
}

From source file:Main.java

public static String readFile(String filename) {
    String content = null;/* www  . java2 s .c o m*/
    File file = new File(filename); //for ex foo.txt
    try {
        FileReader reader = new FileReader(file);
        char[] chars = new char[(int) file.length()];
        reader.read(chars);
        content = new String(chars);
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}

From source file:Main.java

/**
 * Load a text file contents as a <code>String<code>.
 * This method does not perform enconding conversions
 *
 * @param file The input file/*from  w w w  .  j  av a  2 s  .  c o m*/
 * @return The file contents as a <code>String</code>
 * @exception IOException IO Error
 */
public static String deserializeString(File file) throws IOException {
    int len;
    char[] chr = new char[4096];
    final StringBuffer buffer = new StringBuffer();
    final FileReader reader = new FileReader(file);
    try {
        while ((len = reader.read(chr)) > 0) {
            buffer.append(chr, 0, len);
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}

From source file:osc.git.eh3.viplugin.CheckLicenseFile.java

private static boolean checkLicenseFile(String fileName) throws Exception {
    char[] buffer = new char[(int) new File(fileName).length()];
    try {//from ww  w . j a  v a 2  s .  c  o m
        FileReader fileReader = new FileReader(fileName);
        fileReader.read(buffer);
        fileReader.close();
    } catch (FileNotFoundException e) {
        throw new Exception("License file not found: " + fileName);
    } catch (IOException e) {
        throw new Exception("Can't read license file: " + fileName);
    }
    FileReader fileReader;
    String license = new String(buffer);
    if (!decrypt(license)) {
        throw new Exception("Invalid license found: " + fileName);
    }
    return true;
}

From source file:Main.java

public static String readToString(File file) throws IOException {
    if (file == null || !file.exists()) {
        return null;
    }//from w  w w  .ja v a 2s. c o  m
    FileReader reader = new FileReader(file);
    StringBuilder out = new StringBuilder();
    char[] buffer = new char[1024 * 4];
    int numRead = 0;
    while ((numRead = reader.read(buffer)) > -1) {
        out.append(String.valueOf(buffer, 0, numRead));
    }
    reader.close();
    return out.toString();
}

From source file:Main.java

public static String getStringFromFile(String file) throws IOException {
    char[] data = null;
    FileReader fr = null;
    try {// w  w  w  .j  av  a 2  s  .  c o m
        File f = new File(file);
        data = new char[(int) f.length()];
        fr = new FileReader(f);
        fr.read(data);
    } catch (IOException e) {
        throw e;
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return new String(data);
}