Results 1 to 6 of 6
  1. #1
    kernal is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: help with GUI .. read file and write file

    Define "but its not work well "...does it compile? Are there Exceptions?

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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

  4. #4
    kernal is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default 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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: help with GUI .. read file and write file

    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: help with GUI .. read file and write file

    Not in any way an advanced question. Moved from Advanced Java

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

Similar Threads

  1. Cannot read a file From APPDATA, but can write to it
    By Vinny8978 in forum New To Java
    Replies: 1
    Last Post: 04-27-2012, 08:34 PM
  2. Text read and write between *.txt file and *.xls file
    By lemontree45 in forum New To Java
    Replies: 6
    Last Post: 08-12-2011, 03:08 AM
  3. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  4. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:11 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
  •