This discussion is archived
5 Replies Latest reply: Jan 25, 2008 5:24 AM by 807603 RSS

parse one file and export to many files

807603 Newbie
Currently Being Moderated
Hi friends!! I have a some problem. I'm Java beginners
Task I parse the mailbox file and I want export it to many *.eml files.
For me problem is - varible visabilyty. This commented code is wrong The local variable writer may not have been initialized, My quistion is how can I write parsing data to many files? Ore make write visible?
   while ((current_line=reader.readLine()) != null) {
             if (current_line.startsWith("From MAILER_DAEMON")) {
                  beginFile = false;
                  new_Envelop = false;
               }
             else if ((beginFile)&&(current_line.startsWith("From "))) {
                  // make new EML file  -  *1*
                     writer = new BufferedWriter(new FileWriter(dir+"\\"+mail_quantity+".eml"));
                  mail_quantity++;
                  beginFile = false;
                  new_Envelop = true;

               }// Check does previous line is "new line" behind "From " 
             else if ((current_line.startsWith("From "))&&(previous_line.equals(""))) {
                     // writer.flush();writer.close();
                  // close previous EML file - letter is end. *2*
                     // writer = new BufferedWriter(new FileWriter(dir+"\\"+mail_quantity+".eml"));
                  // make new EML file - for new letter *3*
                  new_Envelop=true;
                  mail_quantity++;
               } 
             
             
             if (new_Envelop) {
               // add letter lines to EML file *4*
                  //writer.write(current_line);
                  //writer.flush();
               }
             
             previous_line=current_line;
        }
  • 1. Re: parse one file and export to many files
    807603 Newbie
    Currently Being Moderated
    spoil wrote:
    This commented code is wrong The local variable writer may not have been initialized,
    It is just an IDE warning, not an error.

    Imagine this situation:
    Writer writer;
    if (false) {
        writer = new Writer();
    } else if (true) {
        writer.close();
    }
    With this you risks a NPE.
  • 2. Re: parse one file and export to many files
    800323 Newbie
    Currently Being Moderated
    spoil wrote:
    Hi friends!! I have a some problem. I'm Java beginners
    Task I parse the mailbox file and I want export it to many *.eml files.
    For me problem is - varible visabilyty. This commented code is wrong The local variable writer may not have been initialized, My quistion is how can I write parsing data to many files? Ore make write visible?
       while ((current_line=reader.readLine()) != null) {
         if (current_line.startsWith("From MAILER_DAEMON")) {
              beginFile = false;
              new_Envelop = false;
                   }
         else if ((beginFile)&&(current_line.startsWith("From "))) {
              // make new EML file  -  *1*
              writer = new BufferedWriter(new FileWriter(dir+"\\"+mail_quantity+".eml"));
              mail_quantity++;
              beginFile = false;
              new_Envelop = true;
    
                   }// Check does previous line is "new line" behind "From " 
         else if ((current_line.startsWith("From "))&&(previous_line.equals(""))) {
              // writer.flush();writer.close();
              // close previous EML file - letter is end. *2*
              // writer = new BufferedWriter(new FileWriter(dir+"\\"+mail_quantity+".eml"));
              // make new EML file - for new letter *3*
              new_Envelop=true;
              mail_quantity++;
                   } 
         
         
         if (new_Envelop) {
                   // add letter lines to EML file *4*
              //writer.write(current_line);
              //writer.flush();
                   }
         
         previous_line=current_line;
    }
    The error is simply saying that there is a path that could access the "writer" variable before it is initialized to some value.
    You can simply initialize it to null before you get to this if statement; however, that may not be the default value that makes sense for you code. It is hard to say much more with the information provided.
  • 3. Re: parse one file and export to many files
    800323 Newbie
    Currently Being Moderated
    BalusC wrote:
    spoil wrote:
    This commented code is wrong The local variable writer may not have been initialized,
    It is just an IDE warning, not an error.

    Imagine this situation:
    Writer writer;
    if (false) {
    writer = new Writer();
    } else if (true) {
    writer.close();
    }
    With this you risks a NPE.
    This may actually be a compiler error not just a warning.
  • 4. Re: parse one file and export to many files
    807603 Newbie
    Currently Being Moderated
    jbish wrote:
    This may actually be a compiler error not just a warning.
    You're right. The situation which I've had in my head is
    Writer writer = null;
  • 5. Re: parse one file and export to many files
    807603 Newbie
    Currently Being Moderated
    writer = null;
    solve problem!
    Thanks!!!