Example usage for java.io FileInputStream available

List of usage examples for java.io FileInputStream available

Introduction

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

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

Usage

From source file:FileIOApp.java

public static void main(String args[]) throws IOException {
    FileOutputStream outStream = new FileOutputStream("test.txt");
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//w w w . j  ava  2 s  . com
    outStream.close();
    FileInputStream inStream = new FileInputStream("test.txt");
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
    inStream.close();
    File f = new File("test.txt");
    f.delete();
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File(args[0]);
    if (!file.exists()) {
        System.out.println(args[0] + " does not exist.");
        return;//w  ww  .j a  v a2  s  .co  m
    }
    if (!(file.isFile() && file.canRead())) {
        System.out.println(file.getName() + " cannot be read from.");
        return;
    }
    try {
        FileInputStream fis = new FileInputStream(file);
        char current;
        while (fis.available() > 0) {
            current = (char) fis.read();
            System.out.print(current);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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");

    prop.storeToXML(fos, "Properties Example", "ISO 8859");

    while (fis.available() > 0) {
        System.out.print((char) fis.read());
    }//from   ww  w . j  av  a2s.  com

}

From source file:Main.java

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

    int i = 0;/*from w w  w . ja  va 2s.  co m*/

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the stream
    while ((i = fis.read()) != -1) {
        // available bytes
        int available = fis.available();

        // convert integer to character
        char c = (char) i;

        System.out.println("Available: " + available);
        System.out.println("Read: " + c);
    }

}

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());
    }//  w  w  w . ja  v a 2s .  c om

}

From source file:testSig.java

public static void main(String[] args) {

    /* Test generating and verifying a DSA signature */

    try {/*from   ww w . ja  va2s.com*/

        /* 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());
    }

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    FileInputStream fis = new FileInputStream("cipherTest.in");
    FileOutputStream fos = new FileOutputStream("cipherTest.out");
    int dataInputSize = fis.available();

    byte[] inputBytes = new byte[dataInputSize];
    fis.read(inputBytes);/*from  w ww.  j av  a 2  s  .  c  om*/
    write(inputBytes, fos);
    fos.flush();
    fis.close();
    fos.close();

    String inputFileAsString = new String(inputBytes);
    System.out.println("INPUT FILE CONTENTS\n" + inputFileAsString + "\n");

    System.out.println("File encrypted and saved to disk\n");

    fis = new FileInputStream("cipherTest.out");

    byte[] decrypted = new byte[dataInputSize];
    read(decrypted, fis);

    fis.close();
    String decryptedAsString = new String(decrypted);

    System.out.println("DECRYPTED FILE:\n" + decryptedAsString + "\n");

}

From source file:com.cisco.dvbu.ps.common.adapters.core.CisWsClient.java

public static void main(String[] args) throws Exception {
    if (args.length < 8) {
        System.err.println(//from  w  ww .  ja  v  a  2 s. c  o  m
                "usage: java com.cisco.dvbu.ps.common.adapters.core.CisWsClient endpointName endpointMethod configXml requestXml host port user password <domain>");
        System.exit(1);
    }
    org.apache.log4j.BasicConfigurator.configure();
    // DEBUG, INFO, ERROR
    org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.DEBUG);
    String endpointName = args[0]; // "server"
    String endpointMethod = args[1]; // "getServerAttributes"
    String adapterConfigPath = args[2]; // E:\dev\Workspaces\PDToolGitTest\PDTool_poc\resources\config\6.2.0\cis_adapter_config.xml
    String requestXMLPath = args[3]; // path to request xml

    Properties props = new Properties();
    props.setProperty(AdapterConstants.ADAPTER_HOST, args[4]);
    props.setProperty(AdapterConstants.ADAPTER_PORT, args[5]);
    props.setProperty(AdapterConstants.ADAPTER_USER, args[6]);
    props.setProperty(AdapterConstants.ADAPTER_PSWD, args[7]);
    if (args.length == 9)
        props.setProperty(AdapterConstants.ADAPTER_DOMAIN, args[8]);

    // Read the request xml file
    FileInputStream input = new FileInputStream(requestXMLPath);
    byte[] fileData = new byte[input.available()];
    input.read(fileData);
    input.close();
    String requestXml = new String(fileData, "UTF-8");

    // Execute the CIS Web Service Client for an adapter configuration
    CisWsClient cisclient = new CisWsClient(props, adapterConfigPath);

    System.out.println("Request: " + requestXml);
    // String requestXml = "<?xml version=\"1.0\"?> <p1:ServerAttributeModule xmlns:p1=\"http://www.dvbu.cisco.com/ps/deploytool/modules\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.dvbu.cisco.com/ps/deploytool/modules file:///e:/dev/Workspaces/PDToolGitTest/PDToolModules/schema/PDToolModules.xsd\">     <!--Element serverAttribute, maxOccurs=unbounded-->     <serverAttribute>         <id>sa1</id>         <name>/server/event/generation/sessions/sessionLoginFail</name>         <type>UNKNOWN</type>         <!--Element value is optional-->         <value>string</value>         <!--Element valueArray is optional-->         <valueArray>             <!--Element item is optional, maxOccurs=unbounded-->             <item>string</item>             <item>string</item>             <item>string</item>         </valueArray>         <!--Element valueList is optional-->         <valueList>             <!--Element item is optional, maxOccurs=unbounded-->             <item>                 <!--Element type is optional-->                 <type>UNKNOWN</type>                 <!--Element value is optional-->                 <value>string</value>             </item>             <item>                 <!--Element type is optional-->                 <type>UNKNOWN</type>                 <!--Element value is optional-->                 <value>string</value>             </item>             <item>                 <!--Element type is optional-->                 <type>UNKNOWN</type>                 <!--Element value is optional-->                 <value>string</value>             </item>         </valueList>         <!--Element valueMap is optional-->         <valueMap>             <!--Element entry is optional, maxOccurs=unbounded-->             <entry>                 <!--Element key is optional-->                 <key>                     <!--Element type is optional-->                     <type>UNKNOWN</type>                     <!--Element value is optional-->                     <value>string</value>                 </key>                 <!--Element value is optional-->                 <value>                     <!--Element type is optional-->                     <type>UNKNOWN</type>                     <!--Element value is optional-->                     <value>string</value>                 </value>             </entry>             <entry>                 <!--Element key is optional-->                 <key>                     <!--Element type is optional-->                     <type>UNKNOWN</type>                     <!--Element value is optional-->                     <value>string</value>                 </key>                 <!--Element value is optional-->                 <value>                     <!--Element type is optional-->                     <type>UNKNOWN</type>                     <!--Element value is optional-->                     <value>string</value>                 </value>             </entry>             <entry>                 <!--Element key is optional-->                 <key>                     <!--Element type is optional-->                     <type>UNKNOWN</type>                     <!--Element value is optional-->                     <value>string</value>                 </key>                 <!--Element value is optional-->                 <value>                     <!--Element type is optional-->                     <type>UNKNOWN</type>                     <!--Element value is optional-->                     <value>string</value>                 </value>             </entry>         </valueMap>     </serverAttribute> </p1:ServerAttributeModule>";
    String response = cisclient.sendRequest(endpointName, endpointMethod, requestXml);
    System.out.println("Response: " + response);
}

From source file:org.apache.nutch.parse.oo.OOParser.java

public static void main(String[] args) throws Exception {
    OOParser oo = new OOParser();
    Configuration conf = NutchConfiguration.create();
    oo.setConf(conf);/*from   w w  w . java2s .  c  o m*/
    FileInputStream fis = new FileInputStream(args[0]);
    byte[] bytes = new byte[fis.available()];
    fis.read(bytes);
    fis.close();
    Content c = new Content("local", "local", bytes, "application/vnd.oasis.opendocument.text", new Metadata(),
            conf);
    Parse p = oo.getParse(c).get(c.getUrl());
    System.out.println(p.getData());
    System.out.println("Text: '" + p.getText() + "'");
    /*
    // create the test output file
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("e:\\ootest.txt"), "UTF-8");
    osw.write(p.getText());
    osw.flush();
    osw.close();
    */
}

From source file:com.almende.eve.deploy.Boot.java

/**
 * The default agent booter. It takes an EVE yaml file and creates all
 * agents mentioned in the "agents" section.
 * /*from ww  w  .  java2  s  .c  om*/
 * @param args
 *            Single argument: args[0] -> Eve yaml
 */
public static void main(final String[] args) {
    if (args.length == 0) {
        LOG.warning("Missing argument pointing to yaml file:");
        LOG.warning("Usage: java -jar <jarfile> eve.yaml");
        return;
    }
    final ClassLoader cl = new ClassLoader() {
        @Override
        protected Class<?> findClass(final String name) throws ClassNotFoundException {
            Class<?> result = null;
            try {
                result = super.findClass(name);
            } catch (ClassNotFoundException cne) {
            }
            if (result == null) {
                FileInputStream fi = null;
                try {

                    String path = name.replace('.', '/');
                    fi = new FileInputStream(System.getProperty("user.dir") + "/" + path + ".class");
                    byte[] classBytes = new byte[fi.available()];
                    fi.read(classBytes);
                    fi.close();
                    return defineClass(name, classBytes, 0, classBytes.length);
                } catch (Exception e) {
                    LOG.log(Level.WARNING, "Failed to load class:", e);
                }
            }
            if (result == null) {
                throw new ClassNotFoundException(name);
            }
            return result;
        }
    };
    String configFileName = args[0];
    try {
        InputStream is = new FileInputStream(new File(configFileName));
        boot(is, cl);

    } catch (FileNotFoundException e) {
        LOG.log(Level.WARNING, "Couldn't find configfile:" + configFileName, e);
        return;
    }

}