Results 1 to 6 of 6
- 05-14-2012, 04:00 PM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
help with GUI .. read file and write file
i have been make this programe
to create new file .. and write to file using gui ..
but its not work well
Java Code:package mainme; import java.io.*; import java.util.Scanner; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class WriteToFile extends JFrame{ private JTextField text; private JButton ok; private JFrame frame=new JFrame(); public WriteToFile(){ super("Save "); setLayout(new FlowLayout()); text=new JTextField (10); ok=new JButton("ok"); add (text); add(ok); setSize(300,100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); TextHandler handler=new TextHandler(); text.addActionListener(handler); ButtonHandler handlers=new ButtonHandler(); ok.addActionListener(handlers); } private class TextHandler implements ActionListener{ public void actionPerformed(ActionEvent event ){ ok.getAction(); } } private class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent event ){ if(event.getSource()==ok) try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String file_name = in.readLine(); File file = new File(file_name); boolean exist = file.createNewFile(); if (!exist) { System.out.println("or append (enter o or a)"); file_name = in.readLine(); // JOptionPane.showMessageDialog(null, "File already exist"); //System.exit(0); } /* else if (exist) { System.out.println("or append (enter o or a)"); file_name = in.readLine();}*/ else { FileWriter fstream = new FileWriter(file_name); BufferedWriter out = new BufferedWriter(fstream); out.write(in.readLine()); out.close(); //System.out.println("File created successfully."); } InputStreamReader convert = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(convert); String instr; File outfile = new File(file_name); // boolean append = true; FileWriter fout = new FileWriter(file_name); PrintWriter fileout = new PrintWriter(fout); System.out.print("Enter a string: "); instr = stdin.readLine(); fileout.println(instr); fileout.flush(); fileout.close(); } catch(IOException d) { System.out.println("Error writing to file " + d); }}} public static void main(String[] args) { new WriteToFile(); }}
- 05-14-2012, 05:21 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: help with GUI .. read file and write file
Define "but its not work well "...does it compile? Are there Exceptions?
- 05-14-2012, 05:26 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: help with GUI .. read file and write file
And please be forthright when crossposting to other forums
help with GUI .. read file and write file
- 05-14-2012, 05:45 PM #4
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Re: help with GUI .. read file and write file
its work , but not in gui ..in system.in
i want when user click ok create file ..that was enterd name in textfile
- 05-14-2012, 05:55 PM #5
Re: help with GUI .. read file and write file
See posts on thread at: write data to existing text file | DaniWeb
If you don't understand my response, don't ignore it, ask a question.
- 05-14-2012, 07:35 PM #6
Similar Threads
-
Cannot read a file From APPDATA, but can write to it
By Vinny8978 in forum New To JavaReplies: 1Last Post: 04-27-2012, 07:34 PM -
Text read and write between *.txt file and *.xls file
By lemontree45 in forum New To JavaReplies: 6Last Post: 08-12-2011, 02:08 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
Read and Write file
By mrdestroy in forum New To JavaReplies: 13Last Post: 10-31-2008, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks