I need to write a Java program which reads data 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. The program should write the name of each directory, and the total storage consumed by it to a file called output.txt.
Consider the following input file:
Name | Type | Size | Owner | Directory
root , directory , 128 , admin , NONE
users , directory , 512 , admin , root
navin , directory , 1024 , navin , users
navin.jpg , photo , 128000 , navin , navin
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.
I have tried below code:
package com.threads;
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 trying to fetch five values in an array and want to create specific directory based on the last value in array. I am thinking of creating an in-memory structure but not sure how to use data structures java. Please suggest a good method to do it.
Related
I saved a File.properties in this.getFilesDir() + "Data.propertie".
In the app, I save the data that the user wrote, but when i open the app, all the data (or the file) that I saves from the previous time has been deleted.
Example:
// Store
for (Map.Entry<String,String> entry : MainActivity.Notes.entrySet()) { // Put all data from Notes in properties to store later
properties.put(entry.getKey(), entry.getValue());
}
try { properties.store(new FileOutputStream(this.getFilesDir() + "data.properties"), null); } // Store the data
catch (IOException e) { e.printStackTrace(); } // Error exception
// Load the map
Properties properties = new Properties(); // Crate properties object to store the data
try {
properties.load(new FileInputStream(this.getFilesDir() + "data.proprties")); } // Try to load the map from the file
catch (IOException e) { e.printStackTrace(); } // Error exception
for (String key : properties.stringPropertyNames()) {
Notes.put(key, properties.get(key).toString()); // Put all data from properties in the Notes map
}
// Can load the data
You can see that i saved the data in the file, and I can load it, but when I open the app, the data has been deleted
There is a way to write in the file and save the data that i write to the next time I open the app?
First off Context.getFilesDir returns a File object. This File represents the private data directory of your app.
Then you do getFilesDir() + "foo" which will implicitly call toString() on the File instance. File.toString() is equivalent to File.getPath which does not necessarily return a trailing slash.
This means, if getFilesDir() returns a File at /data/app, your properties file will become /data/appdata.properties which is not in your data Folder and cannot be written to.
Instead, to get a File within a directory you can create a new File instance with that directory. e.g:
File propertiesFile = new File(this.getFilesDir(), "data.properties");
// use propertiesFile for FileOutputStream/FileInputStream etc.
This ensures that your properties file is within that directory and prevents any issues from file separators
We are currently using the EasyUpload add-on, and we have specified the criteria for this component:
a) only CSV files are allowed, with a cap size of 1MB per file.
b) only one file can be submitted at a time.
We just did an uploading test on small-sized CSV files that are below 100Kb. Usually, the upload process completes successfully. Occasionally, the error of "Could not open file, The system cannot find the file specified" is displayed although the file is already inside the temp folder, and we found that this happens either when:
a) If the same file is uploaded again after making a small change and within a few seconds after the file has been uploaded successfully.
b) If there are two tabs of the web app, logged under different users were uploading their respective csv files and they also do the same thing of changing values in the csv before uploading them again.
We tried forcing the file upload through (as another testing method) and noticed after a while that the files sometimes get stuck in the queue although we have imposed a one file at a submission time rule. It was displayed in a message "There are too many files over the count limit". We also considered of putting a sleep / wait command of 3-5 seconds after the file submission.
MultiFileUpload multiFileUpload = new MultiFileUpload() {
#Override
protected void handleFile(File tmpFile, String fileName, String mimeType, long length) {
String[] header = {"EOD_NUM","OUTLET_NAME","POSM_NAME","EOD_DATE","TOTAL_SALES","GROSS_SALES",
"TRAN_COUNT","VOID_COUNT","SERVICE_CHARGE","DISCOUNT_AMT","VAT_TAX_AMT","SVC_TAX_AMT","ROUNDING_ADJ"};
uploadLogger.debug("File: " + tmpFile.getAbsolutePath());
uploadLogger.debug("FileName: " + fileName);
uploadLogger.debug("MimeType: " + mimeType);
uploadLogger.debug("File Length: " + length);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
LocalDateTime now = LocalDateTime.now();
File f2 = null;
f2 = new File(busId+"_"+dtf.format(now)+".csv");
tmpFile.renameTo(f2);
try {
///var/lib/tomcat8/ in linux
///D:\\home\\site\\wwwroot\\ in Windows
uploadLogger.debug("f2 absolutepath: " + f2.getAbsolutePath());
uploadLogger.debug("f2 canonical path: " + f2.getCanonicalPath());
CloudBlockBlob blob = container.getBlockBlobReference(f2.getName());
if(f2.length() > 0){
blob.uploadFromFile(f2.getAbsolutePath());
Notification.show("File upload completed.",Notification.Type.TRAY_NOTIFICATION);
}
CSVReader reader = new CSVReader(new FileReader(f2.getAbsolutePath()), ',' , '"' , 0);
//read header name
//String[] myheader = reader.readNext();
//NOTE :: Store all row and column from csv info List of String Array
myEntries = reader.readAll();
if (myEntries != null && !myEntries.isEmpty()) {
boolean success = uploadDAO.insertUploaderEntry(myEntries,busId, userId,"");
uploadLogger.debug("SUCCESSS??? " + success);
if(success){
Notification successNotify = new Notification("Record has been created successfully.","Upload Successful!");
successNotify.setDelayMsec(3000);
successNotify.setStyleName(ValoTheme.NOTIFICATION_SUCCESS);
successNotify.setPosition(Position.MIDDLE_CENTER);
successNotify.show(Page.getCurrent());
}else {
Notification.show("Error in submitting uploaded record.","Upload failed!"
, Notification.Type.ERROR_MESSAGE).setDelayMsec(3000);
}
Thread.sleep(3000); //added to see if the delay solves the problem or not.
}
} catch (URISyntaxException | StorageException | IOException ex) {
new Notification("Could not open file",ex.getMessage(),Notification.Type.ERROR_MESSAGE).show(Page.getCurrent());
uploadLogger.debug(ex);
} catch (InterruptedException ix) {
uploadLogger.debug("Interrupted Exception found: " + ix.getMessage());
}
}
#Override
protected boolean supportsFileDrops() {
return false;
}
};
multiFileUpload.setMaxFileCount(1);
multiFileUpload.setUploadButtonCaption("Upload CSV file here");
multiFileUpload.setMaxFileSize(fileSizeLimit); // 2MB
multiFileUpload.setAcceptFilter(".csv");
We are unsure whether if this problem is a known limitation of the component or not.
Some of the questions we have discovered along the way are:
a) Is there a better way or to control on the file uploading and to avoid the open file / file not found error?
b) Are the values in the setAcceptedFilter method the mime/type values or something else. We noticed for images, it's "images/*" but for csv, we had to put in as ".csv"
Answering to your second question. The acceptFilter is directly passed to upload inputs "accept" attribute, so both .csv and text/csv should do fine. See https://www.w3schools.com/tags/att_input_accept.asp for more instructions.
Has anybody used the ftp.changeWorkingDirectory command for mainframe datasets. This command returns true when there is a dataset. It also returns true when there is no dataset. Can you share any sample java ftp code if you have any that you use to deal with mainframe datasets. Here is the code I have that does not work.
public static FTPFile[] GetListOfMembersFromPDS(String pdsname, String serverN) {
FTPSClient ftp = FTPConnect(serverN);
FTPFile[] files = null;
boolean success = false;
try {
success = ftp.changeWorkingDirectory(pdsname);
if (!success) {
throw new IOException("Unable to change working directory to " + pdsname
+ " for ftp transfer with ftp client = " + ftp + ". " + ftp.getReplyString());
}
} catch (IOException e) {
e.printStackTrace();`enter code here`
}
System.out.println(success);
try {
files = ftp.listFiles();
} catch (IOException e) {
e.printStackTrace();
}
return files;
}
Changing the working directory in a z/OS dataset environment will generally return true, unless you specify a qualifier that is too long, or the whole name is too long. That does not indicated that the working directory exists or not, as mainframe datasets don't work on a directory structure.
If you change to a "working directory" that doesn't exist yet, you will get this response:
250 "THIS.IS.THE.WORKING.DIRECTOR." is the working directory name prefix.
If you try with a name that is invalid, you'll get false.
501 A qualifier in "THIS.IS.THE.WORKING.DIRECTORY" is more than 8 characters
If you try with a name that is too long, you'll get false.
501 Invalid directory name - too large.
So relying on the boolean from changeWorking Directory won't work. You'll need to look at the reply string with the "getReplyString()" method and then parse it.
A loadlib PDS will say something like
250-The working directory may be a load library
250 The working directory "GAME.LOADLIB" is a partitioned data set
A non-loadlib PDS will just say
250 The working directory "GAME.COBOL" is a partitioned data set.
listFiles, listDirectories and ListNames have different behaviours too.
I have a number of datasets which are GAME.SRCE, GAME.COPYLIB, GAME.LOADLIB and GAME.LOADLIB.PDSE. All of them are partitioned datasets. If you change directory to GAME, and do listNames(), you get all 4. If you listFiles or listDirectories, you ONLY get the COPYLIB and the SRCE. If you change directory to GAME.SRCE, then all three methods will list all the members. If you change directory to GAME.LOADLIB, then ONLY listNames will list the members. You also do not know that GAME.LOADLIB.PDSE exists.
This was done using Apache Commons, commons-net-3.6.jar and z/OS.
Here's a simple example I found by searching. You should see success=false if the directory can't be changed:
// Change working directory
success = ftp.changeWorkingDirectory(pdsname);
String[] replies = ftp.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER: " + aReply);
}
}
if (success) {
System.out.println("Successfully changed working directory.");
} else {
System.out.println("Failed to change working directory to " + pdsname + ". See server's reply.");
}
Ok I am having a HUGE problem. I"m a Senior in College and this is my Senior project I'm working on in order to graduate so your help will be GREATLY appreciated.
I am having a problem with a scanner reading a file. Here is my method.
//This sets the text file to scan which is the List of all Employees ID number and Name only
try {
File file = new File("ListemployeeIDandName.txt");
scan = new Scanner(file);
}
catch (Exception e)
{
System.out.println("Could not find file: "+" ListemployeeIDandName.txt");
}
//First I need to get each employees name so this loop cycles through the file and loads the ID and name into a List
//Odd indexes will be the name of employee and Even will be the ID number
while(scan.hasNext())
{
Listemployeenames.add(scan.next());
}
//scan.reset();
//scan.close();
scanfile = scan.reset();
//Now that I have the List of all employees ID and names...I just want the names so I can search for the respective files
//This method will make a temp list of ONLY employee names by making a list of only the ODD indexes which are the names
LinkedList<String> List_names = new LinkedList<String>();
for(int y = 0;y<Listemployeenames.size();y++)
{
//this means if y is an even number than do nothing otherwise if its ODD add the index to the list
if((y%2) == 0)
{
}
else
{
List_names.add(Listemployeenames.get(y));
}
}
for(int z = 0;z<List_names.size();z++)
{
System.out.println(List_names.get(z));
}
//OK so now that I have the names of all the employees I need to use their names to find their file and load all their info to a the main list
String filepath = "";
for(int a = 0;a<List_names.size();a++)
{
//Ok so this will grab each bio file and send to the Employee Constructor which will set all the data from the file
try {
filepath = List_names.get(a)+"bio.txt";
File file2 = new File(filepath);
scanfile = new Scanner(file2);
}
catch (FileNotFoundException e)
{
System.out.println("Could not find file: "+filepath);
}
finally{
//scanfile.close();
//scan.reset();
}
//this method sends the scanner file to that employee and sets their data
List_AllEmployees.add(new Employee(scanfile));
}
Now this method READS from 2 files...and THEY DO EXIST the first file at the top of the method is working fine ...but for some reason the 2nd file cannot be found even though it does exist.
Now here is where it gets wierd...soo...both files are in the java default...so I tried something wierd...since I know the second files do exist but for some reason scanner can't find them i decided to simply use file explorer and go to the directory where it is and cut and paste it to another directory then immediatly cut and paste it back into the java default directory...i ran the method again AND THEN IT WORKS...
My question is wtf? Why did java scanner recognize the file after I simply cut and pasted it back into its default directory? and how come the first file is working with no problem but the 2nd file needs to manually be placed into the default folder? please help this is my first time using this site so I don't know how to paste my code other than in here. Thanks for your help.
This is my code to read a text file. When I run this code, the output keeps saying "File not found.", which is the message of FileNotFoundException. I'm not sure what is the problem in this code.
Apparently this is part of the java. For the whole java file, it requires the user to input something and will create a text file using the input as a name.
After that the user should enter the name of the text file created before again (assume the user enters correctly) and then the program should read the text file.
I have done other parts of my program correctly, but the problem is when i enter the name again, it just can not find the text file, eventhough they are in the same folder.
public static ArrayList<DogShop> readFile()
{
try
{ // The name of the file which we will read from
String filename = "a.txt";
// Prepare to read from the file, using a Scanner object
File file = new File(filename);
Scanner in = new Scanner(file);
ArrayList<DogShop> shops = new ArrayList<DogShop>();
// Read each line until end of file is reached
while (in.hasNextLine())
{
// Read an entire line, which contains all the details for 1 account
String line = in.nextLine();
// Make a Scanner object to break up this line into parts
Scanner lineBreaker = new Scanner(line);
// 1st part is the account number
try
{ int shopNumber = lineBreaker.nextInt();
// 2nd part is the full name of the owner of the account
String owner = lineBreaker.next();
// 3rd part is the amount of money, but this includes the dollar sign
String equityWithDollarSign = lineBreaker.next();
int total = lineBreaker.nextInt();
// Get rid of the dollar sign;
// we use the subtring method from the String class (see the Java API),
// which returns a new string with the first 'n' characters chopped off,
// where 'n' is the parameter that you give it
String equityWithoutDollarSign = equityWithDollarSign.substring(1);
// Convert this balance into a double, we need this because the deposit method
// in the Account class needs a double, not a String
double equity = Double.parseDouble(equityWithoutDollarSign);
// Create an Account belonging to the owner we found in the file
DogShop s = new DogShop(owner);
// Put money into the account according to the amount of money we found in the file
s.getMoney(equity);
s.getDogs(total);
// Put the Account into the ArrayList
shops.add(s);
}
catch (InputMismatchException e)
{
System.out.println("File not found1.");
}
catch (NoSuchElementException e)
{
System.out.println("File not found2");
}
}
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
} // Make an ArrayList to store all the accounts we will make
// Return the ArrayList containing all the accounts we made
return shops;
}
If you are working in some IDE like Eclipse or NetBeans, you should have that a.txt file in the root directory of your project. (and not in the folder where your .class files are built or anywhere else)
If not, you should specify the absolute path to that file.
Edit:
You would put the .txt file in the same place with the .class(usually also the .java file because you compile in the same folder) compiled files if you compile it by hand with javac. This is because it uses the relative path and the path tells the JVM the path where the executable file is located.
If you use some IDE, it will generate the compiled files for you using a Makefile or something similar for Windows and will consider it's default file structure, so he knows that the relative path begins from the root folder of the project.
Well.. Apparently the file does not exist or cannot be found. Try using a full path. You're probably reading from the wrong directory when you don't specify the path, unless a.txt is in your current working directory.
I would recommend loading the file as Resource and converting the input stream into string. This would give you the flexibility to load the file anywhere relative to the classpath
If you give a Scanner object a String, it will read it in as data. That is, "a.txt" does not open up a file called "a.txt". It literally reads in the characters 'a', '.', 't' and so forth.
This is according to Core Java Volume I, section 3.7.3.
If I find a solution to reading the actual paths, I will return and update this answer. The solution this text offers is to use
Scanner in = new Scanner(Paths.get("myfile.txt"));
But I can't get this to work because Path isn't recognized as a variable by the compiler. Perhaps I'm missing an import statement.
This should help you..:
import java.io.*;
import static java.lang.System.*;
/**
* Write a description of class InRead here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class InRead
{
public InRead(String Recipe)
{
find(Recipe);
}
public void find(String Name){
String newRecipe= Name+".txt";
try{
FileReader fr= new FileReader(newRecipe);
BufferedReader br= new BufferedReader(fr);
String str;
while ((str=br.readLine()) != null){
out.println(str + "\n");
}
br.close();
}catch (IOException e){
out.println("File Not Found!");
}
}
}
Just another thing... Instead of System.out.println("Error Message Here"), use System.err.println("Error Message Here"). This will allow you to distinguish the differences between errors and normal code functioning by displaying the errors(i.e. everything inside System.err.println()) in red.
NOTE: It also works when used with System.err.print("Error Message Here")