Results 1 to 6 of 6
- 01-27-2010, 07:44 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 48
- Rep Power
- 0
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"); } }
- 01-28-2010, 12:14 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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(); }
- 01-28-2010, 12:54 AM #3
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)
- 08-03-2012, 09:07 AM #4
Member
- Join Date
- Aug 2012
- Location
- Galle,Sri Lanka
- Posts
- 2
- Rep Power
- 0
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
}
- 08-03-2012, 09:58 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 08-03-2012, 10:47 AM #6
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.
Thread Hijack
The member is banned for 7 days to allow time to read the warnings.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
How can I save the content of textfield in a text file?
By fred in forum Java AppletsReplies: 7Last Post: 08-17-2010, 06:00 PM -
save file based on file extension
By masa in forum AWT / SwingReplies: 4Last Post: 05-11-2010, 11:17 AM -
need to get the file name from folder
By naveen.dpt2007 in forum New To JavaReplies: 2Last Post: 09-04-2009, 06:57 PM -
Copy a file to a folder.
By leric in forum New To JavaReplies: 7Last Post: 07-29-2009, 05:11 AM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM


LinkBack URL
About LinkBacks


Bookmarks