Results 1 to 19 of 19
  1. #1
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Append a single text file on button click through jTextField [a complete form]

    Hi guys,
    I am stuck badly as I am using java.nio the new java Input Output file system packages. I just wanted to implement simple coding that will append my text file. Below is the code,

    Java Code:
    private void btnSaveEmailActionPerformed(java.awt.event.ActionEvent evt) {                                             
            // TODO add your handling code here:
            try {
                x = new Formatter("Clients.txt");
                
                //Creates a new file by using Formatter class
                           
            } catch (FileNotFoundException ex) {
                Logger.getLogger(USF_JFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
           
            Integer tokNo=Integer.parseInt(txtTokenNo.getText());  //calls getText method to get the text from the txtTokenNo textbox
            
            
            x.format("%-25s %-20d \n" + System.getProperty("line.separator"),"Token No: ",tokNo);  //puts the formatted text in the created text file
            Integer EmpId=Integer.parseInt(txtEmployeeID.getText()); //calls getText method to get the text from the txtEmployeeID textbox
            
            
            x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Employee id: ",EmpId); //puts the formatted text in the created text file
            Date getTrnDate=jDateChooser.getDate(); //calls getText method to get the text from the txtTranDate textbox
            String trnDate=String.format("%1$td-%1$tm-%1$tY",getTrnDate);
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Transaction date: ",trnDate); //puts the formatted text in the created text file
            String roomNo=txtRoomNo.getText(); //calls getText method to get the text from the txtRoomNo textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Room No: ",roomNo); //puts the formatted text in the created text file
            Integer extNo=Integer.parseInt(txtExtNo.getText()); //calls getText method to get the text from the txExtNo textbox
            
            
            x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Extension is: ",extNo); //puts the formatted text in the created text file
            String empName=txtEmployeeName.getText(); //calls getText method to get the text from the txtEmployeeName textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Employee Name: ",empName); //puts the formatted text in the created text file
            String loc=txtLocation.getText(); //calls getText method to get the text from the txtLocation textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Location is: ",loc); //puts the formatted text in the created text file
            String dptName=txtDeptName.getText(); //calls getText method to get the text from the txtDeptName textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Department Name: ",dptName); //puts the formatted text in the created text file
            String probType=(String)cbProbType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbProbType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Problem Type: ",probType); //puts the formatted text in the created text file
            String urgType=(String)cbUrgencyType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbUrgencyType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Urgency Type: ",urgType); //puts the formatted text in the created text file
            String appType=(String)cbAppType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbAppType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Application Type: ",appType); //puts the formatted text in the created text file
            String jStatusType=(String)cbJobStatusType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbJobStatusType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Job Status Type: ",jStatusType); //puts the formatted text in the created text file
            String desc=txtDescription.getText(); //calls getText method to get the text from the txtDescription textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Description: ",desc); //puts the formatted text in the created text file
           
          x.close();       
            
        }
    I want to use the java version 7 File system like
    Java Code:
    BufferedWriter writer =  
        Files.newBufferedWriter( path, Charset.defaultCharset(), 
                                                      StandardOpenOption.CREATE);
    writer.write(content, 0, content.length());
    I really don't have any clue how?

    Please help :(

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Stupid question: Did you try to use StandardOpenOption.APPEND ?
    I like likes!

  3. #3
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Sierra that's why I posted on the thread. I mean how to do it. From where does it will fix. I am really stuck.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Quote Originally Posted by Sierra View Post
    Stupid question: Did you try to use StandardOpenOption.APPEND ?
    Quote Originally Posted by johnsmithcon View Post
    Sierra that's why I posted on the thread. I mean how to do it. From where does it will fix. I am really stuck.
    Was it so difficult to simply answer Sierra's question? There are only two possible answers.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    It is not about the answers DarrylBurke. I know at the back-end I need to have a db. But right now I just wanted to append a simple single text file. If I click a save button how can it be done. I mean where do I write the code and how any clue?

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Append a single text file on button click through jTextField [a complete form]

    No one spoke about any database... it is his name XD
    As you can see we are not sure where your problem actually is, which means you did not make it clear enough for me at least. We cannot see all your problems from this far...
    You said you want to append a file and posted code that would be suited to create a file and write anything to it. I tried to see where the problem is... I asked if you tried instead of CREATing you tried APPENDing as I thought your problem was to append as you said.

    So maybe to get on the productive side:
    - You need a button (JButton in a JFrame)
    - The button implements an ActionListener to capture actions when it is pressed.
    - There in the listener you implement your BufferedWriter almost as you wrote it down and write the String you created in your long formatting method to your file.
    - I suggest you flush() the file buffer in between appends

    So is that what you expected to hear?

    Basically you have all you need but you need to add the code you posted below to your method above... right? Did you try that?
    Or is your problem you do not know how to join your strings to form something you may write in the file? You see me guessing...
    Last edited by Sierra; 01-08-2013 at 03:56 PM. Reason: added brackets to flush...
    I like likes!

  7. #7
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Thanks a lot Sierra, let me work on it and I'll revert you back. Your a big helper.

  8. #8
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    By the way the button is already there I have used NetBeans IDE 7.2.1

  9. #9
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Yes I can see that and your actionlistener is the method you posted obviously. So is your problem getting the formatter string to an output stream or what?
    Do you intend to replace the existing file transaction? Then you just remove the "client.txt" and use toString() from the Formatter class... to write your string?
    That's better?
    I like likes!

  10. #10
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    The Client.txt file is still there. I don't want to remove it. I just need to append it. I mean to say is that I want to add another text after inserting a blank line in the same Client.txt file.

  11. #11
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    And I simply want to do it through java.nio package. But I am little confused where to put the code and how to be very honest.

  12. #12
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Append a single text file on button click through jTextField [a complete form]

    The where depends on what you are actually trying to achieve, that is why I ask so many questions.
    So if I understand you correctly you want:
    - After writing the file using the Formatter (which is in this case a extension of BufferedWriter) you want to reopen the file
    - This reopening shall be in append mode
    - You want to add a custom text content afterwards

    Easier:
    - You just take the buffered Writer of the Formatter and write to it BEFORE closing it ( x.close() )
    (I did not check if that is possible though or if the methods of the writer are hidden in that class.)

    If you want to do it always after the button is pressed and the content is written then you append your code snippet at the end of the long actionlistener method. You reopen the file but (as I asked at first) in APPEND mode and write text, lines or other stuff to it. After that you close it. Done.
    For the Path parameter you will need to use Paths.get(...) probably.
    Last edited by Sierra; 01-08-2013 at 06:29 PM.
    I like likes!

  13. #13
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    private void btnSaveEmailActionPerformed(java.awt.event.ActionE vent evt) {
    // TODO add your handling code here:
    try {
    x = new Formatter("Clients.txt");

    //Creates a new file by using Formatter class

    } catch (FileNotFoundException ex) {
    Logger.getLogger(USF_JFrame.class.getName()).log(L evel.SEVERE, null, ex);
    }

    Path path = FileSystems.getDefault().getPath(".", "Clients.txt");
    String line = null;
    BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset());
    while ( (line = reader.readLine()) != null ) {

    Integer tokNo=Integer.parseInt(txtTokenNo.getText()); //calls getText method to get the text from the txtTokenNo textbox


    x.format("%-25s %-20d \n" + System.getProperty("line.separator"),"Token No: ",tokNo); //puts the formatted text in the created text file
    Integer EmpId=Integer.parseInt(txtEmployeeID.getText()); //calls getText method to get the text from the txtEmployeeID textbox


    x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Employee id: ",EmpId); //puts the formatted text in the created text file
    Date getTrnDate=jDateChooser.getDate(); //calls getText method to get the text from the txtTranDate textbox
    String trnDate=String.format("%1$td-%1$tm-%1$tY",getTrnDate);


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Transaction date: ",trnDate); //puts the formatted text in the created text file
    String roomNo=txtRoomNo.getText(); //calls getText method to get the text from the txtRoomNo textbox


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Room No: ",roomNo); //puts the formatted text in the created text file
    Integer extNo=Integer.parseInt(txtExtNo.getText()); //calls getText method to get the text from the txExtNo textbox


    x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Extension is: ",extNo); //puts the formatted text in the created text file
    String empName=txtEmployeeName.getText(); //calls getText method to get the text from the txtEmployeeName textbox


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Employee Name: ",empName); //puts the formatted text in the created text file
    String loc=txtLocation.getText(); //calls getText method to get the text from the txtLocation textbox


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Location is: ",loc); //puts the formatted text in the created text file
    String dptName=txtDeptName.getText(); //calls getText method to get the text from the txtDeptName textbox


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Department Name: ",dptName); //puts the formatted text in the created text file
    String probType=(String)cbProbType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbProbType DropDownList


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Problem Type: ",probType); //puts the formatted text in the created text file
    String urgType=(String)cbUrgencyType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbUrgencyType DropDownList


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Urgency Type: ",urgType); //puts the formatted text in the created text file
    String appType=(String)cbAppType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbAppType DropDownList


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Application Type: ",appType); //puts the formatted text in the created text file
    String jStatusType=(String)cbJobStatusType.getSelectedIte m(); //calls getSelectedItem method to get the list item from the cbJobStatusType DropDownList


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Job Status Type: ",jStatusType); //puts the formatted text in the created text file
    String desc=txtDescription.getText(); //calls getText method to get the text from the txtDescription textbox


    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Description: ",desc); //puts the formatted text in the created text file

    BufferedWriter writer = Files.newBufferedWriter( path, Charset.defaultCharset(), StandardOpenOption.CREATE);
    writer.write(x, 0, x.)); //Stuck here

    x.close();
    }
    }

    Still getting problems Sierra! But I am really thankful for your replies.

  14. #14
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Java Code:
    private void btnSaveEmailActionPerformed(java.awt.event.ActionEvent evt) {                                             
            // TODO add your handling code here:
            String line = null;
            Path path = FileSystems.getDefault().getPath(".", "Clients.txt");
            BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset());
            while ( (line = reader.readLine()) != null ) {
            BufferedWriter writer =  Files.newBufferedWriter( path, Charset.defaultCharset(), StandardOpenOption.CREATE);
            
            try {
                x = new Formatter("Clients.txt");
                
                //Creates a new file by using Formatter class
                           
            } catch (FileNotFoundException ex) {
                Logger.getLogger(USF_JFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            Integer tokNo=Integer.parseInt(txtTokenNo.getText());  //calls getText method to get the text from the txtTokenNo textbox
            
            
            x.format("%-25s %-20d \n" + System.getProperty("line.separator"),"Token No: ",tokNo);  //puts the formatted text in the created text file
            Integer EmpId=Integer.parseInt(txtEmployeeID.getText()); //calls getText method to get the text from the txtEmployeeID textbox
            
            
            x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Employee id: ",EmpId); //puts the formatted text in the created text file
            Date getTrnDate=jDateChooser.getDate(); //calls getText method to get the text from the txtTranDate textbox
            String trnDate=String.format("%1$td-%1$tm-%1$tY",getTrnDate);
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Transaction date: ",trnDate); //puts the formatted text in the created text file
            String roomNo=txtRoomNo.getText(); //calls getText method to get the text from the txtRoomNo textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Room No: ",roomNo); //puts the formatted text in the created text file
            Integer extNo=Integer.parseInt(txtExtNo.getText()); //calls getText method to get the text from the txExtNo textbox
            
            
            x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Extension is: ",extNo); //puts the formatted text in the created text file
            String empName=txtEmployeeName.getText(); //calls getText method to get the text from the txtEmployeeName textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Employee Name: ",empName); //puts the formatted text in the created text file
            String loc=txtLocation.getText(); //calls getText method to get the text from the txtLocation textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Location is: ",loc); //puts the formatted text in the created text file
            String dptName=txtDeptName.getText(); //calls getText method to get the text from the txtDeptName textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Department Name: ",dptName); //puts the formatted text in the created text file
            String probType=(String)cbProbType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbProbType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Problem Type: ",probType); //puts the formatted text in the created text file
            String urgType=(String)cbUrgencyType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbUrgencyType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Urgency Type: ",urgType); //puts the formatted text in the created text file
            String appType=(String)cbAppType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbAppType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Application Type: ",appType); //puts the formatted text in the created text file
            String jStatusType=(String)cbJobStatusType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbJobStatusType DropDownList
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Job Status Type: ",jStatusType); //puts the formatted text in the created text file
            String desc=txtDescription.getText(); //calls getText method to get the text from the txtDescription textbox
            
            
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Description: ",desc); //puts the formatted text in the created text file
               
          x.close();       
          }   
        }
    Last edited by johnsmithcon; 01-09-2013 at 09:11 AM.

  15. #15
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Even this is not working as again i am stuck. Any help please..

  16. #16
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Append a single text file on button click through jTextField [a complete form]

    First you need to make yourself clearer - "it does not work" is not something we can work with. If something happens that is not expected describe that to us as I cannot reproduce your errors here.
    IF you encounter error messages, put them here in full.

    I get the feeling that you do actually not understand what you are doing where - you want to append after a file is written.
    So you think it would make sense to put the appending after the creation of the file?

    Java Code:
        BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset());
        while ( (line = reader.readLine()) != null )
        {
            BufferedWriter writer =  Files.newBufferedWriter( path, Charset.defaultCharset(), StandardOpenOption.CREATE);
            try
            {
                x = new Formatter("Clients.txt");
                 
                //Creates a new file by using Formatter class
            }
            catch (FileNotFoundException ex)
            {
                Logger.getLogger(USF_JFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    ...
       }
    You are enclosing all of the output into a while(...) loop that reads the file it wants to create...?
    You need to put your new code AFTER the existing code in that method to reopen the file. I still see no reason to do so, but obviously you want to use the new nio package and not the formatter itself... for whatever reason.
    Maybe you want to do soemthing else and not what I am guessing, so maybe you need to explain it a little more in detail. Remember - the more detailed the explanation tha more fitting the answer.
    I like likes!

  17. #17
    johnsmithcon is offline Member
    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Exactly! Sierra, I just wanted to append the same file Clients.txt file and i really don't want to remove it. As you got an idea that I am logging in complaints status through forms. And on each and every button click I just wanted to maintain the Clients.txt file like a log file. Below is the effort that I made again but it is not happening and not giving any error. I mean the append is not happening.
    Java Code:
    private void btnSaveEmailActionPerformed(java.awt.event.ActionEvent evt) {                                             
            // TODO add your handling code here:
    Path source = Paths.get("Clients.txt");
            Path target = Paths.get("Clients.txt");
            ArrayList<String> lines= new ArrayList<>();
            Charset charset= Charset.defaultCharset();
            try(BufferedReader reader = Files.newBufferedReader(source,charset))
            {
            String line = null;
            while ( (line = reader.readLine()) != null ) {
                lines.add(line);
            try {
                x = new Formatter("Clients.txt");
                
                //Creates a new file by using Formatter class
                           
            } catch (FileNotFoundException ex) {
                Logger.getLogger(USF_JFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
           try(BufferedWriter writer =  Files.newBufferedWriter(target, charset))
            {
                Iterator<String> iterator= lines.iterator();
                while(iterator.hasNext())
                {
                    String s = iterator.next();
                    writer.append(s,0,s.length());
                    writer.newLine();
                
            Integer tokNo=Integer.parseInt(txtTokenNo.getText());  //calls getText method to get the text from the txtTokenNo textbox
                    
            x.format("%-25s %-20d \n" + System.getProperty("line.separator"),"Token No: ",tokNo);  //puts the formatted text in the created text file
            Integer EmpId=Integer.parseInt(txtEmployeeID.getText()); //calls getText method to get the text from the txtEmployeeID textbox
         x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Employee id: ",EmpId); //puts the formatted text in the created text file
            Date getTrnDate=jDateChooser.getDate(); //calls getText method to get the text from the txtTranDate textbox
            String trnDate=String.format("%1$td-%1$tm-%1$tY",getTrnDate);
                    
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Transaction date: ",trnDate); //puts the formatted text in the created text file
            String roomNo=txtRoomNo.getText(); //calls getText method to get the text from the txtRoomNo textbox
         x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Room No: ",roomNo); //puts the formatted text in the created text file
         Integer extNo=Integer.parseInt(txtExtNo.getText()); //calls getText method to get the text from the txExtNo textbox
         x.format("\n%-25s %-20d \n" + System.getProperty("line.separator"),"Extension is: ",extNo); //puts the formatted text in the created text file
         String empName=txtEmployeeName.getText(); //calls getText method to get the text from the txtEmployeeName textbox
     x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Employee Name: ",empName); //puts the formatted text in the created text file
    String loc=txtLocation.getText(); //calls getText method to get the text from the txtLocation textbox
    x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Location is: ",loc); //puts the formatted text in the created text file
    String dptName=txtDeptName.getText(); //calls getText method to get the text from the txtDeptName textbox
             x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Department Name: ",dptName); //puts the formatted text in the created text file
            String probType=(String)cbProbType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbProbType DropDownList
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Problem Type: ",probType); //puts the formatted text in the created text file
            String urgType=(String)cbUrgencyType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbUrgencyType DropDownList
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Urgency Type: ",urgType); //puts the formatted text in the created text file
            String appType=(String)cbAppType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbAppType DropDownList
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Application Type: ",appType); //puts the formatted text in the created text file
            String jStatusType=(String)cbJobStatusType.getSelectedItem(); //calls getSelectedItem method to get the list item from the cbJobStatusType DropDownList
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Job Status Type: ",jStatusType); //puts the formatted text in the created text file
            String desc=txtDescription.getText(); //calls getText method to get the text from the txtDescription textbox
            x.format("\n%-25s %-20s \n" + System.getProperty("line.separator"),"Description: ",desc); //puts the formatted text in the created text file
            x.close();    
                }
            }
            catch(IOException e){
            JOptionPane.showMessageDialog(null, e.getMessage(),"Error Message",JOptionPane.PLAIN_MESSAGE);
        }
          }   
        }                                            
        catch(IOException e){
            JOptionPane.showMessageDialog(null, e.getMessage(),"Error Message",JOptionPane.PLAIN_MESSAGE);
        }
        }
    Please assist if you are able to sort it out.
    Last edited by johnsmithcon; 01-10-2013 at 08:02 AM.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Append a single text file on button click through jTextField [a complete form]

    You really need to learn to write cleaner, nore readable code. For starters, that code is peppered with System.getProperty("line.separator"). You could call that method just once and assign the returned value to a String variable which you use in the rest of the code.

    Also, your indents should be consistent, not like this:
    Java Code:
        }
          }   
        }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Append a single text file on button click through jTextField [a complete form]

    Quote Originally Posted by johnsmithcon View Post
    Exactly! Sierra, I just wanted to append the same file Clients.txt file and i really don't want to remove it. As you got an idea that I am logging in complaints status through forms. And on each and every button click I just wanted to maintain the Clients.txt file like a log file. Below is the effort that I made again but it is not happening and not giving any error. I mean the append is not happening.
    Please assist if you are able to sort it out.
    That is quite easy now you explained in detail what you expect. You just move
    x = new Formatter("Clients.txt");
    and
    x.close();
    outside of your method. Create the Formatter object at the beginning of your program and close it when you terminate your application.
    In between you may include a x.flush() to update after you pressed the button. (put it where the close() is right now).

    You do not need Java7 or the new nio package for this...
    I like likes!

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2012, 03:56 AM
  2. Replies: 5
    Last Post: 02-16-2012, 05:16 PM
  3. Why can't I append to text file with JAVA ?
    By fatabass in forum New To Java
    Replies: 6
    Last Post: 01-14-2012, 12:29 AM
  4. Replies: 1
    Last Post: 07-03-2008, 01:05 PM
  5. Replies: 5
    Last Post: 01-17-2008, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •