Reading files and directory from a directory

ublic static void readFromDirectory(String dirName) {
          // stop word ArrayLists
          ArabicStop();
          EnglishStop();
          FrenchStop();
          SpanishStop();
          DutchStop();
          File dir = new File(dirName);
          if (dir.isDirectory()) {
               String[] child = dir.list();
               System.out.println("hi Iam here");
               for (int i = 0; i < child.length; i++) {
                    readFromDirectory(child.toString());
          File[] files = dir.listFiles();
          if (files == null) {
               System.out.println("There are no files in this Directory ");
          } else {
               for (int j = 0; j < files.length; j++) {
                    String filename = files[j].toString();
                    readFile(filename);
               if (dirName.equalsIgnoreCase("English")
                         || dirName.equalsIgnoreCase("Nonenglish")) {
                    for (int i = 0; i < a2.size(); i++) {
                         keyWord = a2.get(i).toString();
                         //System.out.println("===" + keyWord + "===" + "KeyWord");
                         for (int a = 0; a < text.size(); a++) {
                              String t1 = text.get(a).toString();
                              if (keyWord.equalsIgnoreCase(t1)) {
                                   count++;
                                   //System.out.println("Found at:" + files[j] + " file");
                         keyWCount = count;
                         m1.put(keyWord, keyWCount);
                         //System.out.println(m1.get(keyWord));
                         count = 0;
               } else if (dirName.equalsIgnoreCase("Spanish")) {
                    for (int i = 0; i < a5.size(); i++) {
                         keyWord = a5.get(i).toString();
                         //System.out.println("===" + keyWord + "===" + "KeyWord");
                         for (int a = 0; a < text.size(); a++) {
                              String t1 = text.get(a).toString();
                              if (keyWord.equalsIgnoreCase(t1)) {
                                   count++;
                                   //System.out.println("Found at:" + files[j] + " file");
                         keyWCount = count;
                         m1.put(keyWord, keyWCount);
                         //System.out.println(m1.get(keyWord));
                         count = 0;
                    //System.out.println(m1);
               } else if (dirName.equalsIgnoreCase("French")) {
                    for (int i = 0; i < a3.size(); i++) {
                         keyWord = a3.get(i).toString();
                         //System.out.println("===" + keyWord + "===" + "KeyWord");
                         for (int a = 0; a < text.size(); a++) {
                              String t1 = text.get(a).toString();
                              if (keyWord.equalsIgnoreCase(t1)) {
                                   count++;
                                   //System.out.println("Found at:" + files[j] + " file");
                         keyWCount = count;
                         m1.put(keyWord, keyWCount);
                         //System.out.println(m1.get(keyWord));
                         count = 0;
     public static void main(String[] args) {
          int countText = 0;
          int c;
          test t1 = new test();
          System.out.println("IN NON - ENGLISH DOCUMENTS - P :");
          t1.readFromDirectory("NonEnglish");
          for (int j = 0; j < text.size(); j++) {
               if (!text.get(j).toString().equalsIgnoreCase("")) {
                    countText++;
          c = countText;
          //System.out.println(c);
          //System.out.println(text.size());
          for (int i = 0; i < a2.size(); i++) {
               String word = a2.get(i).toString();
               String m = (m1.get(word)).toString();
               double x = Double.parseDouble(m);
               System.out.println(word + ":" + (x / c) * 100 + " %");
I wanna read text files from directory that contain - lots of other directorys... and by using this code I have error,,,
Can anyone help me plzzz
Edited by: Shamma on Oct 9, 2008 3:32 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

You actually have more than 1 problem: you cannot traverse directories recursively and you get a NullPointerException.
For the first problem you might want to verify what this statement returns:
child.toString() (I honestly don't understand why you are calling toString() on a String object, but that's not the real problem!)
You probably expect it to be a full absolute path.
The NullPointerException has already been pointed out by masijade.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • Read files and folders from a CD

    Hi there,
    I am not sure this is the right forum for my question. Please redirect me if I am in the wrong place.
    I have an AIR app that will be installed from a CD. The client will be changing certain data (like images and video files) regularly and wants to be able to simply write a cd with the new files in a folder and the AIR app install.
    I have searched high ad low for ways to read files from a CD. I could do this if I knew the path, but on each system it is different.
    Is there anything which can tell me what the path to the CD drive is or a way to package AIR so that when it installs it looks for a directory on the CD it is installing from and copies it to the AIR application directory?
    Any help is welcome!
    Thanks in advance,
    Nikki

    Hi - it looks like you can find the CD drive using the getStorageVolumes() method of StorageVolumeInfo:
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/Storag eVolumeInfo.html#getStorageVolumes%28%29
    So you'd  iterate through all the storage volumes, and for the ones that are removable (isRemovable property), check for the existence of some file you know will be on your CD (a unique name), and you would then know that is your install CD.
    -rich

  • 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]

  • 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

  • I just stared having problems with importing files from nikon D810 into LR 5.7 it pop a window saying it can not read files on working on a imac 27" running yosemite on my mac pro after a few times it finally was able to read files and import them into LR

    I just stared having problems with importing files from nikon D810 into LR 5.7 it pop a window saying it can not read files on working on a imac 27" running yosemite on my mac pro after a few times it finally was able to read files and import them into LR I never had this problem before was there some kind of update that could of cause this?

  • Reading files and converting into xml structure

    Hi,
    In my application a client requests for the folder structure information to a server through RMI. The server needs to read files and folders on local machine and convert it into some structure (I am thinking of using xml) and send it back. For eg: I am planning to have Server send back something like:
    <directory name = "parentdirectory">
    <file name = "abc.jpg"/>
    <file name = "def.bmp"/>
    <directory" name = "subdirectory">
    <file name = "hij.jpg"/>
    <file name = "klm.bmp"/>
    </directory>
    </directory>
    It is just the names of the files I am interested in and not the contents. Is this a good approach of sending back the data as a string containg xml definition. Is there any better appproach in terms of performance, memory etc? I am currently planning on using DOM for construction of this structure. Is there a source code for reading and converting the folder structure into xml. Just for your information, the clients gets this information and shows it as a tree structure on the GUI.
    Thanks!!!!

    Is this a good approach of sending back the data as a string containg xml definition. It'll work.
    An alternative, more direct approach is to build a memory representation and send this as argument/return value of an RMI call. You'd need to write classes MyDirectory and MyFile; MyFile has just a name; MyDirectory has a name and a collection of MyDirectory and one of MyFile. Make these classes implement Serializable and you can send them over RMI.
    The effort to write those trivial classes would be less than to implement XML encoding/decoding, and also in terms of runtime performance and memory it will be hard to beat Java's serialization with anything XML-based. In this case I doubt performance/memory are relevant considerations though.
    If for some reason I'd go for sending XML Strings anyway, I wouldn't do the encoding/decoding myself; I'd use XStream to convert Java classes to/from XML and still end up writing the above two classes and be done.
    Sorry if you wanted a simple yes or no :-)

  • HT1923 This article contains the only thing that worked for me.  I also had to sign out of iCloud and uninstall it.  Then I had to delete all files and folders from all of those applications that were under Program Files, Program Files x86, and Users.

    This article contains the only thing that worked for me.  I also had to sign out of iCloud and uninstall it.  Then I had to delete all files and folders from all of those applications that were under Program Files, Program Files x86, Program Data and Users.  My iPhone 4 will now sync with iTunes both in its USB-connected  mini dock and over Wi-Fi.  It's unfortunate (negligent programming on the part of Apple?) that the upgrade to iTunes 11 did not remove all of those files as part of the upgrade process. 

    I am having the same issue....Same address for years - same as USPS - I tried 5 times (3 macs and 2 iPhones) and now i have 5 CHARGES for 1.00 each on my credit card. I took the credit card info off so they don't charge me anymore. How can they charge me yet still not allow me to download free updates and say I have an invalid address? I'm sure I will spend weeks or months trying to get a credit out of these incompetent idiots.

  • How to move all music files and pictures from Ipod nano to a new Windows computer that was not backed up on an external harddrive, only found on my ipod.

    My windows lap top was recently destroyed to the point of no recovery.
    All of my music and picture files are still located on my ipod and I plan on getting a new windows laptop
    Is there anyway that I can transfer my (unpurchased) music files and pictures from my ipod nano to my new computer?
    The pictures are more important that the music, so if only one is possible, I would prefer that
    I appreciate any ones helpful comments

    Copy the iTunes library from your backup as below:
    iTunes: How to move [or copy] your music to a new computer [or another drive] - http://support.apple.com/kb/HT4527
    Quick answer if you use iTunes' default preferences settings:  Copy the entire iTunes folder (and in doing so all its subfolders and files) intact to the other drive.  Start iTunes with the option (shift on Windows) key held down and guide it to the new location of the library.
    Sync with the restored library.

  • How do I transfer files and pictures from my blackberry to my mac

    how do I transfer files and pictures from my blackberry to my mac. My mac does not seem to recognize my blackberry as a devise

    Sounds like a question for Blackberry.
    Check your manual or the Blackberry support site, or Google.

  • I transferred files and photos from MacBook to iMac via ethernet cable and using migration assistant. However every time I transferred, it created a separate user and it transferred all files and photos again and again. So now I have 3 users accounts ?

    I transferred files and photos from MacBook to iMac via ethernet cable and using migration assistant. However every time I transferred, it created a separate user and it transferred all files and photos again and again. So now I have 3 users accounts with all files and photos saved 3 times on same iMac !! Any idea how to avoid this ?

    Yes, you ran into a very understandable problem.
    When you use Migration Assistant, it cannot over-write the account that you happen to be using at the time. Upon migrating accounts, it will tell you that it is going to create a new one, but that message is easy to miss.
    If you have three identical accounts, log in to the account you want to keep, and delete the redundant ones in System Preferences > Users & Groups.
    Now that you know, the easiest way to migrate accounts is to use Setup Assistant when you first get a new Mac. That eliminates any possibility for this to occur, but most people are so eager to use their new Mac that they elect to migrate later.
    That's OK too, but in that case you would want to create a temporary account whose sole purpose is to run Migration Assistant so that you may migrate your old one with an identical name. Be sure to give the new account Administrator privileges. When that finishes you would log in to the newly migrated account, and delete the temporary account.

  • I used MobileMe/idisk to hold all of my files and access from my iPad.  It worked beautifully.  What is the best way to do that now?

    I used MobileMe/idisk to hold all of my files and access from my iPad.  It worked beautifully.  What is the best way to do that now?
    In particular, I am having issues with documents that already exist on my Mac and trying to get my iPad to access them. 

    A popular option seems to be DropBox.

  • I accidentally deleted my photos; how do I download my files and photos from Adobe Online Storage?

    I accidentally deleted my photos; how do I download my files and photos from Adobe Online Storage?

    So i think backup online the the most effective way to protect data loss!
    If you found you have lost your important files, you can just download it from the online server.
    And if you delete your files from hard disk and cannot download it from online storage, there is still another way-data recovery software to recover your lost files.

  • Is there a way to download text files and ebooks from Mac to iPad

    How do I transfer text files and ebooks from my Mac or PC to Ipad

    Read here.
    http://support.apple.com/kb/ht4094
    you can transfer the files to the Apps that support them. pdfs and ebooks can be loaded onto iBooks for instance.
    For regular text files you may need a notes or Text App from the App store.

  • I'm about to transfer the contents of my existing HDD in my (dead PSU) iMac to another computer. I'm just wanting to know if and how I will be able to access my files and documents from the old hard drive once I've made the backup?

    I'm about to transfer the contents of my existing HDD in my (dead PSU) iMac to another computer. I'm just wanting to know if and how I will be able to access my files and documents from the old hard drive once I've made the backup?

    You are not going to be able to run your old system from the backup on this old computer as the hardware is incompatible.
    You need to get a new computer or a refurbished one.

  • I got a java excutable file and document from the instrument supplier I want to use it in labview , I want to Know how to do it , step by step

    I got a java excutable file and document from the instrument supplier I want to use it in labview , I want to Know how to do it , step by step
    Attachments:
    this is a java file changr the jpg to jar .jpg ‏101 KB

    I'd recommend searching the forums to find your answer.  http://forums.ni.com/ni/search?submitted=true&q=java+labview

  • How to read tables and fields from a transaction screen.

    hi all,
    i am having problem in reading fields and tables from a screen, could any one help me in mapping tables from a structure as well.
    it will be very help full if u had any documentation.
    Thanking u
    kiran

    Hi,
    In classical debug mode from CALL STACK tab try to find out the structures holding the screen values .
    Capture the values from transaction using FIELD SYMBOLS .
    See the below code.
    DATA : l_prog_val(50) TYPE c VALUE '(SAPLCTMS)WI[]'.
    **To get the (SAPLCTMS)WI value in this program from call stack.
      FIELD-SYMBOLS: <wi> TYPE ANY.
    *Assign the structure (SAPLCTMS)WI to field symbol
      ASSIGN  (l_prog_val) TO <wi>.
      IF sy-subrc NE 0.
        EXIT.
    *Assign the structure (SAPLCTMS)WI to internal table
      ELSE.
        it_wi[] = <wi>.
      ENDIF.
    Sastry.

Maybe you are looking for

  • Cannot update free apps on my iphone and ipad - Asking for payment details

    I am not able to update my free apps on iphone and ipad, apps like instagram, the weather channel and Farmville2 arent updating. I've been asked credit card info and charged 1$. Why? Can someone please help me out. I have tried logging off and loggin

  • Firewire not responding

    Five days ago, I connected a mini DV camera to my brand new, 3 month old Powermac G5 2.0 Ghz computer and transfered footage to edit in final cut pro. I also connected a DV bridge through firewire to convert some tapes and DVD's into footage my compu

  • Editing a JAR file?

    Hi, I need some insights on how to edit a .jar file using an IDE such as Eclipse. Could someone help me out? Thanks! Epithemeus

  • Searching for discontinuity in an array of timestamps

    Hi, I have a problem with an array of timestamps. I wish to search the array for discontinuous times. i.e. there should be a time each minute and I wish to be able to seperate the data into differant arrays according to these discontinuities. i.e. if

  • Problem with adobe air ajax PUT request

    Hi, Sorry for my english. I would like to send a XML document to a server with AJAX XmlHttpRequest Object. I use the PUT method. Adobe Air sends the PUT request without the contents and set the contents-length header to 0. If I replace the PUT method