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.