Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 48

Thread: How do I write output from one program to several files?

  1. #1
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question How do I write output from one program to several files?

    Hello members, I need to be a able to send output from one program to several files based on some conditions, for instance, my program create several jobs with four different attributes, one of these attribute is priority. There are four different priorities a job could have viz: 'VeryHigh', 'High', 'Medium' and 'Low'. After generating say a thousand jobs with several attributes, my program then select (sort) all the jobs with same priority and print them out in groups.
    Now, I want the output sent to different files.
    Say all jobs with priority 'VeryHigh' shall be sent to FileA, all jobs with priority 'High' shall be sent to FileB, all jobs with priority 'Medium' shall be sent to FileC and all jobs with priority 'Low' shall be sent to FileD.
    Can anyone help on this? How do I go about this task?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    Question moved from Member Introductions section.

    What part of the program are you having problems with?
    The decision part has been specified here:
    all jobs with priority 'VeryHigh' shall be sent to FileA,
    all jobs with priority 'High' shall be sent to FileB,
    all jobs with priority 'Medium' shall be sent to FileC and
    all jobs with priority 'Low' shall be sent to FileD.
    If the files are text files, look at using the PrintWriter class to write them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    atgoodprogram (February 1st, 2013)

  4. #3
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    The part am having problem with is the part that has to do with writing to the various text files: Here is a snippet of the code

    for(JobPriority priority:JobPriority.values())
    {
    System.out.println("Getting all jobs with priority " + priority);
    SingleJob[] jobs = despatcher.getJobs(priority);

    for(SingleJob job:jobs){
    //for ( int i=0; i<jobs.length; i++) {
    if (priority == JobPriority.VeryHigh){
    String outputFileName = "Desktop/testResultsA.txt";

    }

    else if ( priority == JobPriority.High){
    String outputFileName = "/Desktop/testResultsB.txt";

    }


    else if (priority == JobPriority.Medium){
    String outputFileName = "testResultsC.txt";
    }
    else{
    String outputFileName = "testResultsD.txt";

    Am finding it difficult to send outputs to the various files as the system only allows me to specify only one output file at the beginning
    Attempts to specify other output files returns the following message: Variable outputFileName is already defined in method.
    May split declaration into a declaration and assignment.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    Can you make a list of the steps the program should take to do it job? When will it open each file?
    When will it write a record to a file? When will it close each file?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    outputFileName is already defined in method.
    You can only define a variable one time in a particular scope like in a method or class. You can assign a variable different values as often as you like.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    atgoodprogram (February 1st, 2013)

  7. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: How do I write output from one program to several files?

    Why can't you use outputFileName1/2/3 ... etc?

  8. The Following User Says Thank You to Starstreak For This Useful Post:

    atgoodprogram (February 1st, 2013)

  9. #6
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Ok, I think I ll need to declare each output file with different names as suggested above, thanks

    --- Update ---

    <for(JobPriority priority:JobPriority.values())
    {
    System.out.println("Getting all jobs with priority " + priority);
    SingleJob[] jobs = despatcher.getJobs(priority);
     
    for(SingleJob job:jobs){
    //for ( int i=0; i<jobs.length; i++) { 
    if (priority == JobPriority.VeryHigh){
    String outputFileName = "Desktop/testResultsA.txt";
     
    }
     
    else if ( priority == JobPriority.High){
    String outputFileName = "/Desktop/testResultsB.txt"; 
     
    }
     
     
    else if (priority == JobPriority.Medium){
    String outputFileName = "testResultsC.txt";
    }
    else{
    String outputFileName = "testResultsD.txt";>

    I think I ll need to declare each output file with a different name as suggested above. Now, how do I send the output to each file after each condition is met?

  10. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: How do I write output from one program to several files?

    We can't see the whole program, but what I would do is call a separate method (or class) to write the file and pass it the file name as a parameter.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    You should define the String: outputFileName before the if statements, not inside of them where they are local variables known only inside the if. You can assign a value to the String inside the if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    atgoodprogram (February 1st, 2013)

  13. #9
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    This option will be very difficult at my level of programming

    --- Update ---

    Quote Originally Posted by Starstreak View Post
    We can't see the whole program, but what I would do is call a separate method (or class) to write the file and pass it the file name as a parameter.
    This option will be quite difficult for my level of java programming

  14. #10
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: How do I write output from one program to several files?

    I think you should post all the code. Then we can be clearer about what is expected at your level.

  15. #11
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Quote Originally Posted by Starstreak View Post
    I think you should post all the code. Then we can be clearer about what is expected at your level.
    [code=java]
    OK, HERE IS THE FULL CODE
    <
    public class SeveralJobs {
     
        /**
         * @param args the command line arguments
         */
        {
     
           SingleJob[] someJobs = SingleJob.createManyJobs(0, 1000);
     
            System.out.println("Moving through jobs one by one with a loop:");
     
            for(int i=0; i<someJobs.length; i++) // here, the number of jobs specified are created, the method to generate the jobs are from a different file 
            {
                SingleJob job = someJobs[i];
     
                // do soemthing with job 
                // ...
                System.out.println(job.toString()); // I need the entire jobs generated to be written a a main file say 'Jobfile'
     
            }
     
            System.out.println("Creating a dispatcher"); // here the jobs are selected based on their priorities. 
          // Currently, jobs with same priority are printed in same group on the screen. What I need is for the jobs to be printed to several files.
     
            JobDespatcher despatcher = new JobDespatcher();
            despatcher.despatch(someJobs);
     
           // SingleJob[] higestPriorityJobs = despatcher.getJobs(JobPriority.VeryHigh);
     
            for(JobPriority priority:JobPriority.values())
            {
                System.out.println("Getting all jobs with priority " + priority);
                SingleJob[] jobs = despatcher.getJobs(priority);
     
                for(SingleJob job:jobs){
                //for ( int i=0; i<jobs.length; i++) {    
                if (priority == JobPriority.VeryHigh){
                  System.out.println(job.toString()+ "Sending Jobs Immediately"); // I want this set of output to written to FileA instead of screen 
                  }
                 else if ( priority == JobPriority.High){
                  System.out.println(job.toString()+"Sending Jobs a day later"); // I need this set of output written to FileB instead of screen   
                 }
                else if (priority == JobPriority.Medium){
                  System.out.println(job.toString()+"Sending Jobs a week later");// This set of output should be written to FileC instead of screen
                 }
                else{
                 System.out.println(job.toString()+"Sending Jobs a month later");This set of output should be written to FileD instead of screen
                }
              }
           }
        }
    }
     
     
    >

  16. #12
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Here is the full code as was requested:
    <
     
    public class SeveralJobs { // This class uses a method from another class to generate jobs with different sizes and priorities 
     
        /**
         * @param args the command line arguments
         */
        {
     
           SingleJob[] someJobs = SingleJob.createManyJobs(0, 1000); // an array object that generate one thousand jobs here
     
            System.out.println("Moving through jobs one by one with a loop:");
     
            for(int i=0; i<someJobs.length; i++)
            {
                SingleJob job = someJobs[i];
     
                // do soemthing with job 
                // ...
                System.out.println(job.toString()); // HERE THE JOB IS PRINTED TO THE SCREEN BUT I WANT THIS PRINTED TO 'MAINFILE'
     
            }
     
            System.out.println("Creating a dispatcher");
     
            JobDespatcher despatcher = new JobDespatcher(); // HERE JOBS ARE SELECTED FOR GROUP PRINTING BASED ON THEIR PRIORITY
            despatcher.despatch(someJobs);
     
           // SingleJob[] higestPriorityJobs = despatcher.getJobs(JobPriority.VeryHigh);
     
            for(JobPriority priority:JobPriority.values())
            {
                System.out.println("Getting all jobs with priority " + priority);
                SingleJob[] jobs = despatcher.getJobs(priority);
     
                for(SingleJob job:jobs){
                //for ( int i=0; i<jobs.length; i++) {    
                if (priority == JobPriority.VeryHigh){
                  System.out.println(job.toString()+ "Sending Jobs Immediately");// JOBS WITH THIS PRIORITY ARE PRINTED TO SCREEN 
                  // BUT I WANT THIS OUTPUT SENT TO 'FILEA'
                  }
                 else if ( priority == JobPriority.High){
                  System.out.println(job.toString()+"Sending Jobs a day later");  // THIS OUTPUT IS PRINTED TO SCREEN BUT I WANT IT PRINTED TO FILEB  
                 }
                else if (priority == JobPriority.Medium){
                  System.out.println(job.toString()+"Sending Jobs a week later");
                 }
                else{
                 System.out.println(job.toString()+"Sending Jobs a month later");
                }
              }
           }
        }
    }
     
     
    >

  17. #13
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Here is the full code:
    <
     
    public class SeveralJobs { // This class uses a method from another class to generate jobs with different sizes and priorities 
     
        /**
         * @param args the command line arguments
         */
        {
     
           SingleJob[] someJobs = SingleJob.createManyJobs(0, 1000); // an array object that generate one thousand jobs here
     
            System.out.println("Moving through jobs one by one with a loop:");
     
            for(int i=0; i<someJobs.length; i++)
            {
                SingleJob job = someJobs[i];
     
                // do soemthing with job 
                // ...
                System.out.println(job.toString()); // HERE THE JOB IS PRINTED TO THE SCREEN BUT I WANT THIS PRINTED TO 'MAINFILE'
     
            }
     
            System.out.println("Creating a dispatcher");
     
            JobDespatcher despatcher = new JobDespatcher(); // HERE JOBS ARE SELECTED FOR GROUP PRINTING BASED ON THEIR PRIORITY
            despatcher.despatch(someJobs);
     
           // SingleJob[] higestPriorityJobs = despatcher.getJobs(JobPriority.VeryHigh);
     
            for(JobPriority priority:JobPriority.values())
            {
                System.out.println("Getting all jobs with priority " + priority);
                SingleJob[] jobs = despatcher.getJobs(priority);
     
                for(SingleJob job:jobs){
                //for ( int i=0; i<jobs.length; i++) {    
                if (priority == JobPriority.VeryHigh){
                  System.out.println(job.toString()+ "Sending Jobs Immediately");// THIS SET OF JOBS ARE PRINTED TO SCREEN 
                  // BUT I WANT THIS OUTPUT SENT TO 'FILEA'
                  }
                 else if ( priority == JobPriority.High){
                  System.out.println(job.toString()+"Sending Jobs a day later");  // THIS SET OF OUTPUT IS PRINTED TO SCREEN 
                  // BUT I WANT IT PRINTED TO 'FILEB'  
                 }
                else if (priority == JobPriority.Medium){
                  System.out.println(job.toString()+"Sending Jobs a week later"); // THIS SET OF OUTPUT IS ALSO PRINTED ON SCREEN 
                 //BUT I NEED THEM PRINTED TO 'FILEC'
                 }
                else{
                 System.out.println(job.toString()+"Sending Jobs a month later"); // AND I NEED THIS SET OF OUTPUT PRINTED TO 'FILED'
                }
              }
           }
        }
    }
     
     
    >

  18. #14
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Starstreak, I ve posted the entire code twice but I can't find it, maybe the admins have not authorized it

  19. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    Your posts have been made visible now.

    Is this what you are trying to do:
    Open some output files to write items to
    loop through a list of items
    write item to one of the files based on its content
    end loop
    close files
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    atgoodprogram (February 1st, 2013)

  21. #16
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Yes Norm, that is exactly what I want to do

    --- Update ---

    Quote Originally Posted by Norm View Post
    Your posts have been made visible now.

    Is this what you are trying to do:
    Open some output files to write items to
    loop through a list of items
    write item to one of the files based on its content
    end loop
    close files
    Yes Norm, That is exactly what am trying to do

  22. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    Now try writing the code to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #18
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Quote Originally Posted by Norm View Post
    Now try writing the code to do it.
    I ve written the code but only one file is been written to the several output files.
    Here is what I did:
    <
    import java.io.FileNotFoundException;
    import utilities.FileOutput;
     
    public class SendToOutputFile { 
     
     
     
       /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws FileNotFoundException  
        {
     
            SingleJob[] someJobs = SingleJob.createManyJobs(0, 30);
     
            System.out.println("Moving through jobs one by one with a loop:");
            FileOutput file = new FileOutput("C:/Users/Desktop/TextDoc2.txt", null);
            for(int i=0; i<someJobs.length; i++)
         {
            SingleJob job = someJobs[i];
            String jobAsString = job.toString(); 
              try
                {
                  file.open();
                  file.println(jobAsString, true);
     
                }
              catch(Exception e)
                {
              e.printStackTrace(System.err);
                 }
              finally
                {
                   file.close();
                 }
     
     
            }     
            System.out.println("Creating a dispatcher");
     
            JobDespatcher despatcher = new JobDespatcher();
            despatcher.despatch(someJobs);
     
           for(JobPriority priority:JobPriority.values())
            {
                System.out.println("Getting all jobs with priority " + priority);
                SingleJob[] jobs = despatcher.getJobs(priority);
     
               for(SingleJob job:jobs){
                   String jobAsString = job.toString();
     
                if (priority == JobPriority.VeryHigh){
                    FileOutput fileA = new FileOutput("C:/Users/Desktop/TextDocA.txt", null);
     
            try
            {
                fileA.open();
                fileA.println(jobAsString, true);
     
                }
            catch(Exception e)
            {
                e.printStackTrace(System.err);
            }
            finally
            {
                fileA.close();
            }
     
                }
                 else if ( priority == JobPriority.High){
                      FileOutput fileB = new FileOutput("C:/Users/Desktop/TextDocB.txt", null);
     
            try
            {
                fileB.open();
                fileB.println(jobAsString, true);
     
                }
            catch(Exception e)
            {
                e.printStackTrace(System.err);
            }
            finally
            {
                fileB.close();
            }
            }
           else if (priority == JobPriority.Medium){
                FileOutput fileC = new FileOutput("C:/Users/Desktop/TextDocC.txt", null);
     
            try
            {
                fileC.open();
                fileC.println(jobAsString, true);
     
            }
            catch(Exception e)
            {
                e.printStackTrace(System.err);
            }
            finally
            {
                fileC.close();
            }
            }
     
                else
           {
                FileOutput fileD = new FileOutput("C:/Users/Desktop/TextDocD.txt", null);
     
            try
            {
                fileD.open();
                fileD.println(jobAsString, true);
     
                }
            catch(Exception e)
            {
                e.printStackTrace(System.err);
            }
            finally
            {
                fileD.close();
            }
     
            }
            }
            }
            }
    }>


    --- Update ---

    Sorry, only one job each is been written to the output files. My code is posted above
    Last edited by Norm; February 1st, 2013 at 03:07 PM. Reason: Removed spaces from code tag

  24. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    Did you notice after posting the above that the code was not formatted?

    only one file is been written to the several output files.
    Is that what you wanted to happen?
    If not, please explain what you want the code to do that it did not do.

    only one job each is been written to the output files
    Did you look at the steps I posted in post #15?
    Did you try to write the code to follow those steps?


    You need to format the code so that the }s are properly positioned
            }
     
            }
            }
            }
            }
    The }s should NOT be all in a column. The nested statements within each pair of {}s should be indented 3-4 spaces so the logic of the code can be read and understood. The code that was posted is very hard to read and understand.

    Please fix the formatting of the {}s.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #20
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    The code was supposed to select the generated jobs from the first file and write them all to the other files based on their priority, for instance all jobs with priority high were supposed to be written to one output file and so for all other jobs. For now, only one job each from each category is been written to the specified output file

  26. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    Did you see the steps I suggested for the program in post#15?
    Here they are again:
    Open some output files to write items to<<<<The files are all opened BEFORE the loop
    loop through a list of items
    write item to one of the files based on its content
    end loop
    close files <<<<<<<<<< The close is after the loop



    Also read post#19 about the code's formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    atgoodprogram (February 2nd, 2013)

  28. #22
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Quote Originally Posted by Norm View Post
    Did you notice after posting the above that the code was not formatted?


    Is that what you wanted to happen?
    If not, please explain what you want the code to do that it did not do.

    Did you look at the steps I posted in post #15?
    Did you try to write the code to follow those steps?


    You need to format the code so that the }s are properly positioned
            }
     
            }
            }
            }
            }
    The }s should NOT be all in a column. The nested statements within each pair of {}s should be indented 3-4 spaces so the logic of the code can be read and understood. The code that was posted is very hard to read and understand.

    Please fix the formatting of the {}s.
    Ok, here is the formatted code:
    <
    import java.io.FileNotFoundException;
    import utilities.FileOutput;
     
    public class SendToOutputFile 
    { 
     
     
     
       /**
         * @param args the command line arguments
         */
          public static void main(String[] args) throws FileNotFoundException  
         {
     
            SingleJob[] someJobs = SingleJob.createManyJobs(0, 30);
     
            System.out.println("Moving through jobs one by one with a loop:");
            FileOutput file = new FileOutput("C:/Users/Desktop/TextDoc2.txt", null);
            for(int i=0; i<someJobs.length; i++)
            {
               SingleJob job = someJobs[i];
               String jobAsString = job.toString(); 
               try
                  {
                   file.open();
                   file.println(jobAsString, true);
     
                   }
                   catch(Exception e)
                   {
                     e.printStackTrace(System.err);
                   }
                   finally
                   {
                   file.close();
                   }
     
     
            }     
               System.out.println("Creating a dispatcher");
     
               JobDespatcher despatcher = new JobDespatcher();
               despatcher.despatch(someJobs);
     
            for(JobPriority priority:JobPriority.values())
            {
                System.out.println("Getting all jobs with priority " + priority);
                SingleJob[] jobs = despatcher.getJobs(priority);
     
               for(SingleJob job:jobs)
                 {
                   String jobAsString = job.toString();
     
                   if (priority == JobPriority.VeryHigh)
                     {
                        FileOutput fileA = new FileOutput("C:/Users/Desktop/TextDocA.txt", null);
     
                      try
                         {
                            fileA.open();
                            fileA.println(jobAsString, true);
     
                          }
                             catch(Exception e)
                              {
                                 e.printStackTrace(System.err);
                              }
                              finally
                           {
                            fileA.close();
                           }
     
                    }
                       else if ( priority == JobPriority.High){
                       FileOutput fileB = new FileOutput("C:/Users/Desktop/TextDocB.txt", null);
     
                          try
                             {
                                fileB.open();
                                fileB.println(jobAsString, true);
     
                              }
                              catch(Exception e)
                                  {
                                     e.printStackTrace(System.err);
                                   }
                                 finally
                                 {
                                    fileB.close();
                                 }
                             }
                      else if (priority == JobPriority.Medium)
                      {
                         FileOutput fileC = new FileOutput("C:/Users/Desktop/TextDocC.txt", null);
     
                         try
                           {
                               fileC.open();
                               fileC.println(jobAsString, true);
     
                            }
                            catch(Exception e)
                                {
                                   e.printStackTrace(System.err);
                                 }
                          finally
                             {
                               fileC.close();
                              }
                         }
     
                   else
                      {
                         FileOutput fileD = new FileOutput("C:/Users/Desktop/TextDocD.txt", null);
     
                       try
                          {
                             fileD.open();
                             fileD.println(jobAsString, true);
     
                           }
                           catch(Exception e)
                             {
                                 e.printStackTrace(System.err);
                             }
                             finally
                           {
                              fileD.close();
                           }
     
                        }
                  }
             }
        }
    }
    >

  29. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I write output from one program to several files?

    That makes it easier to read and understand.

    Now you need to move the open and close of the file outside the loop.
    Every time you open a file, write to it and close it, the file's previous contents is replaced with the new contents.
    If you don't understand my answer, don't ignore it, ask a question.

  30. The Following User Says Thank You to Norm For This Useful Post:

    atgoodprogram (February 1st, 2013)

  31. #24
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Quote Originally Posted by Norm View Post
    That makes it easier to read and understand.

    Now you need to move the open and close of the file outside the loop.
    Every time you open a file, write to it and close it, the file's previous contents is replaced with the new contents.
    Ok, thanks, I ll try that out and give feedback

  32. #25
    Junior Member atgoodprogram's Avatar
    Join Date
    Jan 2013
    Posts
    27
    My Mood
    Amazed
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: How do I write output from one program to several files?

    Quote Originally Posted by atgoodprogram View Post
    Ok, thanks, I ll try that out and give feedback
    I ve tried it and Its not working, its bringing a nullpointer error, or nothing is being written to all the outputfiles, even the first file (TextDoc2) where all the jobs are first generated into has but only one job in it then that one job alone is sent to its category of output file.
    Here is the code :
    <
     
    import java.io.FileNotFoundException;
    import utilities.FileOutput;
     
    public class SendToOutputFile 
    { 
     
     
     
       /**
         * @param args the command line arguments
         */
          public static void main(String[] args) throws FileNotFoundException  
         {
            FileOutput file = new FileOutput("C:/Users/Desktop/TextDoc2.txt", null);
     
            SingleJob[] someJobs = SingleJob.createManyJobs(0, 30);
     
            System.out.println("Moving through jobs one by one with a loop:");
     
            for(int i=0; i<someJobs.length; i++)
            {
               SingleJob job = someJobs[i];
               String jobAsString = job.toString(); 
               try
                  {
                   file.open();
                   file.println(jobAsString, true);
     
                   }
                   catch(Exception e)
                   {
                     e.printStackTrace(System.err);
                   }
     
     
     
            }     
               System.out.println("Creating a dispatcher");
     
               JobDespatcher despatcher = new JobDespatcher();
               despatcher.despatch(someJobs);
     
            for(JobPriority priority:JobPriority.values())
     
            {
     
               FileOutput fileA = new FileOutput("C:/Users/Desktop/TextDocA.txt", null);
               FileOutput fileB = new FileOutput("C:/Users/Desktop/TextDocB.txt", null); 
               FileOutput fileC = new FileOutput("C:/Users/Desktop/TextDocC.txt", null);
               FileOutput fileD = new FileOutput("C:/Users/Desktop/TextDocD.txt", null); 
                System.out.println("Getting all jobs with priority " + priority);
                SingleJob[] jobs = despatcher.getJobs(priority);
                fileA.open();
                fileB.open();
                fileC.open();
                fileD.open();
               for(SingleJob job:jobs)
     
                 {
                   String jobAsString = job.toString();
     
                   if (priority == JobPriority.VeryHigh)
                     {
     
                      try
                         {
     
                            fileA.println(jobAsString, true);
     
                          }
                      catch(Exception e)
                       {
                          e.printStackTrace(System.err);
                       }
     
     
                     }
                   else if ( priority == JobPriority.High){
     
                      try
                          {
                             fileB.println(jobAsString, true);
                           }
                      catch(Exception e)
                          {
                             e.printStackTrace(System.err);
                          }
     
                   }
                  else if (priority == JobPriority.Medium)
                     {
     
                        try
                           {
                               fileC.println(jobAsString, true);
                           }
                       catch(Exception e)
                          {
                             e.printStackTrace(System.err);
                           }
     
                   }
     
                   else
                      {
     
                       try
                          {
                             fileD.println(jobAsString, true);
     
                           }
                         catch(Exception e)
                             {
                                 e.printStackTrace(System.err);
                             }
     
     
     
     
                 finally
                      {
     
     
                        fileA.close();
                        fileB.close();
                        fileC.close();
                        fileD.close();
                      }
                   }
               }
            }
         }
    }
     
     
     
     
    >

Page 1 of 2 12 LastLast

Similar Threads

  1. Input and Output files?
    By jean28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2012, 02:10 PM
  2. How to write my output to a text file :( ?
    By dynamix24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2012, 06:26 PM
  3. [SOLVED] Why won't it write the output away?
    By Purple01 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 12th, 2012, 08:00 AM
  4. Replies: 0
    Last Post: April 6th, 2012, 04:14 PM