/**---------------------2007年07月18号文件拷贝程序-------------------------
-------本程序还要进行完善本程序只是成功的让程序来拷贝文件-----
---------下一步完成文件夹及文件的创建复制------------------------
------------------让程序更有人性话-哈哈--------------------------------------
*/
import java.io.* ;
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.* ;
public class FileCopy extends JFrame implements ActionListener{
JTextField t_from = new JTextField() ;
JTextField t_to = new JTextField() ;
FileCopy(){
//设置界面属性
this.setTitle("文件拷贝") ;
this.setSize(300 , 180) ;
this.setResizable(false) ;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
//设置窗体居中
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() ;
this.setLocation((width-300)/2 , (height-180)/2) ;
//新建一大堆组件
JLabel l_from = new JLabel("源文件") ;
JLabel l_to = new JLabel("目标文件") ;
JButton b_from = new JButton("源文件" ) ;
JButton b_to = new JButton("目标文件") ;
JButton b_copy = new JButton("复制");
JButton b_reset = new JButton("重写") ;
//注册监听器
b_from.addActionListener(this) ;
b_to.addActionListener(this) ;
b_copy.addActionListener(this) ;
b_reset.addActionListener(this) ;
//设置输入面板
JPanel p_input = new JPanel() ;
p_input.setLayout(new GridLayout(2 , 2)) ;
p_input.add(l_from) ;
p_input.add(t_from) ;
p_input.add(b_from) ;
p_input.add(l_to) ;
p_input.add(t_to) ;
p_input.add(b_to) ;
//设置按钮面板
JPanel p_button = new JPanel() ;
p_button.setLayout(new FlowLayout()) ;
p_button.add(b_copy) ;
p_button.add(b_reset) ;
//布置整体面板
this.setLayout(new GridLayout(2 , 1)) ;
this.add(p_input ) ;
this.add(p_button) ;
}
public static void main(String []args){
FileCopy fc = new FileCopy() ;
fc.setVisible(true) ;
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("源文件")) {
try {
JFileChooser jfc = new JFileChooser() ;
jfc.showOpenDialog(this) ;
File f = jfc.getSelectedFile() ;
if(f == null){
JOptionPane.showMessageDialog(this , "选择文件") ;
}
t_from.setText(f.toString()) ;
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this , "选择文件") ;
}
}
if(e.getActionCommand().equals("目标文件")){
try {
JFileChooser jfc = new JFileChooser() ;
jfc.showSaveDialog(this) ;
File f = jfc.getSelectedFile() ;
if(f == null){
JOptionPane.showMessageDialog(this , "选择目录") ;
}
t_to.setText(f.toString()) ;
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this , "选择目录") ;
}
}
if(e.getActionCommand().equals("复制")){
try {
File f = new File(t_from.getText()) ;
File outf = new File(t_to.getText()) ;
byte tmp[] = new byte[8192] ;
FileInputStream fis = new FileInputStream(f) ;
FileOutputStream fos = new FileOutputStream(outf) ;
int size = fis.available() ;
if(size < 8192){
for (int i = 0; i<size; i++){
fos.write(fis.read()) ;
}
}else{
size = fis.available()/8192 ;
for (int i = 0; i<size; i++){
fis.read(tmp) ;
fos.write(tmp) ;
}
size = fis.available() ;
for (int i = 0; i<size; i++){
fos.write(fis.read()) ;
}
}
}
catch (Exception ex) {
}
}
if(e.getActionCommand().equals("重写")){
t_from.setText("") ;
t_to.setText("") ;
}
}
}
-------本程序还要进行完善本程序只是成功的让程序来拷贝文件-----
---------下一步完成文件夹及文件的创建复制------------------------
------------------让程序更有人性话-哈哈--------------------------------------
*/
import java.io.* ;
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.* ;
public class FileCopy extends JFrame implements ActionListener{
JTextField t_from = new JTextField() ;
JTextField t_to = new JTextField() ;
FileCopy(){
//设置界面属性
this.setTitle("文件拷贝") ;
this.setSize(300 , 180) ;
this.setResizable(false) ;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
//设置窗体居中
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() ;
this.setLocation((width-300)/2 , (height-180)/2) ;
//新建一大堆组件
JLabel l_from = new JLabel("源文件") ;
JLabel l_to = new JLabel("目标文件") ;
JButton b_from = new JButton("源文件" ) ;
JButton b_to = new JButton("目标文件") ;
JButton b_copy = new JButton("复制");
JButton b_reset = new JButton("重写") ;
//注册监听器
b_from.addActionListener(this) ;
b_to.addActionListener(this) ;
b_copy.addActionListener(this) ;
b_reset.addActionListener(this) ;
//设置输入面板
JPanel p_input = new JPanel() ;
p_input.setLayout(new GridLayout(2 , 2)) ;
p_input.add(l_from) ;
p_input.add(t_from) ;
p_input.add(b_from) ;
p_input.add(l_to) ;
p_input.add(t_to) ;
p_input.add(b_to) ;
//设置按钮面板
JPanel p_button = new JPanel() ;
p_button.setLayout(new FlowLayout()) ;
p_button.add(b_copy) ;
p_button.add(b_reset) ;
//布置整体面板
this.setLayout(new GridLayout(2 , 1)) ;
this.add(p_input ) ;
this.add(p_button) ;
}
public static void main(String []args){
FileCopy fc = new FileCopy() ;
fc.setVisible(true) ;
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("源文件")) {
try {
JFileChooser jfc = new JFileChooser() ;
jfc.showOpenDialog(this) ;
File f = jfc.getSelectedFile() ;
if(f == null){
JOptionPane.showMessageDialog(this , "选择文件") ;
}
t_from.setText(f.toString()) ;
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this , "选择文件") ;
}
}
if(e.getActionCommand().equals("目标文件")){
try {
JFileChooser jfc = new JFileChooser() ;
jfc.showSaveDialog(this) ;
File f = jfc.getSelectedFile() ;
if(f == null){
JOptionPane.showMessageDialog(this , "选择目录") ;
}
t_to.setText(f.toString()) ;
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this , "选择目录") ;
}
}
if(e.getActionCommand().equals("复制")){
try {
File f = new File(t_from.getText()) ;
File outf = new File(t_to.getText()) ;
byte tmp[] = new byte[8192] ;
FileInputStream fis = new FileInputStream(f) ;
FileOutputStream fos = new FileOutputStream(outf) ;
int size = fis.available() ;
if(size < 8192){
for (int i = 0; i<size; i++){
fos.write(fis.read()) ;
}
}else{
size = fis.available()/8192 ;
for (int i = 0; i<size; i++){
fis.read(tmp) ;
fos.write(tmp) ;
}
size = fis.available() ;
for (int i = 0; i<size; i++){
fos.write(fis.read()) ;
}
}
}
catch (Exception ex) {
}
}
if(e.getActionCommand().equals("重写")){
t_from.setText("") ;
t_to.setText("") ;
}
}
}