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:Main.java

public static void main(String[] args) {
    File aFile = new File("c:/a.htm");
    System.out.println(aFile.length());

}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("C:/demo.txt");
    long fileSize = file.length();

    System.out.println(fileSize + " bytes");
    System.out.println((double) fileSize / 1024 + " KB");
    System.out.println((double) fileSize / (1024 * 1024) + "MB");
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("c:\\java.exe");

    long size = file.length();
    String display = FileUtils.byteCountToDisplaySize(size);

    System.out.println("Name    = " + file.getName());
    System.out.println("size    = " + size);
    System.out.println("Display = " + display);
}

From source file:MainClass.java

public static void main(String[] a) {
    File myFile = new File("C:" + File.separator);
    System.out.println(myFile.length());
}

From source file:com.skynetcomputing.skynettools.Main.java

/**
 * @param args the command line arguments
 *///from   w w w  .  ja  v  a 2  s .  c o  m
public static void main(String[] args) throws IOException {
    Scanner s = new Scanner(System.in);
    boolean isRunning = true;
    while (isRunning) {
        System.out.println("Enter file path for MD5 hash. Leave empty to quit");
        String input = s.nextLine();
        isRunning = !input.isEmpty();
        if (isRunning) {
            File inputFile = new File(input);
            if (inputFile.length() > 0) {
                try (FileInputStream fis = new FileInputStream(inputFile)) {
                    String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
                    System.out.println("MD5: " + md5);
                }
            }
        }
    }

}

From source file:CompressIt.java

public static void main(String[] args) {
    String filename = args[0];/*from w ww .  j a  v a2 s.  c  om*/
    try {
        File file = new File(filename);
        int length = (int) file.length();
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
        GZIPOutputStream gos = new GZIPOutputStream(baos);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = bis.read(buffer)) != -1) {
            gos.write(buffer, 0, bytesRead);
        }
        bis.close();
        gos.close();
        System.out.println("Input Length: " + length);
        System.out.println("Output Length: " + baos.size());
    } catch (FileNotFoundException e) {
        System.err.println("Invalid Filename");
    } catch (IOException e) {
        System.err.println("I/O Exception");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY);");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    File file = new File("yourFileName.txt");
    long fileLength = file.length();
    Reader fileReader = (Reader) new BufferedReader(new FileReader(file));

    pstmt.setCharacterStream(1, fileReader, (int) fileLength);

    int rowCount = pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getBytes(2));
    }//from ww  w.j a v  a 2 s . c  om

    rs.close();
    stmt.close();
    conn.close();
}

From source file:MainClass.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();

    String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";
    try {/*w w w  .  j a v  a  2s .  c om*/
        stmt.executeUpdate("DROP TABLE XML_Data");
    } catch (SQLException se) {
        if (se.getErrorCode() == 942)
            System.out.println("Error dropping XML_Data table:" + se.getMessage());
    }
    stmt.executeUpdate(streamingDataSql);

    File f = new File("employee.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)");
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100");
    if (rset.next()) {
        InputStream xmlInputStream = rset.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) {
    File f = new File("c:/test.txt");

    boolean bool = f.exists();

    if (bool) {/*www.  ja  v a  2  s . c  om*/
        long len = f.length();
        String path = f.getPath();
        System.out.print(path + " file length: " + len);
    }

}

From source file:DemoPreparedStatementSetCharacterStream.java

public static void main(String[] args) throws Exception {
    String fileName = "charDataFile.txt";
    Reader fileReader = null;/* www. j av  a 2s . c o m*/
    long fileLength = 0;
    Connection conn = null;
    PreparedStatement pstmt = null;

    try {
        File file = new File(fileName);
        fileLength = file.length();
        fileReader = (Reader) new BufferedReader(new FileReader(file));
        System.out.println("fileName=" + fileName);
        System.out.println("fileLength=" + fileLength);

        conn = getConnection();
        // begin transaction
        conn.setAutoCommit(false);
        // prepare SQL query for inserting a new row using SetCharacterStream()
        String query = "insert into char_stream_table (id, char_stream_column) values(?, ?)";
        // create PrepareStatement object
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, fileName);
        pstmt.setCharacterStream(2, fileReader, (int) fileLength);
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
        // end transaction
        conn.commit();
    } finally {
        pstmt.close();
        conn.close();
    }
}