Creating list of files from a directory

Hi,
poked around on the internet, and wanted to be able to create an ArrayList of all files in a directory. This code compiles, but doesn't work:
import java.io.*;
import java.util.*;
public class TestDir {
     public static void main(String[] args){
     File folder = new File("testdir");
     System.out.println("does the folder exist? " + folder.exists());
     System.out.println("is folder a directory? " + folder.isDirectory());
     System.out.println("path: " + folder.getAbsolutePath());
     // create list of files
     try{
     ArrayList<File> files = TestDir.getFileList(folder);
     Iterator i = files.iterator();
     while (i.hasNext()){
     File temp = (File) i.next();
     System.out.println("file: " + temp.getName());
     } catch (IOException e){
     System.err.println("problem creating file list:");
     e.printStackTrace();
     } // end main
     public static ArrayList<File> getFileList(File dir) throws FileNotFoundException{
     ArrayList<File> result = new ArrayList<File>();
     File[] files = dir.listFiles();
     List<File> tempfiles = Arrays.asList(files);
          for(File file : files){
          result.add(file);
     return result;
} // end classthis compiles fine, but doesn't give me the list of files.
Any idea what I'm doing wrong?
bp

console output:
C:\jason\java\domPractice>java TestDir
does the folder exist? true
is folder a directory? true
path: C:\jason\java\domPractice\testdir

Similar Messages

  • Getting list of files from Unix directory inclding files frm sub-directries

    Hi All,
            I am trying to use Fm 'SUBST_GET_FILE_LIST'  and
    'RZL_READ_DIR_LOCAL' for getting a list of all files in a Unix directory including files from sub-directories.
    In the first case I am getting an Exception called 'Access Error'
    and when I use the 2nd FM, I am not getting any output.
    Is there a special way to use these FMs.
    Kindly help.
    Regds,
    Shweta

    [url http://java.sun.com/developer/JDCTechTips/2003/tt0122.html#1]READING FILES FROM JAVA ARCHIVES (JARS)

  • Creating a zip file from the contents of a directory

    hi, I am having a problem as the title suggests with a zip fil creation...
    using the basic example zip.java i wished to edit it so it doesnt zip a file fro the current directory but rather a directory i inputted.
    It is able to read the first file then throws out the following error with the code below it:
    java.io.FileNotFoundException: test.jpg (The system cannot find the file specified)
    import java.io.*;
    import java.util.zip.*;
    public class Zip {
       static final int BUFFER = 2048;
       public static void main (String argv[]) {
          try
             BufferedInputStream origin = null;
             FileOutputStream dest = new FileOutputStream("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/test.zip");
             CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
             ZipOutputStream out = new
               ZipOutputStream(new BufferedOutputStream(checksum));
             //out.setMethod(ZipOutputStream.DEFLATED);
             byte data[] = new byte[BUFFER];
             // get a list of files from current directory
             File f = new File("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/");
             f.listFiles();
             String files[] = f.list();
             for (int i=0; i<files.length; i++)
                System.out.println("Adding: "+files);
    FileInputStream fi = new FileInputStream(files[i]);
    origin = new BufferedInputStream(fi, BUFFER);
    ZipEntry entry = new ZipEntry(files[i]);
    out.putNextEntry(entry);
    int count;
    while((count = origin.read(data, 0,
    BUFFER)) != -1)
    out.write(data, 0, count);
    origin.close();
    out.close();
    catch(Exception e)
    e.printStackTrace();
    After investigation i am lead to believe this is because the method returns an array of file and directory names only but not their path and the unqualified names would have therefore be defaulted to the current working directory. Something i understand if this is the case.
    So instead i used the File.listFiles() method to return an array
    of File objects, instead of an array of Strings as shown in the snippet of the changed code below (the changed code highlighted)...but arrived at another error on the second section of highlighted code meaning i cant compile. I cant understand why this is so!
    The error is: "cannot find symbol, Symbol: Contructor ZipEntry (Java.IO.file), location: class.java.util.zip.ZipEntry"
    File f = new File("C:/Documents and Settings/Phil/My Documents/My Pictures/Work/");
             **************File g[] = f.listFiles();***************
             **************String files[] = f.list();**************
             for (int i=0; i<g.length; i++)
                System.out.println("Adding: "+g);
    FileInputStream fi = new FileInputStream(g[i]);
    origin = new BufferedInputStream(fi, BUFFER);
    **************ZipEntry entry = new ZipEntry(g[i]);************
    out.putNextEntry(entry);
    int count;
    while((count = origin.read(data, 0,
    BUFFER)) != -1)
    out.write(data, 0, count);
    origin.close();
    out.close();
    Any help and thoughts most appreciated. Thank u in advance

    I'll admit i took 1 look at that reply an thought "thats stupid that wont work"...
    then a second look an though "actually, i should work i cant believe i didnt think of that"
    Anyways i tried it and it did work
    How are the duke dollars awarded, cos u should have them ordinary_guy
    cheers!

  • Help.....problem with obtaining files from a directory

    Anaybody knows how to obtain list of files from a directory from j2me
    as in j2se ?

    I don't think it's possible because a MIDlet lives isolated from the rest of the device's filesystem. It can only store persistent data on the Record Media Store, which is unique for each MIDlet.

  • How can I create a array with all files from a directory

    How can I create a array of files or varchar with all files from a directory?

    I thought the example could be improved upon. I've posted a solution on my blog that doesn't require writing the directory list to a table. It simply returns it as a nested table of files as a SQL datatype. You can find it here:
    http://maclochlainn.wordpress.com/2008/06/05/how-you-can-read-an-external-directory-list-from-sql/

  • I want read PDF file from SAP directory and create a spool request or print

    Hi all,
    I want read PDF file from SAP directory and create a spool request or print the pdf through SAP. Can any body  help me in this.
    Also please write to me if its possible to open PDF from SAP directory to adobe pdf reader.
    Thanks in advance,
    Sunny

    Hi Sunny,
    Check these links.
    http://www.sapdevelopment.co.uk/reporting/rep_spooltopdf.htm
    http://www.erpgenie.com/sap/abap/pdf_creation.htm
    http://www.geocities.com/mpioud/Z_EMAIL_ABAP_REPORT.html
    http://www.thespot4sap.com/Articles/SAP_Mail_SO_Object_Send.asp
    http://www.sapdevelopment.co.uk/reporting/email/attach_xls.htm
    Hope this resolves your query.
    Reward all the helpful answers.
    Regards

  • Getting a list of files from a shared directory

    Hi All,
    I need to get a list of files from a shared directory on another server and put them in a HTMLB TableView element with links to the files.
    I was not successful with CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES. This method works in test mode, but gives me a gui_not_supported exception in BSP. I guess BSPs are not allowed to use this.
    Is there any other alternative?
    I need to be able to upload the files as well.
    Thanks,
    Roman
    Message was edited by: Roman Dubov

    Hi again,
    Not sure about the shared directory files list...
    In my opinion, this cannot be done using BSP.
    But, you can try using VBScript to access the file system with object of that sort :
    CreateObject("Scripting.FileSystemObject")
    it is only a thought and you can be sure to cross security problems by accessing files through a Web application...
    Best regards,
    Guillaume

  • Unix command to create a copy of file from unix directory

    Hi guys,
    what is the Unix command to create a copy of file from unix directory?
    Thanks a lot!

    If you haven't noticed, this is a ABAP forum.
    You can go to the UNIX thread instead and post it there,
    SAP on UNIX
    You can expect answers there

  • Is there anyway to create a xml file from a list so that xml file is available like an RSS.xml

    Hello
    My objective is to have a xml file created from a list for anonymous access.  The SharePoint RSS feed is a .aspx file not xml, as shown below: 
    _layouts/listfeed.aspx?List=21c2cdaa%2D52f3%2D4057%2Db674%2D45e63ba77e31&View=535eb328%2Db5fb%2D45c5%2D8fe8%2Da130e92afc41
    Also, is there a way to do this so that the xml content has current data like the list.
    Thank you for any insight and direction.

    Hi,
    According to your post, my understanding is that you wanted to create a xml file from a SharePoint list.
    To generate a XML file, we can use the SharePoint Object Model or PowerShell to achieve it.
    Generate a hierarchical XML file from SharePoint list using Client Object Model.
    http://maxderungs.wordpress.com/2012/05/12/generate-a-hierarchical-xml-file-from-sharepoint-list/
    Get SharePoint list items and export them to XML using PowerShell
    http://www.robertkuzma.com/2012/09/get-sharepoint-list-items-and-export-them-to-xml-using-powershell/
    More reference:
    http://www.robertkuzma.com/2010/08/how-to-create-a-custom-xml-output-using-sharepoint-services-3-0/
    http://traceynolte.com/blog/convert-sharepoint-list-to-xml/
    Thanks & Regards,
    Jason
    Jason Guo
    TechNet Community Support

  • Java Program to copy file from one directory to another failing

    Hello All,
    Oracle Applications 12.1.3
    RDBMS: 11.2.0.30
    SQL*Plus: 10.1.0.5.0
    Java beginner here so help is much appreciated.  I'm have some java code that I'm using to copy a file from one directory to another in an Oracle Applications Server.  I notice that when moving to a new instnace I started to get file not found errors.  I think it's because of the directory.  My question is, does the directory in which I trying to pick up the file have to be a DBA_DIRECTORY, or a UTL_FILE directory in order for the java to find the file and move it?
    Here is my code...
    CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "COPY_FILE_JVS" AS
    import java.io.*;
    public class COPY_FILE_JVC{
      public static void copy(String SourceFile, String DestDir) {
        File source = new File(SourceFile);
        File dest = new File(DestDir);
        File cpDestFile = null;
        try {
          if (dest.isDirectory()) {
              if(source.isFile()) {
                System.out.println("File name = " + source.getName());
                System.out.println("File name dest = " + dest.getPath() );
                System.out.println("File name cpDestFile = " + dest.getPath() + File.separator + source.getName());
                cpDestFile = new File(dest.getPath() + File.separator + source.getName());
              else {
                System.out.println("Directory or File Not Found");
                return;
          BufferedInputStream  br = new BufferedInputStream (new FileInputStream (source));
          BufferedOutputStream  bw = new BufferedOutputStream (new FileOutputStream (cpDestFile));
          int read = 0;
          while((read = br.read()) != -1) {
            //System.out.println((char)read);
            bw.write(read);
          br.close();
          bw.close();
        catch (FileNotFoundException e) {
        e.printStackTrace();
        catch (IOException e) {
        e.printStackTrace();

    I get these messages when calling the java from SQL Plus as follows...
    serveroutput on size 100000;
    dbms_java.set_output(1000000);
    MY_PKG.CopyFile('/home/my_user/set_env.sh','/interface/home'); (this is call to oracle pl/sql package which calls the java.)
    PL/SQL procedure successfully completed.
    Directory or File Not Found
    PL/SQL procedure successfully completed.
    If I change the directory from /home/my_user/ to any directory that is defined in DBA_DIRECTORIES or is a UTL_FILE directory to program works.  Is it perhaps because the java is in a PL/SQL package as seen below?  The PL/SQL program runs as the APPS user and I have issued the following the command grant JAVASYSPRIV to APPS.
    Here is the PL/SQL Package....
    CREATE OR REPLACE PACKAGE BOLINF.MY_PKG AUTHID CURRENT_USER AS
    CopyFile(p_SourceFile IN VARCHAR2, p_DestDir IN VARCHAR2);
    +++++++++++++++++++++++++++++
    CREATE OR REPLACE PACKAGE BODY BOLINF.MY_PKG  is
    CopyFile(p_SourceFile IN VARCHAR2, p_DestDir IN VARCHAR2)
    AS LANGUAGE JAVA NAME 'COPY_FILE_JVC.copy(java.lang.String, java.lang.String)';

  • How to open and read many files from a directory and store contents in 2D array?

    I want to make a VI that opens and reads the data from various files contained in a directory (200 files each with 2 columns) and store these in a single 2D array. For file number 1 I want to store the data from both columns in the 2D array, but for files 2 to 200 I only want to store the second column of each file. Can someone please help?

    Hi Nadav,
    Thanks for your help. I have followed your instructions but i cannot get it to work. I used the LIST DIRECTORY to list the files in the directory - that works. However, how do I read each of the 200 files using READ FROM SPREADSHEET FILE without me having to manually select each of the 200 files? So, if I use LIST DIRECTORY to list all 200 files in an array, how do I get each of these to open and store the data in a 2D array? Here is what I have done (File called read_files.VI) Could you please help me? Thank you very much in advance.
    Attachments:
    read_files.vi ‏18 KB

  • How do i move a file from one directory to another?

    Can anyone help me in moving a file from one directory to another?
    The following code just does not work!!
    File source_file = new File(source_file);
    if(source_file.exists())
    File dest_file = new File(destination_file);
    if(!dest_file.exists())
    boolean moved = source_file.renameTo(dest_file);
    System.out.println("moved.."+moved);
    The varirable moved is always false.
    Can anyone tell me whts wrong in this code?

    The problem in your code is you used file object as argument when you create a new File. I changed a little bit codes and it should be fine now.
    String source_file1="test111.txt";
    String destination_file1="test222.txt";
    try{
         File source_file = new File(source_file1);
         if(source_file.exists())
              File dest_file = new File(destination_file1);
              if(!dest_file.exists())
                   boolean moved = source_file.renameTo(dest_file);
                   System.out.println("moved.."+moved);
    catch(Exception e){
               e.printStackTrace();
    }

  • Creating an xml file from a playlist - message for iTunes staff

    When I created an xml file from a playlist I noticed that in the xml file the track name was merged with the artist name in this manner "I want you - Bob Dylan".
    That was hardly a problem as I could easily correct this in the FileMaker Pro environment.
    But subsequently I found out, that within iTunes the track name was similarly changed to the same format. Apparently this happened when iTunes created the xml file and inadvertently also changed the track name within iTunes.
    It can easily be corrected by entering File/Info, but I had over 10.000 songs in that list and it will take a lot of time. I have to do this, as I created earlier for each song a file path in FileMaker Pro, which are unrecognizable now.
    Could you please correct the xml file creating procedure?

    I must confess that I can't reproduce this result myself anymore.
    I created the xml file in iTunes via File/Library/Export Playlist/xml structure (I translate the Dutch terms loosely) and subsequently I imported the xml file into a Filemaker Pro via File/Import/xml.
    The xsl style sheet looks ok.
    It looks like a freak of nature.
    Still I have the evidence in my iTunes playlist, where I have another 9000 clicks to go to repair the damage.
    Let's forget about it, until it happens again.
    Thanks for your time.

  • How to create a pdf file from multiple images ?

    Dear All,
    I want to create a SINGLE page pdf file from two or more page size images that are combined to make a single page pdf. Again, this question is on pdfs that are made out of several, atleast  two color images and a black-and-white mask for one of them.
    I have such pdf files from an unknown source (the producer is edited out) whereby there are three tiff images, obtained using the well known pdfimages extractor.
    When I want to make a pdf out of tiff or png or other image formats, I right click and tell Adobe Acrobat to make a pdf.
    However, I dont know how I can give a command to select say,  three tif images and specify which is the mask for which and then join  them in a way that I get the pdf from the composite of the two color images and a mask for one of them.
    Please help me out.
    I am a little familiar with the pdf structure skeleton and when necessary, fixed xref tables in one of my favorite editors. A few years ago, I also wrote a bunch of javascripts to make some annotations and needed some automation and used some itext type libraries. However, I need your help in this problem as I am now rusty and forgot some of what I studied to solve my earlier problems. This is a new problem for me. Gentle hints from you would be very nice to help me in this problem. Please specify if necessary what manual and pages to read. in the pdfspec.
    Best Regards
    Disabled Veteran [physically handicapped]

    Hello Again.
    On Fri, May 11, 2012 at 12:20 PM, lrosenth <[email protected]> wrote:
    >
    > Re: How to create a pdf file from multiple images ?
    >
    > created by lrosenth in PDF Language and Specifications - View the full
    > discussion
    > ________________________________
    >
    > No clue who Irving is…
    >
    I was hoping to have your first name so its easy for me to address you.
    > I have no clue what OS platform, programming language, etc. you use so
    > can’t really narrow things down.
    I would gladly mention that I am working on windows platform, preferably XP.
    I can also work on linux for free products that come with it.
    >  Also, as this is an Adobe forum, we only
    > recommend Adobe products – so there may be other options that even my list
    > wouldn’t include.
    But adding other products, for the help of an adobe products user,
    even if it outside adobe, shall add greater prestige to your company
    and give impression of user-centeredness.
    > If you read ISO 32000-1:2008 (aka the PDF standard), you will find the
    > information about Images and Image Masks well described.  I didn’t think I
    > needed to repeat any of that information.
    Well, just add the few pdf stanzas since you are proficient on it and
    I am presently a little rusty as I mentioned. Just asking a little
    extra yard, not even to go an extra mile.
    > And, if you read that same document, you will see that there is NO SUCH
    > THING as a “text only PDF”.   All PDF documents are structured binary files.
    Well, ascii format or uncompressed format that is human readable. I
    know its a binary file, but human readable ascii version of it.
    I hope you can give me some stanza and various other approaches
    possible so I can select or combine things for myself. I see only a
    miniscule number of posts claiming to have written masks in this forum
    and then with no details.
    Regards
    Roger
    Message was edited by: dying veteran
    because the adobe posting system went crazy and truncated all except the first line ... dunno why

  • Creating a PD file from MS Word (2007) document

    I have Adobe Acrobat 8 Pro (as part of CS3) and MS Word 2007 (as part of Office 2007)and have been trying to create a PDF file from a word document.
    Here are the steps I have taken:
    Open Word document and then click the "Acrobat" Tab.
    Then click "Create PDF" tab
    In the dialog box that appears I selected "Fully Fucntional PDF" and clicked OK
    I get the following message: "PDFMaker cannot locate the Adobe PDF Printer's printer driver. Please re-install Adobe Acrobat 8.0."
    So I re-installed Adobe CS3 (which contains Acrobat 8 Pro). This did not solve the problem.
    I repeated the whole process again this time selecting "Quick and simple PDF" and clicked OK. This time a PDF file was created.
    My question is - why can't I create a "Fully Functional PDF" file? Is there something wrong or do I need some higher level version of Acrobat?
    Looking forward to some advice. Thanks. Alan

    Look in the printer list to be sure that the Adobe PDF printer is loaded. Also, go to the Adobe.com>download>updates and download all of the updates for AA8 and install them in order (don't try to skip unless the site says you can). The current version is AA8.1.3. That might solve the problem. It may be that you need at least AA8.1.2 to work properly with WORD 2007. There were a lot of problems with the initial release.

Maybe you are looking for

  • White MacBook become yellow-dirty

    Hi, I've read a lot of articles from the web about cleaning methods for white macbook. I've bought this laptop in 2006 and now, after three years of use (I'm a lady and I usually have my computer sessions with clean hands :-)) the area (especialt dx

  • Creating a delay on an advanced action

    Other than using the timeline, does anyone know of a way to create a delay in an advanced action? What i'm trying to do is when the user clicks a button, have an object appear, then have it move to the next slide. If i place both actions in an advanc

  • Reading in data from a file and terminating at EOF

    Hey all, Im relatively new to labview so this may seem to be a simple question. My task is to read in a file with integers with "n" lines, until the end of file. I only need to read in the first 3 numbers of each line. Right now my code works for onl

  • No VGA output

    Hi all, I have a small problem I have just purchased a new K7N2 Delta motherboard, from Scan.com.   I am running an Asus GeForce 4 V9480/TVD graphic card,  with 1 Gb of DDR 2700 memory and a 120Gb HDD, with Windows XP Pro. Now the problem I have is t

  • Setting a default background color

    Hi All, I would like to set a default background color for all the components that I'm going to draw. What is the best way to achieve this? Thanks. Syed