Search code examples
javastax

How to get filesize of file generated using XMLStreamWriter?


I am using XMLStreamWriter class from StAX to generate XML file from a java program. I followed this tutorial and the XMLFile is getting generated correctly. How do I get the size of the file that has been generated ?


Solution

  • Using the method File#length()

    Returns: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.

    File out = new File("data\\output2.xml");
    System.out.printf("file size: %d bytes%n", out.length());
    

    With java 1.7 you can use the static method Files#size (note: Files)

    Path path = Paths.get("data\\output2.xml");
    System.out.printf("file size: %d bytes%n", Files.size(path));