The Artima Developer Community
Sponsored Link

Java Answers Forum
Size of files in Java

12 replies on 1 page. Most recent reply: Apr 9, 2008 2:36 PM by Jamie Smith

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 12 replies on 1 page
Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Size of files in Java Posted: Apr 6, 2008 4:09 AM
Reply to this message Reply
Advertisement
Just a trivial question but I am very new to Java.

I have the following class:

public class DiskUsage
{

public static void main(String[] args)
{

if (args.length == 1)
{
FileUtilities.diskUsage(args[0]);
}
else
{
System.out.println("This is not a directory or there is not a directory in this location.");
}


/* set type */ size = FileUtilities.diskUsage(args[0]);
System.out.println("The total size of " + args[0] + " is " + size);
}
}

How do I output the size of the files in total? Where it says /* set type */ I have changed that to int but it says incompatible types.

I have another class but I don't think that is relevant to this problem.


Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Size of files in Java Posted: Apr 7, 2008 6:48 AM
Reply to this message Reply
In the definition of FileUtilities.diskUsage() it will say what type it returns. That is the type you should use.

Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Re: Size of files in Java Posted: Apr 7, 2008 7:20 AM
Reply to this message Reply
This is the FileUtilities class:

import java.io.*;
import java.util.*;
import java.io.File;

public class FileUtilities
{
public static void diskUsage(File f)
{

if (f.isDirectory())
{
File[] fileList = f.listFiles();

for (int i = 0; i < fileList.length; ++i)
{
diskUsage(fileList);
}
}
else
{
System.out.println(f);
}
}

public static void diskUsage(String s)
{
diskUsage(new File(s));
}
}

I have tried setting the type to int and File but they are incompatible. :s

Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Size of files in Java Posted: Apr 7, 2008 8:05 AM
Reply to this message Reply
Oh, you’re being taught recursion; marvellous.

Your diskUsage() doesn’t find the size of any files (hint: java.io.File has a length() method — which type does it return?); doesn’t keep a running total of disk space used; doesn’t attempt to return any answer to its caller. You need to fix all these things.

Oh, and next time you post code here, post it in java tags; that way it will display properly. http://www.artima.com/forums/howtopost.html

Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Re: Size of files in Java Posted: Apr 7, 2008 8:54 AM
Reply to this message Reply
so I need to set size to be the length of args[0]?

args[0].length ?

Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Size of files in Java Posted: Apr 7, 2008 9:33 AM
Reply to this message Reply
Is args[0] a java.io.File?

Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Re: Size of files in Java Posted: Apr 7, 2008 10:49 AM
Reply to this message Reply
No it isn't...that's just the arguments passed to the command line isn't it...

Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Size of files in Java Posted: Apr 7, 2008 10:55 AM
Reply to this message Reply
But you do have an object of type File, to which you can apply the .length() method.

I’m really not going to spell everything out for you. You have to think for yourself if you are gong to learn.

Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Re: Size of files in Java Posted: Apr 7, 2008 12:03 PM
Reply to this message Reply
fileList is of File[] type.

so...

fileList.length = FileUtilities.diskUsage ...

Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Re: Size of files in Java Posted: Apr 9, 2008 2:20 AM
Reply to this message Reply
doesn't work...

Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Size of files in Java Posted: Apr 9, 2008 7:33 AM
Reply to this message Reply
fileList is of type File[], not of type File. Hint: Where do you get fileList from?

Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Re: Size of files in Java Posted: Apr 9, 2008 10:57 AM
Reply to this message Reply
Ohh my bad, was looking at the wrong thing.

File f is of type file.

fileList comes from FileUtilities class

Jamie Smith

Posts: 8
Nickname: aurora88
Registered: Apr, 2008

Re: Size of files in Java Posted: Apr 9, 2008 2:36 PM
Reply to this message Reply
I've tried setting long size = f.length(); in the FileUtilities class. Also, when I run this application now, it displays every folder and file twice. :s

Flat View: This topic has 12 replies on 1 page
Topic: Size of files in Java Previous Topic   Next Topic Topic: arrays and final outcome

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use