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 Document read(String path) {
    Document doc = null;//  ww  w . j a va  2  s  .c om
    try {
        File file = new File(path);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        if (file.length() != 0) {
            doc = db.parse(file);
        } else {
            return null;
        }
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return doc;
}

From source file:Utils.java

public static byte[] readAsByteArray(File file) {
    return readAsByteArray(file, file.length());
}

From source file:Main.java

public static long getFolderSize(File f) {
    long size = 0;
    if (f.isDirectory()) {
        for (File file : f.listFiles()) {
            size += getFolderSize(file);
        }//from w w w . jav a2s.co m
    } else {
        size = f.length();
    }
    return size;
}

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

private static byte[] readBytes(File file) {
    FileInputStream in = null;// w  w  w . j  a v a2 s .c  om
    final byte buffer[] = new byte[(int) file.length()];
    try {
        in = new FileInputStream(file);
        in.read(buffer);
    } catch (final IOException e) {
        MSGS.format(ERROR_READING_FILE_1, file.toString(), e);
    }
    if (in != null) {
        try {
            in.close();
        } catch (final IOException e) {
            // fail quietly
        }
    }
    return buffer;
}

From source file:Main.java

public static void openNamedFile(String filename) {
    try {//from   w ww.ja  v a2s.  com
        File f = new File(filename);
        //   Log.e("kuinfa", "filename= " + filename);
        FileInputStream fis = new FileInputStream(f);

        long size = f.length();
        name = f.getName();
        patch = f.getParentFile().toString();
        DataInputStream dis = new DataInputStream(fis);
        byte[] b = new byte[(int) size];
        int length = dis.read(b, 0, (int) size);

        dis.close();
        fis.close();

        String ttt = new String(b, 0, length, "UTF-8");

        try {
            ttt = new String(ttt.getBytes(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

From source file:com.asakusafw.runtime.stage.optimizer.LibraryCopySuppressionConfigurator.java

static String selectLibraries(String libraries) {
    String minLibrary = null;/*from  w  ww.  ja  va 2s . co m*/
    long minSize = Long.MAX_VALUE;
    for (String library : libraries.split(",")) { //$NON-NLS-1$
        Path path = new Path(library);
        String scheme = path.toUri().getScheme();
        if (scheme != null && scheme.equals("file")) { //$NON-NLS-1$
            File file = new File(path.toUri());
            long size = file.length();
            if (size < minSize) {
                minLibrary = library;
                minSize = size;
            }
        }
    }
    if (minLibrary != null) {
        return minLibrary;
    }
    return libraries;
}

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Return the text content of a file/*from   ww w . ja  v  a2s  . c  o m*/
 */
public static String getText(File file) throws Exception {
    if (file != null && file.exists() && file.canRead()) {
        byte[] b = new byte[(int) file.length()];
        log.debug("Getting text from file: " + file);
        FileInputStream filestream = null;
        filestream = new FileInputStream(file);
        filestream.read(b);
        filestream.close();
        return new String(b);
    } else {
        throw new Exception("Could not read from file: " + file.getAbsolutePath());
    }
}

From source file:com.linkedin.pinot.core.segment.index.loader.LoaderUtils.java

/**
 * Write an index file to v3 format single index file and remove the old one.
 *
 * @param segmentWriter v3 format segment writer.
 * @param column column name./* w w w  .  j  av a 2  s  .  c  o m*/
 * @param indexFile index file to write from.
 * @param indexType index type.
 * @throws IOException
 */
public static void writeIndexToV3Format(SegmentDirectory.Writer segmentWriter, String column, File indexFile,
        ColumnIndexType indexType) throws IOException {
    int fileLength = (int) indexFile.length();
    PinotDataBuffer buffer = null;
    try {
        if (segmentWriter.hasIndexFor(column, indexType)) {
            // Index already exists, try to reuse it.

            buffer = segmentWriter.getIndexFor(column, indexType);
            if (buffer.size() != fileLength) {
                // Existed index size is not equal to index file size.
                // Throw exception to drop and re-download the segment.

                String failureMessage = "V3 format segment already has " + indexType + " for column: " + column
                        + " that cannot be removed.";
                LOGGER.error(failureMessage);
                throw new IllegalStateException(failureMessage);
            }
        } else {
            // Index does not exist, create a new buffer for that.

            buffer = segmentWriter.newIndexFor(column, indexType, fileLength);
        }

        buffer.readFrom(indexFile);
    } finally {
        FileUtils.deleteQuietly(indexFile);
        if (buffer != null) {
            buffer.close();
        }
    }
}

From source file:Main.java

/**
 * /*  w  w w .  j  a v a  2s . c  om*/
 * @param filename
 * @return
 */
public static long getFileSize(String filename) {
    File file = new File(filename);
    if (!file.exists() || !file.isFile()) {
        Log.e(TAG, "GETFILESIZE: File \"" + filename + "\" doesn\'t exist");
        return -1;
    }
    return (file.length() / 1024);
}

From source file:Main.java

/**
 * Rename existing file in same directory if target file exists, delete
 * Code nicked from http://stackoverflow.com/users/325442/mr-bungle
 * @param context//  w  w  w.ja v a2 s. c  o m
 * @param originalFileName
 * @param newFileName
 */
private static void rename(Context context, String originalFileName, String newFileName) {
    File originalFile = context.getFileStreamPath(originalFileName);
    if (originalFile.exists()) {
        Log.d("SavingHelper",
                "renaming " + originalFileName + " size " + originalFile.length() + " to " + newFileName);
        File newFile = new File(originalFile.getParent(), newFileName);
        if (newFile.exists()) {
            context.deleteFile(newFileName);
        }
        originalFile.renameTo(newFile);
    }
}