Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte of data from this input stream.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {// www. j  ava  2s .  c o  m

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os, Charset.defaultCharset());

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {/*from   w  w  w  . j  av a 2 s .  com*/

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os, java.nio.charset.StandardCharsets.UTF_8);

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
        try {//from   ww w.  j  a  v  a 2s  .com
            FileInputStream fin = new FileInputStream(args[i]);
            FileOutputStream fout = new FileOutputStream(args[i] + "dfl");
            DeflaterOutputStream dos = new DeflaterOutputStream(fout);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                dos.write(c);
            }
            dos.close();
            fin.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
        try {/*from   www.  ja  va  2s .  com*/
            FileInputStream fin = new FileInputStream(args[i]);
            FileOutputStream fout = new FileOutputStream(args[i] + DEFLATE_SUFFIX);
            DeflaterOutputStream dos = new DeflaterOutputStream(fout);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                dos.write(c);
            }
            dos.close();
            fin.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

From source file:CopyBytes.java

public static void main(String[] args) throws IOException {
    FileInputStream in = null;
    FileOutputStream out = null;/*ww  w. j a  v  a2  s  . c  o  m*/
    try {
        in = new FileInputStream("xanadu.txt");
        out = new FileOutputStream("outagain.txt");
        int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Properties prop = new Properties();

    prop.put("Chapter Count", "200");
    prop.put("Tutorial Count", "15");
    prop.put("tutorial", "java2s.com");

    // create a output and input as a xml file
    FileOutputStream fos = new FileOutputStream("properties.xml");
    FileInputStream fis = new FileInputStream("properties.xml");

    // store the properties in the specific xml
    prop.storeToXML(fos, "Properties Example");

    // print the xml
    while (fis.available() > 0) {
        System.out.print((char) fis.read());
    }//from www  . j  a  v  a  2 s . c  o  m

}

From source file:Main.java

public static void main(String[] args) {

    try {/*from   w ww.jav  a2  s  . co m*/
        // create a new OutputStreamWriter
        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os);

        // create a new FileInputStream to read what we write
        FileInputStream in = new FileInputStream("test.txt");

        // write something in the file
        writer.write(70);

        // flush the stream
        writer.flush();
        // read what we write
        System.out.println((char) in.read());
        writer.close();
        os.close();

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

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");

    keyGenerator.init(128);/*  w ww  . java 2  s  . com*/
    Key secretKey = keyGenerator.generateKey();

    Cipher cipherOut = Cipher.getInstance("Blowfish/CFB/NoPadding");

    cipherOut.init(Cipher.ENCRYPT_MODE, secretKey);
    BASE64Encoder encoder = new BASE64Encoder();
    byte iv[] = cipherOut.getIV();
    if (iv != null) {
        System.out.println("Initialization Vector of the Cipher:\n" + encoder.encode(iv));
    }
    FileInputStream fin = new FileInputStream("inputFile.txt");
    FileOutputStream fout = new FileOutputStream("outputFile.txt");
    CipherOutputStream cout = new CipherOutputStream(fout, cipherOut);
    int input = 0;
    while ((input = fin.read()) != -1) {
        cout.write(input);
    }

    fin.close();
    cout.close();
}

From source file:Main.java

public static void main(String[] args) {

    try {/*from   w ww  . j  av a 2s .  c o  m*/
        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os);

        // create a new FileInputStream to read what we write
        FileInputStream in = new FileInputStream("test.txt");

        // write something in the file
        writer.write(70);

        // flush the stream
        writer.flush();

        // read what we write
        System.out.println((char) in.read());

        // close the stream
        writer.close();
        os.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:testSig.java

public static void main(String[] args) {

    /* Test generating and verifying a DSA signature */

    try {//from   w ww.j a  v  a  2s  .  c o m

        /* generate a key pair */

        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
        keyGen.initialize(1024, new SecureRandom());
        KeyPair pair = keyGen.generateKeyPair();

        /*
         * create a Signature object to use for signing and verifying
         */

        Signature dsa = Signature.getInstance("SHA/DSA");

        /* initialize the Signature object for signing */

        PrivateKey priv = pair.getPrivate();

        dsa.initSign(priv);

        /* Update and sign the data */

        FileInputStream fis = new FileInputStream(args[0]);
        byte b;
        while (fis.available() != 0) {
            b = (byte) fis.read();
            dsa.update(b);
        }
        ;

        fis.close();

        /*
         * Now that all the data to be signed has been read in, sign it
         */
        byte[] sig = dsa.sign();

        /* Verify the signature */

        /* Initialize the Signature object for verification */
        PublicKey pub = pair.getPublic();
        dsa.initVerify(pub);

        /* Update and verify the data */

        fis = new FileInputStream(args[0]);
        while (fis.available() != 0) {
            b = (byte) fis.read();
            dsa.update(b);
        }
        ;

        fis.close();

        boolean verifies = dsa.verify(sig);

        System.out.println("signature verifies: " + verifies);

    } catch (Exception e) {
        System.err.println("Caught exception " + e.toString());
    }

}