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:org.vuphone.vandyupon.test.ImagePostTester.java

public static void main(String[] args) {
    try {/*  w  w w. ja v  a  2  s . c  o m*/
        File image = new File("2008-05-8 ChrisMikeNinaGrad.jpg");
        byte[] bytes = new byte[(int) image.length()];
        FileInputStream fis = new FileInputStream(image);

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        fis.close();

        HttpClient c = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "http://afrl-gift.dre.vanderbilt.edu:8080/vandyupon/events/?type=eventimagepost&eventid=2"
                        + "&time=" + System.currentTimeMillis() + "&resp=xml");

        post.addHeader("Content-Type", "image/jpeg");
        ByteArrayEntity bae = new ByteArrayEntity(bytes);
        post.setEntity(bae);

        try {
            HttpResponse resp = c.execute(post);
            resp.getEntity().writeTo(System.out);
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket socket = new Socket("localhost", 8000);
    File file = new File("C:/Users/abc/Desktop/image.jpg");
    FileInputStream fileInputStream = new FileInputStream(file);

    byte[] fileBytes = new byte[(int) file.length()];
    OutputStream outputStream = socket.getOutputStream();
    int content;/*from   w  w  w.  j ava2 s  .  com*/
    while ((content = fileInputStream.read(fileBytes)) != -1) {
        outputStream.write(fileBytes, 0, (int) file.length());
    }

    System.out.println("file size is " + fileBytes.length);
    for (byte a : fileBytes) {
        System.out.println(a);
    }
    socket.close();
    fileInputStream.close();
}

From source file:dk.statsbiblioteket.jpar2.filecompare.FileCompare.java

/**
 * Main method. //  w w  w  .  j ava  2s .c o  m
 * @param args
 */
public static void main(String[] args) {

    Options options = new Options();
    options.addOption("s", "slices", true, "The number of slices to use in the comparison");

    CommandLineParser parser = new PosixParser();
    try {
        CommandLine cmd = parser.parse(options, args);
        args = cmd.getArgs();
        if (!cmd.hasOption("s") || args.length != 2) {
            System.exit(2);
        }
        int slices = Integer.parseInt(cmd.getOptionValue("s").trim());

        File f1 = new File(args[0]);
        File f2 = new File(args[1]);
        if (f1.length() == f2.length()) {
            int sliceSize = (int) (f1.length() / slices);//rounding here...

            DataFile df1 = new DataFile(f1, sliceSize);
            DataFile df2 = new DataFile(f2, sliceSize);

            List<Integer> defectIndexes = df1.compareWithIndex(df2);

            for (int index : defectIndexes) {
                System.out.println("index " + index + ", from " + index * sliceSize + " to "
                        + (index + 1) * sliceSize + " is defect");
            }
            if (defectIndexes.size() == 0) {
                System.out.println("Files are identical");
            }

        } else {
            System.out.println("Files differ in length, cannot help you");
        }
    } catch (ParseException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

}

From source file:UnbufferedPost.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Usage: ChunkEncodedPost <file>");
        System.out.println("<file> - full path to a file to be posted");
        System.exit(1);//from w ww .  j av a2  s.  c  om
    }
    HttpClient client = new HttpClient();

    PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");

    File file = new File(args[0]);
    httppost.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file), file.length()));

    try {
        client.executeMethod(httppost);

        if (httppost.getStatusCode() == HttpStatus.SC_OK) {
            System.out.println(httppost.getResponseBodyAsString());
        } else {
            System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
        }
    } finally {
        httppost.releaseConnection();
    }
}

From source file:com.discursive.jccook.httpclient.PostFileExample.java

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

    // Configure Logging
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");

    HttpClient client = new HttpClient();

    // Create POST method
    String weblintURL = "http://ats.nist.gov/cgi-bin/cgi.tcl/echo.cgi";
    PostMethod method = new PostMethod(weblintURL);

    File file = new File("project.xml");
    method.setRequestBody(new FileInputStream(file));
    method.setRequestContentLength((long) file.length());

    // Execute and print response
    client.executeMethod(method);/* w w w  .  j a  va 2  s .c o m*/
    String response = method.getResponseBodyAsString();
    System.out.println(response);

    method.releaseConnection();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from   w w  w.  ja  va2s .c  om*/

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    String sql = "INSERT INTO mysql_all_table(" + "col_boolean," + "col_byte," + "col_short," + "col_int,"
            + "col_long," + "col_float," + "col_double," + "col_bigdecimal," + "col_string," + "col_date,"
            + "col_time," + "col_timestamp," + "col_asciistream," + "col_binarystream," + "col_blob) "
            + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    pstmt.setBoolean(1, true);
    pstmt.setByte(2, (byte) 123);
    pstmt.setShort(3, (short) 123);
    pstmt.setInt(4, 123);
    pstmt.setLong(5, 123L);
    pstmt.setFloat(6, 1.23F);
    pstmt.setDouble(7, 1.23D);
    pstmt.setBigDecimal(8, new BigDecimal(1.23));
    pstmt.setString(9, "a string");
    pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis()));
    pstmt.setTime(11, new Time(System.currentTimeMillis()));
    pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis()));

    File file = new File("infilename1");
    FileInputStream is = new FileInputStream(file);
    pstmt.setAsciiStream(13, is, (int) file.length());

    file = new File("infilename2");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(14, is, (int) file.length());

    file = new File("infilename3");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(15, is, (int) file.length());

    pstmt.executeUpdate();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File f = new File("filename");
    RandomAccessFile raf = new RandomAccessFile(f, "rw");

    // Read a character
    char ch = raf.readChar();

    // Seek to end of file
    raf.seek(f.length());

    // Append to the end
    raf.writeChars("aString");
    raf.close();//from   ww w.j a va  2  s . com
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);/*from   w  ww  .j  a v a  2  s  .  co  m*/

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob(3);

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);//from w ww . j a v a2s  . com

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob("myClobColumn");

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}

From source file:fitnesse.runner.FormattingOption.java

public static void main(String[] args) throws Exception {
    FormattingOption option = new FormattingOption();
    option.args(args);/*from  w ww.j av  a2  s. c  om*/
    File inputFile = new File(option.resultFilename);
    FileInputStream input = new FileInputStream(inputFile);
    int byteCount = (int) inputFile.length();
    option.process(input, byteCount);
}