❶ java图形用户界面的选择一个文件并复制(另存为)的代码,麻烦了。
importjava.awt.EventQueue;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjava.awt.Font;
importjavax.swing.JTextField;
importjavax.swing.JButton;
importjava.awt.Color;
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjavax.swing.JFileChooser;
publicclassCopyFile{
privateJFrameframe;
privateJTextFieldtextField;
privateJTextFieldtextField_1;
privateJFileChooserchooser;
privateStringreadPath;
privateStringwritePath;
/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
CopyFilewindow=newCopyFile();
window.frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*Createtheapplication.
*/
publicCopyFile(){
initialize();
}
/**
*.
*/
privatevoidinitialize(){
frame=newJFrame();
frame.setBounds(100,100,545,277);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabellabel=newJLabel("u6587u4EF6uFF1A");
label.setFont(newFont("黑体",Font.BOLD,18));
label.setBounds(26,68,57,25);
frame.getContentPane().add(label);
JLabellblNewLabel=newJLabel("u4FDDu5B58u76EEu5F55uFF1A");
lblNewLabel.setFont(newFont("黑体",Font.BOLD,18));
lblNewLabel.setBounds(10,119,95,25);
frame.getContentPane().add(lblNewLabel);
textField=newJTextField();
textField.setBounds(105,68,299,25);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1=newJTextField();
textField_1.setBounds(105,121,299,25);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
chooser=newJFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置选择模式,既可以选择文件又可以选择文件夹
JButtonbutton=newJButton("u6253u5F00");
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
intindex=chooser.showOpenDialog(null);
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
if(index==JFileChooser.APPROVE_OPTION){
//把获取到的文件的绝对路径显示在文本编辑框中
textField.setText(chooser.getSelectedFile()
.getAbsolutePath());
readPath=textField.getText();
}
}
});
button.setFont(newFont("黑体",Font.BOLD,18));
button.setBounds(432,67,87,26);
frame.getContentPane().add(button);
JButtonbutton_1=newJButton("u6D4Fu89C8");
button_1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
intindex=chooser.showSaveDialog(null);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
if(index==JFileChooser.APPROVE_OPTION){
//把获取到的文件的绝对路径显示在文本编辑框中
textField_1.setText(chooser.getSelectedFile()
.getAbsolutePath());
writePath=textField_1.getText()+"\";
}
}
});
button_1.setFont(newFont("黑体",Font.BOLD,18));
button_1.setBounds(432,118,87,26);
frame.getContentPane().add(button_1);
JButtonbutton_2=newJButton("u53E6u5B58u4E3A");
button_2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
readPath=textField.getText();
writePath=textField_1.getText()+"\";
if(File(readPath,writePath)==-1){//原文件不存在
JOptionPane.showMessageDialog(null,"源文件不存在","警告",JOptionPane.ERROR_MESSAGE);
}
}
});
button_2.setForeground(Color.RED);
button_2.setFont(newFont("黑体",Font.BOLD,18));
button_2.setBounds(213,180,93,34);
frame.getContentPane().add(button_2);
}
/*
**
*复制单个文件
*
*@paramoldPathString原文件路径如:c:/fqf.txt
*
*@paramnewPathString复制后路径如:f:/fgf.txt
*
*@returnint0表示成功,-1表示原文件不存在,-2表示未知错误。
*/
publicintFile(StringoldPath,StringnewPath){
try{
intbytesum=0;
intbyteread=0;
Fileoldfile=newFile(oldPath);
if(oldfile.exists()){//文件存在时
InputStreaminStream=newFileInputStream(oldPath);//读入原文件
System.out.println(newPath);
if(isExist(newPath)){
FileOutputStreamfs=newFileOutputStream(newPath);
byte[]buffer=newbyte[1444];
while((byteread=inStream.read(buffer))!=-1){
bytesum+=byteread;//字节数文件大小
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
fs.close();
return0;
}else{
return-2;
}
}
return-1;
}catch(Exceptione){
System.out.println("复制单个文件操作出错");
e.printStackTrace();
return-2;
}
}
publicstaticbooleanisExist(StringfilePath){
Stringpaths[]=filePath.split("\\");
Stringdir=paths[0];
for(inti=0;i<paths.length-2;i++){//注意此处循环的长度
try{
dir=dir+"/"+paths[i+1];
FiledirFile=newFile(dir);
if(!dirFile.exists()){
dirFile.mkdir();
System.out.println("创建目录为:"+dir);
}
}catch(Exceptionerr){
System.err.println("ELS-Chart:文件夹创建发生异常");
}
}
Filefp=newFile(filePath);
if(!fp.exists()){
returntrue;//文件不存在,执行下载功能
}else{
returnfalse;//文件存在不做处理
}
}
}
❷ 急求:JAVA编写复制文件夹的代码
一个简单的方式就是调用cmd命令,使用windows自带的功能来替你完成这个功能
我给你写个例子
import java.io.*;
public class test{
public static void main(String[] args){
BufferedReader in = null;
try{
// 这里你就当作操作对dos一样好了 不过cmd /c 一定不要动
Process pro = Runtime.getRuntime().exec("cmd /c d:\\ReadMe.txt e:\\");
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
String str;
while((str = in.readLine()) != null){
System.out.println(str);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(in != null){
try{
in.close();
}catch(IOException i){
i.printStackTrace();
}
}
}
}
}