Rabu, 15 April 2009

Save File

SEMOGA CODING INI BISA BERMANFAAT

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class Tugas3 extends JPanel
{
JTextArea jta = new JTextArea("TUGAS 3 MANDIRI \n\n by \n\n poros-star.blogspot.com");
JMenuItem jmi;
JScrollPane jsp = new JScrollPane(jta);

JMenuBar jmBar= new JMenuBar();
JButton btnOpen = new JButton("Open");
JButton btnSave = new JButton("Save");
JButton btnClear =new JButton("Clear");



String text = "";

public Tugas3()
{
class open implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(Tugas3.this, "btnOpen");
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String btnOpen = fc.getSelectedFile().getPath();
if(btnOpen == null)
{
return;
}
try
{
Reader in = new FileReader(btnOpen);
char[] buff = new char[5000];
int nch;
while((nch = in.read(buff, 0, buff.length)) != -1)
{
jta.setDocument(new PlainDocument());
jta.append(new String(buff, 0, nch));
}
}
catch (IOException io)
{
System.err.println("IOException: " + io.getMessage());
}
}
else
{
return;
}
}
}

class save implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(Tugas3.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String btnSave = fc.getSelectedFile().getPath();
if(btnSave == null)
{
return;
}
else
{
String docToSave = jta.getText();
if(docToSave != null)
{
FileOutputStream fstrm = null;
BufferedOutputStream ostrm = null;
try
{
fstrm = new FileOutputStream(btnSave);
ostrm = new BufferedOutputStream(fstrm);
byte[] bytes = null;
try
{
bytes = docToSave.getBytes();
}
catch(Exception e1)
{
e1.printStackTrace();
}
ostrm.write(bytes);
}
catch(IOException io)
{
System.err.println("IOException: " + io.getMessage());
}
finally
{
try
{
ostrm.flush();
fstrm.close();
ostrm.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}
}
}
}
else
{
return;
}
}
}

setLayout(new BorderLayout());
jmBar.add(btnOpen);
btnOpen.addActionListener(new open());

jmBar.add(btnSave);
btnSave.addActionListener(new save());

jmBar.add(btnClear);
btnClear.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.setText("");
}
});;

add(jmBar, BorderLayout.NORTH);
add(jsp, BorderLayout.CENTER);

}

public static void main(String args[])
{
JFrame f = new JFrame();
Tugas3 applet = new Tugas3();
f.setTitle("TUGAS 3");
f.setBackground(Color.lightGray);
f.getContentPane().add(applet, BorderLayout.CENTER);
f.addWindowListener(new appClose());
f.setSize(500, 300);
f.setVisible(true);
}

protected static final class appClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}

0 komentar:

Posting Komentar

Pasang Iklan

OPENSOURCE AREA © 2008. Design by :Yanku Templates Sponsored by: Tutorial87 Commentcute