Example usage for java.io File length

List of usage examples for java.io File length

Introduction

In this page you can find the example usage for java.io File length.

Prototype

public long length() 

Source Link

Document

Returns the length of the file denoted by this abstract pathname.

Usage

From source file:com.rabbitmq.examples.FileProducer.java

public static void main(String[] args) {
    Options options = new Options();
    options.addOption(new Option("h", "uri", true, "AMQP URI"));
    options.addOption(new Option("p", "port", true, "broker port"));
    options.addOption(new Option("t", "type", true, "exchange type"));
    options.addOption(new Option("e", "exchange", true, "exchange name"));
    options.addOption(new Option("k", "routing-key", true, "routing key"));

    CommandLineParser parser = new GnuParser();

    try {// www . j  a v  a 2s .  co  m
        CommandLine cmd = parser.parse(options, args);

        String uri = strArg(cmd, 'h', "amqp://localhost");
        String exchangeType = strArg(cmd, 't', "direct");
        String exchange = strArg(cmd, 'e', null);
        String routingKey = strArg(cmd, 'k', null);

        ConnectionFactory connFactory = new ConnectionFactory();
        connFactory.setUri(uri);
        Connection conn = connFactory.newConnection();

        final Channel ch = conn.createChannel();

        if (exchange == null) {
            System.err.println("Please supply exchange name to send to (-e)");
            System.exit(2);
        }
        if (routingKey == null) {
            System.err.println("Please supply routing key to send to (-k)");
            System.exit(2);
        }
        ch.exchangeDeclare(exchange, exchangeType);

        for (String filename : cmd.getArgs()) {
            System.out.print("Sending " + filename + "...");
            File f = new File(filename);
            FileInputStream i = new FileInputStream(f);
            byte[] body = new byte[(int) f.length()];
            i.read(body);
            i.close();

            Map<String, Object> headers = new HashMap<String, Object>();
            headers.put("filename", filename);
            headers.put("length", (int) f.length());
            BasicProperties props = new BasicProperties.Builder().headers(headers).build();
            ch.basicPublish(exchange, routingKey, props, body);
            System.out.println(" done.");
        }

        conn.close();
    } catch (Exception ex) {
        System.err.println("Main thread caught exception: " + ex);
        ex.printStackTrace();
        System.exit(1);
    }
}

From source file:SignatureTest.java

public static void main(String[] args) {
    try {//from   w ww.  j  av a  2  s  .  com
        if (args[0].equals("-genkeypair")) {
            KeyPairGenerator pairgen = KeyPairGenerator.getInstance("DSA");
            SecureRandom random = new SecureRandom();
            pairgen.initialize(KEYSIZE, random);
            KeyPair keyPair = pairgen.generateKeyPair();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(keyPair.getPublic());
            out.close();
            out = new ObjectOutputStream(new FileOutputStream(args[2]));
            out.writeObject(keyPair.getPrivate());
            out.close();
        } else if (args[0].equals("-sign")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            PrivateKey privkey = (PrivateKey) keyIn.readObject();
            keyIn.close();

            Signature signalg = Signature.getInstance("DSA");
            signalg.initSign(privkey);

            File infile = new File(args[1]);
            InputStream in = new FileInputStream(infile);
            int length = (int) infile.length();
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            signalg.update(message);
            byte[] signature = signalg.sign();

            DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
            int signlength = signature.length;
            out.writeInt(signlength);
            out.write(signature, 0, signlength);
            out.write(message, 0, length);
            out.close();
        } else if (args[0].equals("-verify")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[2]));
            PublicKey pubkey = (PublicKey) keyIn.readObject();
            keyIn.close();

            Signature verifyalg = Signature.getInstance("DSA");
            verifyalg.initVerify(pubkey);

            File infile = new File(args[1]);
            DataInputStream in = new DataInputStream(new FileInputStream(infile));
            int signlength = in.readInt();
            byte[] signature = new byte[signlength];
            in.read(signature, 0, signlength);

            int length = (int) infile.length() - signlength - 4;
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            verifyalg.update(message);
            if (!verifyalg.verify(signature))
                System.out.print("not ");
            System.out.println("verified");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");

    conn.setAutoCommit(false);// w w w . j a  va 2  s  .c o  m

    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE MANUALS(GAMEID INT, MANUAL LONG VARCHAR)");
    conn.commit();

    File file = new File("manuals.xml");
    InputStream is = new FileInputStream(file);

    PreparedStatement ps = conn.prepareStatement("INSERT INTO MANUALS VALUES(?,?)");

    ps.setInt(1, 1285757);

    ps.setAsciiStream(2, is, (int) file.length());

    ps.execute();
    conn.commit();
}

From source file:InsertPictureToMySql.java

public static void main(String[] args) throws Exception, IOException, SQLException {
    Class.forName("org.gjt.mm.mysql.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root");
    String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)";

    FileInputStream fis = null;//from  www. ja  v a 2s  . c  o m
    PreparedStatement ps = null;
    try {
        conn.setAutoCommit(false);
        File file = new File("myPhoto.png");
        fis = new FileInputStream(file);
        ps = conn.prepareStatement(INSERT_PICTURE);
        ps.setString(1, "001");
        ps.setString(2, "name");
        ps.setBinaryStream(3, fis, (int) file.length());
        ps.executeUpdate();
        conn.commit();
    } finally {
        ps.close();
        fis.close();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "javatutorial";
    String userName = "root";
    String password = "root";

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url + dbName, userName, password);
    File imgfile = new File("images.jpg");
    FileInputStream fin = new FileInputStream(imgfile);
    PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
    pre.setInt(1, 5);// w ww . j a v a  2  s.co  m
    pre.setString(2, "A");
    pre.setBinaryStream(3, fin, (int) imgfile.length());
    pre.executeUpdate();
    pre.close();
    con.close();
}

From source file:bear.plugins.java.JenkinsCacheTest.java

public static void main(String[] args) {
    //        Optional<JavaPlugin.JDKFile> optional = JavaPlugin.JenkinsCache.load(new File("jenkins-temp.json"),
    //            "http://ftp-nyc.osuosl.org/pub/jenkins/updates/updates/hudson.tools.JDKInstaller.json",
    //            "7u51");
    ///*from   w  ww .j a v  a  2 s .  c o m*/
    //        System.out.println(optional.get());

    File file = JenkinsCache.download("7u51", new File("jenkins-temp.json"), new File(".bear"),
            "http://ftp-nyc.osuosl.org/pub/jenkins/updates/updates/hudson.tools.JDKInstaller.json",
            "chaschev@gmail.com", "**");

    System.out.println(file.length());
}

From source file:controller.tempClass.java

/**
 * @desc Image manipulation - Conversion
 *
 * @filename ImageManipulation.java/* ww w  .  j av  a  2s.  c o m*/
 * @author Jeevanandam M. (jeeva@myjeeva.com)
 * @copyright myjeeva.com
 */
public static void main(String[] args) {

    File file = new File("E:\\Photograph\\Temporary\\mshien.jpg");

    try {
        // Reading a Image file from file system
        FileInputStream imageInFile = new FileInputStream(file);
        byte imageData[] = new byte[(int) file.length()];
        imageInFile.read(imageData);

        // Converting Image byte array into Base64 String
        System.out.println(encodeImage(imageData));
        String imageDataString = encodeImage(imageData);

        // Converting a Base64 String into Image byte array
        byte[] imageByteArray = decodeImage(imageDataString);

        // Write a image byte array into file system
        FileOutputStream imageOutFile = new FileOutputStream("C:\\Users\\doanvanthien\\Desktop\\LOGO.png");

        imageOutFile.write(imageByteArray);

        imageInFile.close();
        imageOutFile.close();

        System.out.println("Image Successfully Manipulated!");
    } catch (FileNotFoundException e) {
        System.out.println("Image not found" + e);
    } catch (IOException ioe) {
        System.out.println("Exception while reading the Image " + ioe);
    }
}

From source file:Main.java

public static void main(String[] args) {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");
    if (!file1.exists() || !file2.exists()) {
        System.out.println("One or both files do not exist");
        System.out.println(false);
    }/*from  ww  w  .ja  va 2 s  . co m*/
    System.out.println("length:" + file1.length());
    if (file1.length() != file2.length()) {
        System.out.println("lengths not equal");
        System.out.println(false);
    }
    try {
        FileInputStream fis1 = new FileInputStream(file1);
        FileInputStream fis2 = new FileInputStream(file2);
        int i1 = fis1.read();
        int i2 = fis2.read();
        while (i1 != -1) {
            if (i1 != i2) {
                areFilesIdentical = false;
                break;
            }
            i1 = fis1.read();
            i2 = fis2.read();
        }
        fis1.close();
        fis2.close();
    } catch (IOException e) {
        System.out.println("IO exception");
        areFilesIdentical = false;
    }
    System.out.println(areFilesIdentical);
}

From source file:info.reborncraft.ImageManipulation.java

/**
 * @param args//ww w .j  a v a 2 s.c  om
 */
public static void main(String[] args) {
    File file = new File("server-icon.png");

    try {
        /*
         * Reading a Image file from file system
         */
        FileInputStream imageInFile = new FileInputStream(file);
        byte imageData[] = new byte[(int) file.length()];
        imageInFile.read(imageData);
        imageInFile.close();
        /*
         * Converting Image byte array into Base64 String
         */
        String imageDataString = encodeImage(imageData);

        System.out.println(imageDataString);
    } catch (FileNotFoundException e) {
        System.out.println("Image not found" + e);
    } catch (IOException ioe) {
        System.out.println("Exception while reading the Image " + ioe);
    }

}

From source file:Main.java

public static void main(String[] args) {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");

    if (!file1.exists() || !file2.exists()) {
        System.out.println("One or both files do not exist");
        System.out.println(false);
    }//  w  w  w .  j a v a  2s .c  om

    System.out.println("length:" + file1.length());
    if (file1.length() != file2.length()) {
        System.out.println("lengths not equal");
        System.out.println(false);
    }

    try {
        FileInputStream fis1 = new FileInputStream(file1);
        FileInputStream fis2 = new FileInputStream(file2);
        int i1 = fis1.read();
        int i2 = fis2.read();
        while (i1 != -1) {
            if (i1 != i2) {
                areFilesIdentical = false;
                break;
            }
            i1 = fis1.read();
            i2 = fis2.read();
        }
        fis1.close();
        fis2.close();
    } catch (IOException e) {
        System.out.println("IO exception");
        areFilesIdentical = false;
    }
    System.out.println(areFilesIdentical);
}