Discussion:
Reading Disc Directories
(too old to reply)
Raymond BARBER
2010-04-24 13:03:23 UTC
Permalink
Hi, can one of you nice folks give me a steer as to how to READ a directory
ona disc and print out the reult to screen. I know it should be simple but
having trouble finding info
Thanks very much
REB
Tony Sipma
2012-03-21 21:13:22 UTC
Permalink
Post by Raymond BARBER
Hi, can one of you nice folks give me a steer as to how to READ a directory
ona disc and print out the reult to screen. I know it should be simple but
having trouble finding info
Thanks very much
REB
Here is what I came up with playing with this very idea:


//Testing java file stuff

import java.io.*;

public class FileTest{

public static void main(String[] args) {
// set file path to the directory/path this is run from.
String filePath = "/";
//if a file path given at command line use provided file path
if (args.length == 1)
filePath = args[0];
//create file handler to a specific path
File fileHandler = new File(filePath);
//get list of files and directories under file path
String[] myList = fileHandler.list();
//display files/directories
for (int x = 0; x < myList.length; x++)
{
//output index x in array
System.out.print(myList[x]);
//create complete file path to this file/directory
String os = System.getProperty("os.name").toLowerCase();//get name of os
String loopPathString = null;
if (os.indexOf("win") >= 0)//if Windows
loopPathString = filePath + "\\" + myList[x];
if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0)//if Unix or Linux
loopPathString = filePath + "//" + myList[x];
//create file handler for path just created
File loopPath = new File(loopPathString);
//output if index x in array is a file or directory
if (loopPath.isDirectory())
System.out.print("\t:d\n");
else if (loopPath.isFile())
System.out.print("\t:f\n");
}

}
}

Continue reading on narkive:
Loading...