• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help on file dialog class

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
i am learning beginners java. i am facing problem with file Dialog class. Following program show the dialog box but i can't open any file or save any file from it. in order to save and open a file which methods can i use in this program? I tried FileDialog.LOAD and FileDialog.Save.This doesn't show any error but does not work.please help me
Regards
Smriti Singla
.................................................................
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class sampleframe extends Frame
{
sampleframe(String title)
{
super(title);
MyWindowAdapter ad=new MyWindowAdapter(this);
addWindowListener(ad);
}
}
class MyWindowAdapter extends WindowAdapter
{
sampleframe ss;
public MyWindowAdapter(sampleframe ss)
{
this.ss=ss;
}
public void windowClosing(WindowEvent we)
{
ss.setVisible(false);
}
}
public class filedialog
{
public static void main(String args[])
{
Frame f=new sampleframe("file dialog box");
f.setVisible(true);
f.setSize(100,100);
FileDialog fd=new FileDialog(f,"filedialog",FileDialog.LOAD);
fd.setVisible(true);
}
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
FileDialog is only for choosing the file. If you go through its methods you will find that it includes getFile() and getDirectory() that returns a String object representting the chosen file and the directory. You have to use these strings to create filestreams for reading from the file or writing to the file. The FileDialog.LOAD and FileDialog.SAVE constants are only for indicating that the purpose of the dialog is for opening a file or saving a file. ATB.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you need to use the FileDialog to get the name of the file and open it with something else, like a FileInputStream.

You need to check for Exceptions to make that work. You might not want to use a DataInputStream, it depends what sort of data is in the file. Look in java.io at the various input streams to find what you need.
If you want to do other stuff with the file, like renaming it or modifying attributes, you can use the java.io.File class.
Hope that helps, Grant.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic