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 3 of 3

Thread: help with GUI .. read file and write file

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with GUI .. read file and write file

    have been make this programe
    to create new file .. and write to file using gui ..
    but its not work well



    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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

    This thread has been cross posted here:

    http://www.java-forums.org/advanced-java/59700-help-gui-read-file-write-file.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Also at write data to existing text file | DaniWeb
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Read and write file in Java
    By waiheng1986 in forum File Input/Output Tutorials
    Replies: 1
    Last Post: March 18th, 2012, 11:54 AM
  2. [SOLVED] Can I have two scanners to the same file? (I want to read through one file 2 times)
    By beginnerprogrammer in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 20th, 2011, 11:23 AM
  3. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. how to read file name inside .gz file....
    By kathir0301 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 6th, 2010, 10:06 AM