Getting size of a file

Posted on 2005-05-05
Last Modified: 2010-03-31
Is there a way to get the size of a file object without actually reading the entire file?

If not, will reading the file and checking the length() of it provide me with an accurate size? How big is each char?

aString.length() * 2 //if chars are 16-bit?

or is it better to use:
byte[] array = aString.getBytes()
int sizeInBits = array.length

Question by:kristofer
    LVL 15

    Accepted Solution

    new File(xxxx).length() return the size without reading it.
    LVL 1

    Expert Comment

    aozarov is right I believe. From your question, I'm guessing there's some confusion on Java's data types. To summarise :

    Java's String / Char are Unicode encoded - 16 bits / character

    Text in common text files are either ASCII / ISO-8859 encoded - 8 bits / character

    Therefore size of file is very dependant on the encoding use for the text data.
    The text in file can be encoded in either way(most commonly). Typically most txt files are ASCII / ISO-8895 encoded.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now