Results 1 to 6 of 6
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Default how to save output (from TextField) to a particular Folder as txt file

    hi,
    i m facing a small prob in GUI, need to save my output to a particular folder.

    below this code showing the square of any integer in TextField named: "resultField". i've a button "save" that need to save the result from that TextField, suppose in my "c:\Repository " folder as a txt file.

    In addition, the name of the txt file can be same as given input integer. Example: 77, 99, .. whatever the input integer.

    how can i do this ?? pls help...

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class SaveData extends Frame implements ActionListener {
    
    private TextField inputField, resultField;
    private Button saveButton, squareButton, exitButton;
    
      SaveData(String s) {
        super(s);
    	 
        setLayout(new GridLayout(3, 3, 5, 3));
    	 
        add(new Label("Type in an integer: "));
        inputField= new TextField();  add(inputField);
    	 
    	 squareButton=new Button("Square"); add(squareButton); squareButton.addActionListener(this);
    	 
    	 resultField=new TextField(); add(resultField);
    	 
    	 saveButton=new Button("SAVE"); add(saveButton); saveButton.addActionListener(this);
    
        exitButton=new Button("Exit"); add(exitButton); exitButton.addActionListener(this);
       
    	
    	 setSize(250,130);
    	 setLocation(600,200);
        setVisible(true); }
    	 
    	 
      public void actionPerformed(ActionEvent e) {
      if (e.getSource()==squareButton) {
      int x=Integer.parseInt(inputField.getText());
      resultField.setText(Integer.toString(x*x));  }
      
      else if (e.getSource()==exitButton) {  this.setVisible(false);  this.dispose(); }
      
      }
      
      
      public static void main(String args[]){
      new SaveData("test");
      }
    }

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    236
    Rep Power
    11

    Default

    You can write information to a file using FileWriter like so:
    Java Code:
    public void saveData(String name, String data)
    {
    FileWriter writer = new FileWriter(new File("C:\\Repository\\" + name + ".txt");
    writer.write(data);
    writer.flush();
    writer.close();
    }

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    11

    Default

    If you want more info on File Handling, there is an insightful tutorial on it on the java.sun.com website: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    Vbabey is offline Member
    Join Date
    Aug 2012
    Location
    Galle,Sri Lanka
    Posts
    2
    Rep Power
    0

    Post Re: how to save output (from TextField) to a particular Folder as txt file

    // this programme helps you to enter any data to a text file in a predetermined place
    //by means of a text box and a action button....
    /*
    * Input.java
    *
    * Created on August 3, 2012, 11:59 AM
    */

    package me.org.source;

    import java.io.BufferedWriter;
    import java.io.FileWriter;

    /**
    *
    * @author Vbabey
    */
    public class Input extends javax.swing.JFrame {
    public String data;
    /** Creates new form Input */
    public Input() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    dataText = new javax.swing.JTextField();
    okButton = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("Favourite Quote");

    okButton.setMnemonic('O');
    okButton.setText("OK");
    okButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    okButtonActionPerformed(evt);
    }
    });

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel1.setForeground(new java.awt.Color(0, 102, 102));
    jLabel1.setText("Favourite Quote");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(dataText, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
    .addGap(18, 18, 18)
    .addComponent(okButton)
    .addGap(17, 17, 17))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(43, 43, 43)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(okButton)
    .addComponent(dataText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel1))
    .addContainerGap(46, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    data =dataText.getText();
    try{
    // Create file
    FileWriter fstream = new FileWriter("C:\\Users\\Vbabey\\Desktop\\java\\out. txt");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(data);
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    dataText.setText("");
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Input().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JTextField dataText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton okButton;
    // End of variables declaration

    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: how to save output (from TextField) to a particular Folder as txt file

    Quote Originally Posted by Vbabey View Post
    // this programme helps you to enter any data to a text file in a predetermined place
    This is a two and a half year old thread, which already has a valid answer.

    Locking the thread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to save output (from TextField) to a particular Folder as txt file

    Member Vbabey has already been asked to not post to old dead threads or hijack another user's thread.
    http://www.java-forums.org/new-java/...ad-hijack.html

    The member is banned for 7 days to allow time to read the warnings.

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

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2010, 07:00 PM
  2. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 12:17 PM
  3. need to get the file name from folder
    By naveen.dpt2007 in forum New To Java
    Replies: 2
    Last Post: 09-04-2009, 07:57 PM
  4. Copy a file to a folder.
    By leric in forum New To Java
    Replies: 7
    Last Post: 07-29-2009, 06:11 AM
  5. Replies: 7
    Last Post: 11-19-2008, 03:09 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
  •