I want to have FileDialog which will be displayed when user clicks on a save button. when the file dialog is displayed the file should be written to seletecd folder. Can any one show me how to do that? I will appreciate it!
Pradeepta Bhattacharya
Greenhorn
Joined: Sep 07, 2001
Posts: 15
posted
0
Originally posted by john klucas: I want to have FileDialog which will be displayed when user clicks on a save button. when the file dialog is displayed the file should be written to seletecd folder. Can any one show me how to do that? I will appreciate it!
I think This Will Solve Your Problem ..Please imtimate me if you need something else Thanking You Pradeepta The Code : import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public class SaveFile extends Frame implements ActionListener { Button b; FileDialog fd; String dir,filename,extention; byte[] barr=null; public SaveFile() { super("Save File Demo"); setLayout(new FlowLayout()); b=new Button("Open"); add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent ae) { try { if(ae.getActionCommand().equals("Open")) { fd=new FileDialog(this,"Open Dialog",FileDialog.LOAD); fd.show(); dir=fd.getDirectory(); filename=fd.getFile(); System.out.println(dir+" "+filename); if(dir.length()>0 && filename.length()>0){ b.setLabel("Save"); FileInputStream fis=new FileInputStream(dir+filename); barr = new byte[fis.available()]; fis.read(barr,0,fis.available()); StringTokenizer stoken=new StringTokenizer(filename,"."); while(stoken.hasMoreTokens()) extention=stoken.nextToken(); fis.close(); } } else if(ae.getActionCommand().equals("Save")) { if(dir.length()>0 && filename.length()>0){ fd=new FileDialog(this,"Save Dialog",FileDialog.SAVE); fd.show(); dir=fd.getDirectory(); filename=fd.getFile(); FileOutputStream fos=new FileOutputStream(dir+filename+"."+extention); fos.write(barr); fos.close(); b.setLabel("Open"); } } }catch(Exception e){System.out.println(e);} } public static void main(String[] args) { SaveFile sf=new SaveFile(); sf.setSize(400,400); sf.setVisible(true); } }