wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Really opening and saving REAL files!!!??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Really opening and saving REAL files!!!???" Watch "Really opening and saving REAL files!!!???" New topic
Author

Really opening and saving REAL files!!!???

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Ok I know I asked how to use JFileChooser to open and save files. And to tell you the truth it actually works.. I can actually view all the folders and drives on my computer. But I've noticed something.. YOU can't really actually open a file. For example I've written code (Which I will show below) where I can actually view every thing on my harddives. But when I go in to
the Bin directory inside the JDK1.3 directory and actually try to open a file I cannot actually open the file. I mean a java Application should be able to open another java file right.. just as Microsoft word can open .doc files. How do you actually open a .java file from the code I have below. And how do you save a file. All the examples I have seen on allow one to open files..
Anyway the code is below..if anyone can fix the code or even give me ideas on how toa ctually OPEN the files and Save files so I can actually open them later... Thanks in advance... any help would be appreciated.. so here is my code:

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.beans.*;
class FileOpenSave
{
public static void main(String args[])
{
MakeFrame makeFrame = new MakeFrame();
}
}
class MakeFrame
{
public MakeFrame()
{
JFrame frame = new JFrame("File Chooser");
JPanel pane = new JPanel();
JButton open = new JButton("Open Files");

//add actions to open button
open.setMnemonic(KeyEvent.VK_D);
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
OpenClass open =new OpenClass();

}
});


JButton save = new JButton("Save Files");
//Actions need to be added to save button

pane.add(open);
pane.add(save);
frame.getContentPane().add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Main Window");
frame.setSize(200 ,200);
frame.setVisible(true);

}
}

class OpenClass
{
JFileChooser chooser = new JFileChooser();

public OpenClass()
{
int state = chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
if(file != null &&
state == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, file.getPath());
}
else if(state == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(null, "Canceled");
}
else if(state == JFileChooser.ERROR_OPTION) {
JOptionPane.showMessageDialog(null, "Error!");
}
}
}


yours respectfully<br />Avin Sinanan
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Avin,
You are correct! The component is called JFileChooser not JFileReader so your comment is not all together shocking.
Don't get confused about a programming language and an application. Java is a programming language you can use it to build applications. Microsoft Word is an application that was written using a programming language. Its features (i.e., reading in/writing out various file types) were programmed by programmers using a programming language similar to Java.
In java, you can read and write various types of files (binary or ASCII) using many different class objects (Streams, Readers, Pipes, Sockets, HTTP). But in order to make it happen you will need to write some code to do it. There exists no class to do it for you because each users' conditions will be different (location, type, methods required, etc).
The simplest is to read/write ASCII files. Java supplies Reader and Writer classes for those. To use it just place the following section of code into your OpenClass in the APPROVE statement:

To write it back out you will need to store the in file contents into some sort of collection. Once you have it store you can look at FileWriter and BufferedWriter and write it back to a new file using JFileChooser to get a directory and name from the user!
Regards,
Manfred.
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Manfred,
Thanks for the advice.. I did as you told me.. I piy the piece of code in the openclass() method but when I complile I am getting 4 erroes.. its says it cannon resolve the variables FileReader and BufferedReader. WHy is this?
Here is the altered code... thanks for everyting.. but what am I doing wrong?
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.beans.*;
class FileOpenSave
{
public static void main(String args[])
{
MakeFrame makeFrame = new MakeFrame();
}
}
class MakeFrame
{
public MakeFrame()
{
JFrame frame = new JFrame("File Chooser");
JPanel pane = new JPanel();
JButton open = new JButton("Open Files");

//add actions to open button
open.setMnemonic(KeyEvent.VK_D);
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
OpenClass open =new OpenClass();

}
});


JButton save = new JButton("Save Files");
//Actions need to be added to save button

pane.add(open);
pane.add(save);
frame.getContentPane().add(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Main Window");
frame.setSize(200 ,200);
frame.setVisible(true);

}
}

class OpenClass
{
JFileChooser chooser = new JFileChooser();

public OpenClass()
{
int state = chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
if(file != null &&
state == JFileChooser.APPROVE_OPTION) {
//
// Open file reader so we can read in ASCII characters.
//
FileReader fr = new FileReader( file );
//
// Open buffer reader so we can read in lines.
//
BufferedReader in = new BufferedReader( fr );
System.out.println( "\nContents of: '" + file.getPath() + "':\n" );
String line;
while( (line = in.readLine()) != null )
{
System.out.println( line );
}
System.out.println( "*********************************" );
//
// Close both readers in reverse order.
//
in.close();
fr.close();
}
else if(state == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(null, "Canceled");
}
else if(state == JFileChooser.ERROR_OPTION) {
JOptionPane.showMessageDialog(null, "Error!");
}
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Avin,
Sorry I forgot about that change.
Replace:
import java.io.File;
with
import java.io.*;
This will allow the compiler to import the other io classes that it needs.
Regards,
Manfred.
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Manfred,
Sorry to bother you again..I made the change but its still not working.. its giving me 4 errors ..these errors are :
C:\jdk1.3\bin\FileOpenSave.java:64: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileReader fr = new FileReader( file );

C:\jdk1.3\bin\FileOpenSave.java:71: unreported exception java.io.IOException; must be caught or declared to be thrown
while( (line = in.readLine()) != null )
C:\jdk1.3\bin\FileOpenSave.java:79: unreported exception java.io.IOException; must be caught or declared to be thrown
in.close();
C:\jdk1.3\bin\FileOpenSave.java:80: unreported exception java.io.IOException; must be caught or declared to be thrown
fr.close();
I have no idea what these mean... ? could I bother you one more.. sorry for being a pest
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Avin,
Sorry again, been one of those days.
You need to enclose the entire code that I pasted into my previous response because all the java io classes throw an IOException. Example:

Regards,
Manfred.
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hey Manfred thanks a million... it worked finaly.. thanks!!!
posted another question ..Actually I posted a question asking anyone if they could actually make it work like a realk program like microsoftword. I inclued code to. If you have time could you give my question a look. Cause I noticed that when I try to open a file it opens it in MS-DOS. It was a big help though and thanks again!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Really opening and saving REAL files!!!???