Traversing files and directories underneath a directory
I'm looking at some sample code about traversing files and directories underneath a directory. I'm trying to understand exactly what the code is doing. I see that dir.list() is building a string array of filenames and directory names. Then it tests each to see whether it's a directory. If it's a directory then is looks and each file and or directory in that directory.
But I don't understand how it keeps going below just the second level directory. The two lines of code I'm not sure I understand are:
1) process(dir);
2) visitAllDirsAndFiles(new File(dir, children));
Can anyone help me to understand exactly how this code block can traverse beyond just the second directory level?
// Process all files and directories under dir
public static void visitAllDirsAndFiles(File dir) {
process(dir);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllDirsAndFiles(new File(dir, children));
No, I guess they might be assuming somethings. Here is the page with the sample code:
[http://exampledepot.com/egs/java.io/TraverseTree.html]
Similar Messages
-
JFileChooser - show files and directories only select directory
I need a JFileChooser that will display directories and zip files but only allow a directory to be selected.
basically i need to be able to select a certain directory that contains a certain zip file, so to make it easier to find i want the zip files to show up so i can find the correct directory...
I tried...
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
else if (f.getName().endsWith(".zip")) {
return true;
return false;
public String getDescription() {
return "testing";
});but that only displays directories in the GUI.
The javadoc for the setFileSelectionMode() says, "Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories. The default is JFilesChooser.FILES_ONLY. " ....which makes you think that setting the file selection mode only alters what can be SELECTED...however it alters what is displayed as well.
...any ideas?
thanksThis should display the files of interest provided the filter is tuned to do so, and allow you to select either the file of interest or the folder... If you select the file, then folder it is contained is returned.
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
MyFileFilter filter = new MyFileFilter();
chooser.setFileFilter(filter);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getCurrentDirectory());
} -
How to list all files and directories in another directory
I need to be able to list all the directories and files in a directory. I need to write a servlet that allows me to create an html page that has a list of files in that directory and also list all the directories. That list of files will be put into an applet tag as a parameter for an applet that I have already written. I am assuming that reading directories/files recursively on a web server will be the same as reading directories/files on a local system, but I don't know how to do that either.
Hi,
Here is a method to rotate through a directory and put all the files into a Vector (files).
* Iterates throught the files in the root file / directory that is passed
* as a parameter. The files are loaded into a <code>Vector</code> for
* processing later.
* @param file the root directory or the file
* that you wish to have inspected.
public void loadFiles(File file) {
if (file.isDirectory()) {
File[] entry= file.listFiles();
for (int i= 0; i < entry.length; i++) {
if (entry.isFile()) {
//Add the file to the list
files.add(entry[i]);
} else {
if (entry[i].isDirectory()) {
//Iterate over the entries again
loadFiles(entry[i]);
} else {
if (file.isFile()) {
//Add the file
files.add(file);
See ya
Michael -
[SOLVED] Check for old configuration files and directories in ~/
Hello,
Since I started with Arch I tried and uninstalled a few programs and would like to check for possible left overs in ~/
I tried the "lostfiles" script, but it does not seem to check the home directory. Then I followed this: https://wiki.archlinux.org/index.php/Pa … ny_package - But using the suggested alias in my bash, it gives me the below:
$ pacman-disowned-files
[sudo] password for [my user]:
bash: -path: command not found
bash: /root: Is a directory
bash: -print: command not found
What am I doing wrong?
Regards
Martin
Last edited by onslow77 (2015-02-04 23:52:44)Hello,
ohh, I see, I need to do this (in directory where I put the script)
sh pacman-disowned -files > test.txt
That solved the usage of the commands, but it does not however list files [in the test.txt file] that do not belong to any package in my ~/ directory (I put a dummy file in my ~/ to see if it got noticed) - but it did not. So how can I list files and directories in my ~/ directory that is not put there by any package that currently is installed?
Regards
Martin
Last edited by onslow77 (2015-02-04 22:49:11) -
How to select file and directories to ZIP
HI,
I'm doing an application that would zip a file and directories. here's an exmple:
fileName = FileNameTest.csv
folderName = FileNameTest_A
folderName = FileNameTest_B
The foldername is taken from the fileName with "_A" and "_B" at the end. The file and directories are located in a directory where there can also be other files and directories in it. What I want to do is to zip only the above file and directories.
Any help will be greatly appreciated!
Thanks!Not a very clear specification. What do you want the resulting zip entries/files/paths to look like?
-
File Chooser is displaying names of the Files and Directories as boxes
Hi All,
Actually i am working on an application in which i have the requirement to internationalize the File Chooser in All Operating Systems. The application is working properly for all languages on MAC OS, but not working properly for the language Telugu on both the Ubuntu and Windows OS. The main problem is, when i try to open the File Chooser after setting the language to TELUGU all the labels and buttons in the File Chooser are properly internationalized but names of Files and directories in the File Chooser are displaying as boxes.
So please provide your suggestions to me.
Thanks in Advance
Uday.I hope this can help you:
package it.test
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
* @author Alessandro
public class Test extends JDialog {
private JFileChooser fc = null;
private Frame bfcParent = null;
public Test(Frame parent, boolean modal) {
super(parent, modal);
this.bfcParent = parent;
if (fc == null) {
fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
fc.setLocale(Locale.ITALIAN);//i think you should use english
//these are in telugu
UIManager.put("FileChooser.openDialogTitleText", "Open Dialog");
UIManager.put("FileChooser.saveDialogTitleText", "Save Dialog");
UIManager.put("FileChooser.lookInLabelText", "LookIn");
UIManager.put("FileChooser.saveInLabelText", "SaveIn");
UIManager.put("FileChooser.upFolderToolTipText", "UpFolder");
UIManager.put("FileChooser.homeFolderToolTipText", "HomeFolder");
UIManager.put("FileChooser.newFolderToolTipText", "New FOlder");
UIManager.put("FileChooser.listViewButtonToolTipText", "View");
UIManager.put("FileChooser.detailsViewButtonToolTipText", "Details");
UIManager.put("FileChooser.fileNameHeaderText", "Name");
UIManager.put("FileChooser.fileSizeHeaderText", "Size");
UIManager.put("FileChooser.fileTypeHeaderText", "Type");
UIManager.put("FileChooser.fileDateHeaderText", "Date");
UIManager.put("FileChooser.fileAttrHeaderText", "Attr");
UIManager.put("FileChooser.fileNameLabelText", "Label");
UIManager.put("FileChooser.filesOfTypeLabelText", "filesOfType");
UIManager.put("FileChooser.openButtonText", "Open");
UIManager.put("FileChooser.openButtonToolTipText", "Open");
UIManager.put("FileChooser.saveButtonText", "Save");
UIManager.put("FileChooser.saveButtonToolTipText", "Save");
UIManager.put("FileChooser.directoryOpenButtonText", "Open Directory");
UIManager.put("FileChooser.directoryOpenButtonToolTipText", "Open Directory");
UIManager.put("FileChooser.cancelButtonText", "Cancel");
UIManager.put("FileChooser.cancelButtonToolTipText", "Cancel");
UIManager.put("FileChooser.newFolderErrorText", "newFolder");
UIManager.put("FileChooser.acceptAllFileFilterText", "Accept");
fc.updateUI();
public int openFileChooser() {
fc.setDialogTitle("Open File");
fc.resetChoosableFileFilters();
int returnVal = 0;
fc.setDialogType(JFileChooser.OPEN_DIALOG);
returnVal = fc.showDialog(this.bfcParent, "Apri File");
//Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("Hai scelto di aprire un file");
} else {
System.out.println("hai annullato l'apertura");
return returnVal;
private static void createAndShowGUI() {
JFrame frame = new JFrame("FileChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel(new BorderLayout());
JButton openButton = new JButton("Open File");
final Test test = new Test(frame, true);
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
test.openFileChooser();
openButton.setEnabled(true);
jp.add(openButton, BorderLayout.AFTER_LAST_LINE);
//Add content to the window.
frame.add(jp);
//Display the window.
frame.pack();
frame.setVisible(true);
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
createAndShowGUI();
}bye ale -
What files and directories are shipped with the Oracle Agent ?
Hi:
- Could someone please outline the intent of all the files
and directories that are shipped with the Oracle agent?
- I think this was documented somewhere but I cannot
recall where I saw this previously.
- Another question: if I wanted to add a logfile for my plug-in
(for debugging/troubleshooting)
under which directory should this logfile be placed ?
Thanks
Johnhttp://download.oracle.com/docs/cd/B19306_01/em.102/b40002/structure.htm#i1028189 should help you understand the directory structure of OEM Agent.
-
On our internal web page we have some links to various areas
like Office Forms, Safety, etc... The supervisors that maintain
these documents have access to shares that allow them add, remove,
and update the files as needed. Since the content in each changes
fairly often we're currently using CFDIRECTORY with the LIST action
to display the contents to the users on the web page.
Some of the areas now contain quite a few documents and the
supervisors would like to be able to group their documents in
subdirectories. I know I can I can use RECURSE="yes" to have the
web page list the contents of these folders, but the supervisors
would like the end users to see the subdirectories as well so
things are less confusing and cluttered on both ends.
So is there any way in CF to set up these pages so that
subdirectories can can be navigated and the documents selected? If
possible, I'd like something that can handle this dynamically so
that I don't have to constantly change the code every time a
supervisor decides to add/remove a directory.
We're currently running CF8 Standard.
Thanks!Vidikron wrote:
> On our internal web page we have some links to various
areas like Office Forms,
> Safety, etc... The supervisors that maintain these
documents have access to
> shares that allow them add, remove, and update the files
as needed. Since the
> content in each changes fairly often we're currently
using CFDIRECTORY with the
> LIST action to display the contents to the users on the
web page.
>
> Some of the areas now contain quite a few documents and
the supervisors would
> like to be able to group their documents in
subdirectories. I know I can I can
> use RECURSE="yes" to have the web page list the contents
of these folders, but
> the supervisors would like the end users to see the
subdirectories as well so
> things are less confusing and cluttered on both ends.
>
> So is there any way in CF to set up these pages so that
subdirectories can can
> be navigated and the documents selected? If possible,
I'd like something that
> can handle this dynamically so that I don't have to
constantly change the code
> every time a supervisor decides to add/remove a
directory.
>
> We're currently running CF8 Standard.
>
> Thanks!
>
Pretty much what you got should do it. The simplest change
would be to
output entire directory structure on your current page, just
nesting the
files and directories.
I would just create a looping output that uses the fields of
the record
set to output the directories and the files with-in them
probably as a
nested set of <ul> unordered lists.
If you want to break up this user interface into distinct
units which
only list the contents of a current directory then just do
the output as
you have it without using recursion. But make the
sub-directory
listings links that when clicked repopulate the current page
listing the
contents of the selected directory.
Neither should take a great deal of rework to your current
interface. -
Document containing the SAP Files and Directories
hi,
Pls let me know the link where i can get the file and directories of location of all sap files such as disptacher logs, profiles etcDear Balaji,
Run transaction Al11.
There you can see the directory structure of SAP files.
Regards,
Arun
PS: Reward points are welcome -
[Solved] File browser that supports mixed files and directories
Are there any file browsers that show files and directories together, in the way that macs do? Most of the time I like the seperated structure, but for some things I'd like to be able to sort by, say, date modified, and have all the newest files and directories together. I'm hoping I'm just overlooking an option somewhere (I'm using nemo now), but this is kind of a tricky thing to google.
EDIT
Just looked again and noticed the option, right there in front of me. Oops. Still, andyone happen to know how to make this a property of a single directory, and not global?
Last edited by ce1984 (2014-04-25 02:05:16)ls - just don't use
--group-directories-first -
Please help on create files and directories in Session Bean
Hi,
I am trying to create files and directories at run time from a session bean. Since EJB 2.0 prohibit the use of the java.io.* package. The only thing that I know how is to use the URLConnection. After getting a connection through URL. I may use the getOutputStream() to get a output stream to write to the HTTP server. But the getOutputStream() returns an OutputStream object which is one of the package in the java.io package. Since I need to create file and directory, I need to use the FileOutputStream that is also under the java.io package.
Can someone give me a hint what should I do. Here is a directory and the file that I may need to create;
Directory1/Drictory2/myFiles.au
Thank you in advance, your help is greatly appreciated.From 24.1.2 (Programming Restrictions) of the EJB 2.0 spec:
� An enterprise bean must not use the java.io package to attempt to access files and directories in the file system.
The file system APIs are not well-suited for business components to access data. Business components should use a resource manager API, such as JDBC, to store data.
You might want to look at the File System Service Provider in JNDI for an alternative approach. I have never used an FSSP though. -
Set numerous files and directories to 664 and 775
This is actually a Solaris 9 question:
I need to set numerous files/directories under a selected "branch" (directory) to 664 (files) and 775 (directories), recursively.
also need to ensure all files/directories created from then on are 664/775, respectively.
ThanksI guess I wasn't clear...
The directory/file structure consists of multiple levels (subdirectories) and files in each of those subdirectories.
I can do a
chmod -R 775 whatever
this changes ALL files AND directories under directory: whatever to 755
I need to change all subdirectories to 775 and all files (non-directories) within those subdirectories to 664
Is there a way to indicate file or directory.. There does not appear to be an option using chmod..
Thanks -
Copying files and directories from AIX/UNIX to Client PC in ABAP.
Hi all,
Can someone help me. I have to make an ABAP program which copies the J2EE Visual Adminstrator programs from our AIX/UNIX server to the Client PC.
I have to copy all files and directories in /usr/sap/DXI/DVEBMGS70/j2ee/admin/. to D:\j2ee\*.*
Is it possibly in ABAP? How would you do it?
Best regards
Brian StrungeHi,
try this.
DATA: lcl_directory TYPE char128,
constants: c_mask TYPE char9 VALUE ',.,..'.
lcl_directory = ''/local/data/interface/A28/DM/OUT'. <b>"target directory</b>
CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
EXPORTING
directory = lcl_directory
filemask = c_mask
IMPORTING
serverfile = p_f2
EXCEPTIONS
canceled_by_user = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE e000(zmm) WITH text-039.
flg_app = 'X'.
ENDIF.
******************************************************8
REPORT zdirtest.
DATA lv_dir TYPE rsmrgstr-path VALUE '/usr/sap/trans/data'. <b>"source directory</b>
DATA: wa_files TYPE rsfillst,
it_files LIKE TABLE OF wa_files.
START-OF-SELECTION.
CALL FUNCTION 'SUBST_GET_FILE_LIST'
EXPORTING
dirname = lv_dir
filenm = '*'
TABLES
file_list = it_files
EXCEPTIONS
access_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE /1 'Error! :-('.
ELSE.
LOOP AT it_files INTO wa_files.
WRITE: /1 wa_files-name,
wa_files-len.
ENDLOOP.
Regards
Anver -
I have an external hard drive that was formatted by a PC and has files and directories copied to it etc. I want to use it on my IMAC for backup. I see it on my my IMAC . I can open files etc. But I can't seem to write to it nor can I delete current content. I don't care if I lose current content. How do I initialize it for use with the MAC?
You can't write to it because it's formatted as NTFS which OS X will read but not write to. If you want to continue using the drive with both a PC and OS X you will need to download and install NTFS-3G so you can then write to it from your Mac. You can get NTFS-3G at:
http://www.macupdate.com/app/mac/24481/ntfs-3g
If you want to use the drive exclusively with your Mac then move the data off it and reformat it in Disk Utility (Applications - Utilities - Disk Utilities) as Mac OS Extended (Journaled.) -
How to find files and subdirectories in a directory
can anyone tell me how to find files and subdirectories in a directory .
Here's a code snippet,
http://javaalmanac.com/egs/java.io/TraverseTree.html
Maybe you are looking for
-
Why is my apple ID not verifying to activate my imessage?
My apple Id is not verifying when I try to activate my iMessage. I just keeps going back to the sign in screen. Anybody know what's wrong so I can start texting.
-
SoftReference and WeakReference clearing
According to the javadoc of SoftReference: http://java.sun.com/javase/6/docs/api/java/lang/ref/SoftReference.html: Suppose that the garbage collector determines at a certain point in time that an object is softly reachable. At that time it may choose
-
Sun Studio 12EA and Netbeans Profiler 5.5
I was wondering if anyone was able, or if it is possible to get the Netbeans 5.5 profiler for Java working in Sun Studio 12EA. The installation into netbeans-5.5_mars seems to work fine, yet the profiling menu does not appear in Sun Studio after a re
-
Dear all, this is my 3rd day that I spent in front of my computer and my mind start cracking. Firstly here is my computer config: win7 ultimate service pack 1 always "run as administrator". I had bold 9000 (OS5 - BIS) and and was using/syncing with
-
Creating an Import Document iView
Hi, I´m trying to create an iview for batch document upload to the portal. At the moment, I've only been able to upload documents from a source directory in the server, given an xml file uploaded from the client, which indicates the location of these