📄 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 + -