?? bytedemo.java
字號:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ByteDemo extends JFrame implements ActionListener {
JPanel pnlMain;
JLabel lblSource,lblObject;
JButton btnOpen,btnCopy,btnbitCopy;
JTextField txtSource,txtObject;
JFileChooser fc;
FileInputStream fisSource;
FileOutputStream fosObject;
FileReader fr;
FileWriter fw;
public ByteDemo()
{
pnlMain=new JPanel(new FlowLayout());
setContentPane(pnlMain);
lblSource=new JLabel("源 文 件:");
lblObject =new JLabel("目標文件");
txtSource=new JTextField(10);
txtObject=new JTextField(10);
fc=new JFileChooser();
btnOpen=new JButton("打開");
btnCopy=new JButton("字節復制");
btnbitCopy=new JButton("字符復制");
btnCopy.setEnabled(false);
btnbitCopy.addActionListener(this);
btnOpen.addActionListener(this);
btnCopy.addActionListener(this);
pnlMain.add(lblSource);
pnlMain.add(txtSource);
pnlMain.add(lblObject);
pnlMain.add(txtObject);
pnlMain.add(btnOpen);
pnlMain.add(btnCopy);
pnlMain.add(btnbitCopy);
setTitle("文件復制");
setSize(200,150);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if( ae.getSource().equals(btnOpen))
{
int intRetVal=fc.showOpenDialog(this);
if(intRetVal==JFileChooser.APPROVE_OPTION)
{
txtSource.setText(fc.getSelectedFile().toString());
String strFileName=(fc.getSelectedFile().getName());
String strPath=(fc.getSelectedFile().getPath());
btnCopy.setEnabled(true);
int i=strFileName.indexOf(".");
String str=strFileName.substring(0,i);
str=str+"bake"+strFileName.substring(i);
txtObject.setText(str);
}
}else if(ae.getSource()==btnCopy)
{
copyFileByte();
}else if(ae.getSource()==btnbitCopy)
{
copyFilebit();
}
}
public void copyFilebit()
{
if(!txtSource.getText().equals(null)&&!txtObject.getText().equals(null))
{
try
{
fw=new FileWriter(txtObject.getText());
fr=new FileReader(txtSource.getText());
int temp;
while(fr.read()!=-1)
{
fw.write(fr.read());
}
JOptionPane.showMessageDialog(null, "字符方式復制成功");
fr.close();
fw.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, " 復制失敗");
}
}
}
public void copyFileByte()
{
if(!txtSource.getText().equals(null)&&!txtObject.getText().equals(null))
{
try
{
fisSource=new FileInputStream(txtSource.getText());
fosObject=new FileOutputStream(txtObject.getText());
int intTemp;
while((intTemp=fisSource.read())!=-1)
{
fosObject.write(intTemp);
}
fisSource.close();
fosObject.close();
JOptionPane.showMessageDialog(null, "字節方式復制成功");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "沒有找到文件");
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ByteDemo();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -