4 Replies - 1118 Views - Last Post: 30 August 2013 - 03:01 PM Rate Topic: -----

#1 AndreeU17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 163
  • Joined: 29-June 13

Not understanding JFileChooser

Posted 30 August 2013 - 07:57 AM

Hi im still creating a notepad/Editor. Due to college homework and busy life I have not had the chance to improve my skills of practice as of yet.

Here is the link of the example in having issues with:
http://www.dreaminco...ad-application/

sorry doing all this on my phone before my MacroEconomics class begins

I don't under stand this section
: This section will also cover the basics of file I/O, and the use of JFileChooser
view sourceprint?
01
public void actionPerformed (ActionEvent e) {
02
    // if the source of the event was our "close" option
03
    if (e.getSource() == this.close)
04
        this.dispose(); // dispose all resources and close the application
05
     
06
    // if the source was the "open" option
07
    else if (e.getSource() == this.openFile) {
08
        JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open)
09
        int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel)
10
        // NOTE: because we are OPENing a file, we call showOpenDialog~
11
        // if the user clicked OK, we have "APPROVE_OPTION"
12
        // so we want to open the file
13
        if (option == JFileChooser.APPROVE_OPTION) {
14
            this.textArea.setText(""); // clear the TextArea before applying the file contents
15
            try {
16
                // create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)
17
                Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
18
                while (scan.hasNext()) // while there's still something to read
19
                    this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea
20
            } catch (Exception ex) { // catch any exceptions, and...
21
                // ...write to the debug console
22
                System.out.println(ex.getMessage());
23
            }
24
        }
25
    }
26
     
27
    // and lastly, if the source of the event was the "save" option
28
    else if (e.getSource() == this.saveFile) {
29
        JFileChooser save = new JFileChooser(); // again, open a file chooser
30
        int option = save.showSaveDialog(this); // similar to the open file, only this time we call
31
        // showSaveDialog instead of showOpenDialog
32
        // if the user clicked OK (and not cancel)
33
        if (option == JFileChooser.APPROVE_OPTION) {
34
            try {
35
                // create a buffered writer to write to a file
36
                BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
37
                out.write(this.textArea.getText()); // write the contents of the TextArea to the file
38
                out.close(); // close the file stream
39
            } catch (Exception ex) { // again, catch any exceptions and...
40
                // ...write to the debug console
41
                System.out.println(ex.getMessage());
42
            }
43
        }



I don't under stand why use Bufferedwriter for save function rather than Open since you want to open a txt file thats editable

Sorry for the coffee hopefully the site has it there still :)/>

Once I get home I'll clean this mess Thank you

*Edited: change topic title

This post has been edited by pbl: 30 August 2013 - 03:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Not understanding JFileChooser

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12335
  • View blog
  • Posts: 45,442
  • Joined: 27-December 08

Re: Not understanding JFileChooser

Posted 30 August 2013 - 07:58 AM

A BufferedWriter is to write to the File, not to read from it. You could use a BufferedReader to do the reading.
Was This Post Helpful? 1
  • +
  • -

#3 laytonsdad   User is offline

  • Let it rip!
  • member icon

Reputation: 466
  • View blog
  • Posts: 1,992
  • Joined: 30-April 10

Re: Not understanding JFileChooser

Posted 30 August 2013 - 10:12 AM

Can you use a more relevant title for your posts so others can find them better, say through a google search. ;)
Was This Post Helpful? 1
  • +
  • -

#4 AndreeU17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 163
  • Joined: 29-June 13

Re: Not understanding JFileChooser

Posted 30 August 2013 - 01:04 PM

View Postmacosxnerd101, on 30 August 2013 - 08:58 AM, said:

A BufferedWriter is to write to the File, not to read from it. You could use a BufferedReader to do the reading.


Okay if BurreferedWriter allows me to write to a File, then what is FileWriter?
Do i need to implement a FileWriter then inside the () i put the bufferedwriter?
Was This Post Helpful? 0
  • +
  • -

#5 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Not understanding JFileChooser

Posted 30 August 2013 - 03:01 PM

BufferedWriter is a wrapper class around a Writer class. FileWriter indirectly extends Writer. FileWriter can write to files alone, but BufferedReader provides an alternate interface to the class, and can also manage certain functionality.

BufferedWriter offers some advantages over FileWriter including that it can cache data for more efficient usage, and providing additional convenience methods.

When you use BufferedWriter, it calls on it's now internal reference to the Writer Object, which then does as it needs to do. This is the same for BufferedWriter.

This post has been edited by Mylo: 30 August 2013 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1