how to use io operations with open/save file dialog box in file menu for simple notepad

Discussion in 'Java' started by boris, Jul 22, 2007.

  1. boris

    boris Guest

    i am creating two simple notepad.One with awt and another with
    swings.but i am not able to set io operations for the open/save file
    dialog box .I am learning java .please help.
     
    boris, Jul 22, 2007
    #1
    1. Advertising

  2. boris

    Guest

    Okay.
    One thing: the Open/Save File Dialog Box will just return the path
    where you will save or open your file.
    So, you have to do the two things (get the file path and save/open it)
    in two times.
    I thing code explain better than my (really) bad English:

    [START OF CODE]
    import java.io.File; //We need that to use the File object
    import javax.swing.JFileChooser; //It show open and save dialogs

    public class IO {

    public void saveFile(){
    String path = ""; //Variable who will contain the path where
    we will write our file
    JFileChooser fileChooser = new JFileChooser(); //Object that
    we will use to select our file

    int returnValue; //Variable who whill contain the value of the
    ShowSaveDialog function (if user click "save" or "abord")
    returnValue = fileChooser.showSaveDialog(null); //Show a "save
    window"

    if (returnValue == JFileChooser.APPROVE_OPTION){ //If user
    click "save"
    File file; //We create a new File object
    file = fileChooser.getSelectedFile(); //We open the file
    //Normally, here you write into your file
    System.out.println("We open the file: " + file.getName());
    }
    else{ //The user click "abord"
    System.out.println("The user don't want to save the
    file!");
    }
    }

    public void openFile(){
    String path = ""; //Variable who will contain the path where
    is the file that we will read
    JFileChooser fileChooser = new JFileChooser(); //Object that
    we will use to select our file

    int returnValue; //Variable who whill contain the value of the
    ShowOpenDialog function (if user click "open" or "abord")
    returnValue = fileChooser.showOpenDialog(null); //Show a "open
    window"

    if (returnValue == JFileChooser.APPROVE_OPTION){ //If user
    click "open"
    File file; //We create a new File object
    file = fileChooser.getSelectedFile(); //We open the file
    //Normally, here you read from your file
    System.out.println("We open the file: " + file.getName());
    }
    else{ //The user click "abord"
    System.out.println("The user don't want to read the
    file!");
    }
    }

    }
    [END OF CODE]

    Don't forget to read the doc'!
    How to use File Choosers: http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
    (note that my example is here)
    How to read/write from files: http://java.sun.com/docs/books/tutorial/essential/io/charstreams.html

    Good luck.
    Regards.

    On 22 juil, 17:17, boris <> wrote:
    > i am creating two simple notepad.One with awt and another with
    > swings.but i am not able to set io operations for the open/save file
    > dialog box .I am learning java .please help.
     
    , Jul 22, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry
    Replies:
    2
    Views:
    51,195
    Jon A. Cruz
    Mar 4, 2004
  2. BusyBoy
    Replies:
    2
    Views:
    622
    =?Utf-8?B?TGVvbiBNYXluZQ==?=
    Oct 31, 2006
  3. Jon   Paal

    bypass open/save/cancel dialog box

    Jon Paal, Jan 22, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    1,580
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jan 22, 2007
  4. hell2heaven
    Replies:
    0
    Views:
    430
    hell2heaven
    Oct 26, 2008
  5. eddie wang
    Replies:
    0
    Views:
    425
    eddie wang
    Oct 3, 2003
Loading...

Share This Page