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(char cbuf[], int offset, int length) throws IOException 

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("hello.txt");
    FileReader fr = new FileReader(f);

    char[] c = new char[(int) f.length()];
    char[] cnew = new char[(int) f.length()];
    StringBuffer sbuf = new StringBuffer();
    fr.read(c, 0, (int) f.length());

    int len = (int) f.length();

    for (int i = 0, j = len - 1; i < len; i++, j--) {
        cnew[i] = c[j];//from  w  w w. j  a  v a2s. c  o m
        sbuf.append(cnew[i]);
    }

    System.out.println(sbuf.toString());
    fr.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("Main.java");
    FileReader fr = new FileReader(f);

    char[] c = new char[(int) f.length()];
    char[] cnew = new char[(int) f.length()];
    StringBuffer sbuf = new StringBuffer();
    fr.read(c, 0, (int) f.length());

    int len = (int) f.length();

    for (int i = 0, j = len - 1; i < len; i++, j--) {
        cnew[i] = c[j];/*from w  ww. j  a v  a 2 s  .  c o  m*/
        sbuf.append(cnew[i]);
    }

    System.out.println(sbuf.toString());
    fr.close();

}

From source file:com.dianxin.imessage.common.util.SignUtil.java

public static PrivateKey getPrivateKey(String keypath) {
    if (privateKey != null)
        return privateKey;
    log.debug("???");
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    try {//from w w  w  .  j  a  v a  2s .c  om
        FileReader fileReader = new FileReader(keypath);
        char[] cbuf = new char[40960];
        fileReader.read(cbuf, 0, 40960);
        PEMReader reader = new PEMReader(new StringReader(new String(cbuf)));
        KeyPair keyPair = (KeyPair) reader.readObject();
        privateKey = keyPair.getPrivate();
        reader.close();
    } catch (Exception e) {
        log.warn("??", e);
    }
    return privateKey;
}

From source file:Main.java

public static String readDeviceUUID(String devicePath) {
    String uuid = null;// w  w  w  .  j  a  va  2  s. c o  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:de.uni_rostock.goodod.tools.Configuration.java

private static boolean isXMLConfig(File confFile) throws FileNotFoundException, IOException {
    FileReader reader = new FileReader(confFile);
    char beginning[] = new char[7];
    // we search for "<?xml" but need two additional characters to accommodate a potential BOM
    reader.read(beginning, 0, 7);
    if (('<' == beginning[0]) && ('?' == beginning[1]) && ('x' == beginning[2]) && ('m' == beginning[3])
            && ('l' == beginning[4])) {
        reader.close();/*w w  w .j  a va  2  s . co m*/
        return true;
    }

    // Case with byte order mark:
    if (('<' == beginning[2]) && ('?' == beginning[3]) && ('x' == beginning[4]) && ('m' == beginning[5])
            && ('l' == beginning[6])) {
        reader.close();
        return true;
    }
    reader.close();
    return false;
}

From source file:org.kepler.Kepler.java

/**
 * read the osextension.txt file and return a hashtable of the properties
 * //from w  w  w.  j  a va 2  s  .  com
 * NOTE this method is duplicated in CompileModules.java. Change both if you
 * change one.
 */
private static Hashtable<String, String> readOSExtensionFile(File f) throws Exception {
    // String newline = System.getProperty("line.separator");
    Hashtable<String, String> properties = new Hashtable<String, String>();
    FileReader fr = new FileReader(f);
    StringBuffer sb = new StringBuffer();
    char[] c = new char[1024];
    int numread = fr.read(c, 0, 1024);
    while (numread != -1) {
        sb.append(c, 0, numread);
        numread = fr.read(c, 0, 1024);
    }
    fr.close();

    String propertiesStr = sb.toString();
    // String[] props = propertiesStr.split(newline);
    String[] props = propertiesStr.split(";");
    for (int i = 0; i < props.length; i++) {
        String token1 = props[i];
        StringTokenizer st2 = new StringTokenizer(token1, ",");
        String key = st2.nextToken();
        String val = st2.nextToken();
        properties.put(key, val);
    }

    return properties;
}

From source file:com.agloco.util.StringUtil.java

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

    File f = new File(file);

    if (f.exists()) {
        try {
            FileReader fr = new FileReader(f);
            int size = (int) f.length();
            int len = 0, off = 0;
            char[] template = new char[size];
            do {
                len = fr.read(template, off, size - off);
                if (len > 0)
                    off += len;
            } while (len > 0);
            contents = new String(template);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }

    return contents;
}

From source file:org.apache.velocity.test.sql.HsqlDB.java

private String getFileContents(String fileName) throws Exception {
    FileReader fr = null;

    try {/*  w  ww.  jav  a  2  s . co  m*/
        fr = new FileReader(fileName);

        char[] fileBuf = new char[1024];
        StringBuffer sb = new StringBuffer(1000);
        int res = -1;

        while ((res = fr.read(fileBuf, 0, 1024)) > -1) {
            sb.append(fileBuf, 0, res);
        }

        return sb.toString();
    } finally {

        if (fr != null) {
            fr.close();
        }
    }
}

From source file:com.paxxis.cornerstone.encryption.TripleDESEncryptionHandler.java

public void initialize() {
    if (encryptionKeyFile != null) {
        try {//w ww.j av a2s  .  c  om
            FileReader fr = new FileReader(encryptionKeyFile);
            int cnt;
            char[] cbuf = new char[256];
            StringBuilder builder = new StringBuilder();
            while (-1 != (cnt = fr.read(cbuf, 0, 256))) {
                String s = new String(cbuf, 0, cnt);
                builder.append(s);
            }

            encryptionKey = builder.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    if (encryptionKey == null) {
        throw new RuntimeException("No Encryption Key");
    }

    try {
        byte[] arrayBytes = encryptionKey.getBytes(UNICODE_FORMAT);
        KeySpec ks = new DESedeKeySpec(arrayBytes);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME);
        cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME);
        key = skf.generateSecret(ks);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.qedeq.base.io.IoUtility.java

/**
 * Reads contents of a file into a string buffer. Uses default encoding.
 *
 * @param   file        This file will be loaded.
 * @param   buffer      Buffer to fill with file contents.
 * @throws  IOException File exception occurred.
 *
 * @deprecated  Use {@link #loadFile(File, StringBuffer, String)}.
 *//*w ww  .  ja va 2  s  .  c  o  m*/
public static void loadFile(final File file, final StringBuffer buffer) throws IOException {

    final int size = (int) file.length();
    final char[] data = new char[size];
    buffer.setLength(0);
    FileReader in = null;
    try {
        in = new FileReader(file);
        int charsread = 0;
        while (charsread < size) {
            charsread += in.read(data, charsread, size - charsread);
        }
    } finally {
        close(in);
    }
    buffer.insert(0, data);
}