Home > Mobile >  Is there a way to have a directory run through the main program?
Is there a way to have a directory run through the main program?

Time:02-20

I am having difficulty passing a directory to the code below. When I am prompted to enter a directory, I do as shown below: C:\Users. I get a 0 byte output, which is not accurate. Meaning that the program is not registering the typed directory.

code:

//import jdk.internal.icu.text.UnicodeSet;

import java.io.File;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;



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

        //String s = "C:\\Users\\

        //File filess = new File(s);

        System.out.println("Enter a directory or a file: ");
        Scanner input = new Scanner(System.in);
        String directory = input.nextLine();

        Hwk2018 obj = new Hwk2018();

        System.out.println(obj.getSize(new File(directory))   " bytes");
    }


        int i = 0;
        Queue<File> que = new LinkedList<>();

        public long getSSize(File directory)
        {
            long size = 0;
            que.add(directory);


            while(!que.isEmpty())
            {
                File t = que.poll();
                if(!t.isDirectory())
                {
                    size  = t.length();
                }
                else
                {
                    //for(int i = 0; )

                    que.add(directory);

                }
            }
            return size;


        }

        public static long getSize(File file)
        {
            long size = 0;

            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; files != null && i < files.length; i  ) {
                    size  = getSize(files[i]);
                }
            } else {
                size  = file.length();
            }

            return size;

        }

}

output when running ' MBP src % java Hwk2018 ' on the terminal and subsequently typing C:\Users:

0 bytes

Expected Output:

87 bytes (or some numerical value other than 0)

CodePudding user response:

I would use File#exists to verify that the user input is a valid path, you could also add File#isDirectory check, but since getSize is doing this, it's probably not required.

subsequently typing C:\Users: I'm using a Mac

Mac's don't have a concept of "drives" like windows, they have "volumes" and as such it should probably be /Users, but you could run into other issues, since you won't have read access to other users.

The following will print the path of your "home" directory and also uses File#exist to verify the user input

import java.io.File;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        //String s = "C:\\Users\\
        //File filess = new File(s);

        String directory = System.getProperty("user.home");
        System.out.println("Your home directory is ["   directory   "]");

        System.out.println("Enter a directory or a file: ");
        Scanner input = new Scanner(System.in);
        directory = input.nextLine();

        File parent = new File(directory);
        if (parent.exists()) {
            Main obj = new Main();
            System.out.println(obj.getSize(new File(directory))   " bytes");
        } else {
            System.out.println(directory   " is not a valid directory");
        }
    }

    public static long getSize(File file) {
        long size = 0;

        System.out.println("Scanning "   file.getName());

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; files != null && i < files.length; i  ) {
                size  = getSize(files[i]);
            }
        } else {
            size  = file.length();
        }

        return size;

    }

}
  • Related