• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to calculate total directory sizes after reading from a text file  RSS feed

 
Atish Panghate
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following input file:

root, directory, 128, admin, NONE
users, directory, 512, admin, root
navin, directory, 1024, navin, users
navin.jpg, photo, 128000, navin, navin
Project.doc, document, 256000, navin, navin
Sholay.mpg, video, 4123456123, navin, navin
amit, directory, 128, amit, users
Resume.doc, document, 256000, amit, amit


This contains information about files stored in a file system. Each line corresponds to one file, and the fields are separated by commas. The first field contains the filename, the second contains the file type, the third field is the size of the file in bytes, the fourth field is the username of the owner of the file, and the last field is the name of the parent directory of this file (i.e. the name of the directory in which this file is located.) Note: the special parent directory name NONE indicates that this file is the root directory of the filesystem. Also, for the purposes of this program, assume that all file/directory names are unique in the system.

You have to write a Java program which reads data in this format from a file called input.txt, parses it, and figures out the total size of storage consumed by each directory in this system. The size of storage consumed by any directory is defined as the sum of the size of this directory, sizes of all the files in this directory and the total storage sizes consumed by all the directories in this directory. Your program should write the name of each directory, and the total storage consumed by it to a file called output.txt. The output should have one directory per line, and the format should be dirname: size.

Thus, if the input above were placed in a file called input.txt in the same directory as your program, then your program should create a file called output.txt in the same directory, and output.txt should contain the following:

navin: 4123841147
amit: 256128
root: 4124097915
users: 4124097787


I need to write a program in Java to accomplish above task. I have tried as below.

import java.io.*;

public class TotalDirectorySize {
public static void main(String[] args) {

BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader("C:\\input.txt"));

while ((sCurrentLine = br.readLine()) != null) {
System.out.println("Row values from file" + " " + sCurrentLine);
String[] values = sCurrentLine.split(",");
for (String val : values)
System.out.println("Values are" + " " + val);
createDirectory(values);

}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

}


Here I am parsing the file and reading values. Now am thinking of creating a object with all the 5 values from each line in file. But do I need an in-memory file structure to be created. Please suggest.
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to get the real size of a real directory? If so, I suggest you update to NIO2 and look for the file metadata section. Also update your exception handling and use try‑with‑resources.
Or are you trying to read the integers in that text file and add them together? Are you simply finding the numbers which appear to be the third token in each line, or are you creating a MyFile class and object with those data in?

Please use code tags because unformatted code is difficult to read.
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find a Scanner easier to use for those data.
Any type of parsing of that input file depends on the structure of the input being predictable and consistent; if the commas are in the wrong place the whole thing will fail dreadfully.
 
Atish Panghate
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create an in memory tree structure with directory names provided in the file but not sure how to create it. Also I may need to create a filesystem with directory in files and creates files with length mentioned may be by using random access file class. Please let me know suitable way to get started
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why you need a tree for that data. An ordinary map should be sufficient, with key = directory name and value = sum of file sizes.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't see why you need a tree for that data. An ordinary map should be sufficient, with key = directory name and value = sum of file sizes.

Seems like you'd need some knowledge of the tree structure because you need both the size of a child directory and the parent directory. The size of a document would need to be propagated up through the chain of parent directories adding its size to each one.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Seems like you'd need some knowledge of the tree structure because you need both the size of a child directory and the parent directory. The size of a document would need to be propagated up through the chain of parent directories adding its size to each one.


Well, the example doesn't show any child directories. That's the problem with coding based on examples instead of specifications, of course. So we don't know how that's going to be done in an input file, if it's going to be done at all. But I still don't think you need a tree; you only need to propagate the child directory's size up to each of its ancestors as you find out what it is. You would need a more complex value object then what I originally posted in my original 30-second answer, though.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Carey Brown wrote:Seems like you'd need some knowledge of the tree structure because you need both the size of a child directory and the parent directory. The size of a document would need to be propagated up through the chain of parent directories adding its size to each one.


Well, the example doesn't show any child directories. That's the problem with coding based on examples instead of specifications, of course. So we don't know how that's going to be done in an input file, if it's going to be done at all. But I still don't think you need a tree; you only need to propagate the child directory's size up to each of its ancestors as you find out what it is. You would need a more complex value object then what I originally posted in my original 30-second answer, though.

root, directory, 128, admin, NONE
users, directory, 512, admin, root
navin, directory, 1024, navin, users
navin.jpg, photo, 128000, navin, navin
Project.doc, document, 256000, navin, navin
Sholay.mpg, video, 4123456123, navin, navin
amit, directory, 128, amit, users
Resume.doc, document, 256000, amit, amit

users is a child of root
navin is a child of users
amit is a child of users
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess those are supposed to be child directories. I guess I'm stuck here in real life where directories in different hierarchies can have the same name, which would make that format useless. But sorry for my bad assumptions.

I see that the child directories always occur after their parents; can we assume that's one of the rules of formation for these files? Or is that a bad assumption as well? If it's a correct one, then you can still build a map and increment relevant entries as you read from the file. If it's not... well, I've written code to build a tree when you receive its nodes in random order and I can tell you it's not fun at all.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!