-1

Below is the program that prints all the files & folders from the given path.

import java.io.File;

public class ListDirectoryRecursive{

    public static void listRecursive(File dir){
        if(dir.isDirectory()){
            File[] items = dir.listFiles();
            for(File item : items){
                System.out.println(item.getAbsoluteFile());
                if(item.isDirectory()){
                    listRecursive(item);
                }
            }
        }
    }

    public static void main(String[] args){
        /* Unix path is: /usr/home/david/workspace/JavaCode */
        File dir = new File("C:\\Users\\david\\workspace\\JavaCode");
        listRecursive(dir);
    }
}

How do i make this java program run on Unix? What is the standard approach to make this program portable?

Edit: I guess, we know on any OS, user home directory is part of the environment setting with values like "c:\users\david" in windows and "/user/home/david" in Unix.

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • You dont have to explicitely say c:\, you could just type /Users/david/workspace/JavaCode and can use it on both the platform. – SMA Dec 20 '14 at 07:25
  • System.getProperty("user.home"); – J.K Dec 20 '14 at 07:26
  • @almasshaikh: ...assuming `/Users/david` were valid on UNIX (it may not be). – nneonneo Dec 20 '14 at 07:26
  • I always use the `/` for path separators *irrespective* of platform in Java (works on Win and Unix). Pass a different path in Windows from Unix as a platform specific variable. Related: http://stackoverflow.com/q/3548775/3651800 – Matt Coubrough Dec 20 '14 at 07:27
  • I agree @nneonneo I believe that's what OP is after? – SMA Dec 20 '14 at 07:28
  • @almasshaikh: OP actually says "Unix path is: /usr/home/david/workspace/JavaCode" in the comments. So he does not want `/Users` on UNIX. – nneonneo Dec 20 '14 at 07:30
  • @nneonneo This program could be part of an application, so please do not consider reading command line arguments, please read my comment below. – overexchange Dec 20 '14 at 07:38
  • @overexchange: Then you have to be more specific as to what directories you are accessing. Accessing the root directory is different from accessing a user directory, is different from accessing a temporary directory, etc. – nneonneo Dec 20 '14 at 07:41

3 Answers3

1

Take the directory as an argument via args:

File dir = new File(args[1]);

(checking of course that args.length is sufficient).

Then you can simply do

java ListDirectoryRecursive C:\Users\david\workspace\JavaCode

on Windows, and

java ListDirectoryRecursive ~david/workspace/JavaCode

on UNIX. This has the distinct advantage of allowing you use this program to list any directory, rather than being a hardcoded path.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

Make the hardcoded absolute path C:\\Users\\david\\workspace\\JavaCode relative.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
1

please dont harcode

File dir = new File("C:\\Users\\david\\workspace\\JavaCode");

use System.getProperty("user.home"); to get /usr/home/david or C:\Users\david\ directory like

String path = System.getProperty("user.home") + File.separator + "workspace" + File.separator + "JavaCode";
File dir = new File(path);

thanks overexchange

J.K
  • 2,290
  • 1
  • 18
  • 29
  • `System.getProperty()` answers half of the question, Can we use, any of these 4 properties `File.separator` / `File.pathSeparator` / `File.separatorChar` / `File.pathSeparatorChar` to define separators? If yes, How do i use it? – overexchange Dec 20 '14 at 07:34
  • 1
    `String path = System.getProperty("user.home") + File.separator + "workspace" + File.separator + "JavaCode";` – overexchange Dec 20 '14 at 08:20