2 Replies Latest reply: Jan 1, 2008 6:41 PM by 807603 RSS

    Directories

    807603
      Ok, so i have a code that gets all the files in a directory, and prints it to the screen. What i want it to do is print all the info from the files to one single file. This is what I have so far.
      import java.io.*;
      import java.util.*;
      import java.lang.*;
      
      
      public class DirectoryReader
          {
              public static void main(String[] args)
                  {
                      try
                          {
                             File Directory = new File("./ZymusScape/Shops/");
                             File[] FileList = Directory.listFiles();
                                    
                             FileWriter OutputFile = new FileWriter("./ZymusScape/Shops/Shops.Cache");
                             BufferedWriter Writer = new BufferedWriter(OutputFile);
      
                             for(int FileIndex = 1; FileIndex < FileList.length - 1; FileIndex++)
                                 {
                                     String FileName = FileList[FileIndex].getName();
                                              
                                     FileReader InputFile = new FileReader("./ZymusScape/Shops/" +FileName);
                                     BufferedReader Reader = new BufferedReader(InputFile);
                                                                                 
                                     String Line = Reader.readLine();
                                              
                                     while(Line != null)
                                         {
                                             System.out.println(Line);
                                             Writer.write(Line);//What I want it to do is read the info from each individual line, and write to the Output file
                                             Line = Reader.readLine();//If i don't have this here, it just repeats the first line of the First file
                                         }
                                 }
                         }
      
                     catch(Exception IncorrectInfo)
                         {
                             System.out.println("Error: " +IncorrectInfo.getMessage());
                         }
          }
      Any ideas on how to get the desired outcome?

      Also, I'm having trouble deleting directories. I would appreciate help on those too.

      Edited by: Zymus on Jan 1, 2008 1:33 PM
        • 1. Re: Directories
          DrClap
          Yes, I know how to get the desired outcome. But I wouldn't do it starting from the code you have there. So how about explaining what that code actually does, and how it differs from the desired outcome?

          Oh, and about deleting directories. You didn't say whether you were having problems deleting them via Java code or from the command line. Let's assume it's via Java, since that's the topic of this forum. Create a File object that represents the directory and call its delete() method. Note that you can't delete a directory which contains files... that's just my guess at your actual problem which you didn't mention.
          • 2. Re: Directories
            807603
            DrClap wrote:
            Yes, I know how to get the desired outcome. But I wouldn't do it starting from the code you have there. So how about explaining what that code actually does, and how it differs from the desired outcome?

            Oh, and about deleting directories. You didn't say whether you were having problems deleting them via Java code or from the command line. Let's assume it's via Java, since that's the topic of this forum. Create a File object that represents the directory and call its delete() method. Note that you can't delete a directory which contains files... that's just my guess at your actual problem which you didn't mention.
            okay, here's my replaced code:
            import java.io.*;
            import java.util.*;
            import java.lang.*;
             
             
            public class DirectoryReader
                {
                    public static void main(String[] args)
                        {
                            try
                                {
                                   File Directory = new File("./ZymusScape/Shops/");//Go to this directory
                                   File[] FileList = Directory.listFiles();//List a File[] of all the files in the Directory
                                          
                                   FileWriter OutputFile = new FileWriter("./ZymusScape/Shops/Shops.Cache");//Make a new File Called Shops.Cache
                                   BufferedWriter Writer = new BufferedWriter(OutputFile);
             
                                   for(int FileIndex = 1; FileIndex < FileList.length - 1; FileIndex++)
                                       {
                                           String FileName = FileList[FileIndex].getName();//Change the FileName so that it equals the Name of the index of the directory
                                                    
                                           FileReader InputFile = new FileReader("./ZymusScape/Shops/" +FileName);//read the File
                                           BufferedReader Reader = new BufferedReader(InputFile);
                                                                                       
                                           String Line = Reader.readLine();//Read each Line
                                                    
                                           while(Line != null)//While he line isnt null
                                               {
                                                   System.out.println(Line);
                                                   Writer.write(Line);//*********What I want it to do is read the info from each individual line, and write to the Output file*********
                                                   Line = Reader.readLine();//If i don't have this here, it just repeats the first line of the First file
                                               }
                                       }
                               }
             
                           catch(Exception IncorrectInfo)
                               {
                                   System.out.println("Error: " +IncorrectInfo.getMessage());//Print the error to the screen
                               }
                }
            The comment with the '*'s is what Prints it to the screen. so i tried doing
            Writer.write(Line);
            which i though would print the line to the output file, but it doesnt.

            And the Deleting Directories, I made a loop the makes a folder called Test, and makes 250 folders in Test. After that, it makes 250 folders in each of those folders. and there are no files in any of those folders. but i can't delete any of the directories.