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.

Results 1 to 14 of 14

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

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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,
    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();       
     
        }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

    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

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

    Are you getting any errors? Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Dear Norm. There is no error but the text file is not appending. I want to append the same file on each and every click button.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

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

    Can you make a small complete program that compiles, executes and shows the problem?

    the text file is not appending
    Can you explain what the program does when it executes?
    Does it create a new file?
    Does it replace an existing file?
    What data i written to the file?
    Is the correct data written to the file?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Dear Norm. Yes i did it but where I have to implement this code in my programme and the code is written below...

    package org.kodejava.example.io;



    import java.io.*;



    public class AppendFileExample

    {

    public static void main(String[] args)

    {

    File file = new File("user.txt");



    try

    {

    FileWriter writer = new FileWriter(file, true);

    writer.write("username=kodejava;password=secret"

    + System.getProperty("line.separator"));

    writer.flush();

    writer.close();

    } catch (IOException e)

    {

    e.printStackTrace();

    }

    }

    }

    --- Update ---

    Can you explain what the program does when it executes? (YES)
    Does it create a new file? (YES)
    Does it replace an existing file?(YES)
    What data i written to the file?(YES)
    Is the correct data written to the file?(YES)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

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

    Does the program do what you want now?
    If not, please explain.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    I would like to implement this code in my programme to append the file like in this code appending the file is work properly but how can i arrange this code in my programme code as above mention in first post.

    Thanks.




    package org.kodejava.example.io;
     
     
     
    import java.io.*;
     
     
     
    public class AppendFileExample
     
    {
     
    public static void main(String[] args) 
     
    {
     
    File file = new File("user.txt");
     
     
     
    try
     
    { 
     
    FileWriter writer = new FileWriter(file, true);
     
    writer.write("username=kodejava;password=secret"
     
    + System.getProperty("line.separator"));
     
    writer.flush();
     
    writer.close();
     
    } catch (IOException e) 
     
    {
     
    e.printStackTrace();
     
    }
     
    }
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

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

    how can i arrange this code in my programme code
    If you now know how to create a file or open a file and append records to the file, you need to copy the logic to your other program.
    Open the file in append mode before you want to write to it,
    write to it when you have the data to be written and
    close the file when you are done writing to the file.

    Can you find those places in the code to do each of those things?

    Note: The code should NOT have all statements starting in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Exacltly i would like to arrange this code in my programme but problem is this that where should i put this code to append the file.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

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

    where should i put this code to append the file.
    where in the code is the data ready to be written to the file?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Making a new class of the append file code and how can i input multile variables in string?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

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

    how can i input multile variables in string?
    Are you asking how to save multiple Strings in a single variable?
    You could read the Strings into an ArrayList.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Yes i am asking for multiple Strings in a single variable how can i do this?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,350
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

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

    Several ways:
    Use an ArrayList to hold the Strings
    Use an array to hold the Strings
    Define a custom class that can contain multiple Strings
    Put all the Strings in one String with a special character separating them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can't find an example on how to append text to jframe!
    By jetset22 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 10th, 2012, 03:37 AM
  2. add JTextField with a click of button
    By A4Andy in forum AWT / Java Swing
    Replies: 1
    Last Post: August 31st, 2011, 07:34 AM
  3. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  4. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 08:51 AM
  5. How can I append text in JTextArea from another class
    By chikaman in forum AWT / Java Swing
    Replies: 2
    Last Post: December 10th, 2009, 09:26 AM