導航:首頁 > 編程語言 > java復制代碼

java復制代碼

發布時間:2023-08-29 21:52:28

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();
}
}
}
}
}

閱讀全文

與java復制代碼相關的資料

熱點內容
演算法戰書籍 瀏覽:575
卸載網路伺服器是什麼意思 瀏覽:123
菜鳥app的收貨地址在哪裡 瀏覽:488
伺服器配什麼顯卡 瀏覽:369
動態壁紙不動了是怎麼回事安卓 瀏覽:412
申萬宏源app哪裡看總盈利 瀏覽:133
單片機測電感電容 瀏覽:165
android在子線程中更新ui 瀏覽:694
演算法分析師面試有什麼要求 瀏覽:994
容器演算法大全圖解 瀏覽:69
cad後置命令失效 瀏覽:692
殺手阻擊存檔文件夾是哪一個 瀏覽:212
禁書pdf 瀏覽:920
沒用app語音智能提醒怎麼設置 瀏覽:502
linuxwiki安裝 瀏覽:680
隔牆演算法 瀏覽:174
安卓手機為什麼app不通知 瀏覽:550
申請雲伺服器購買費用 瀏覽:115
雲伺服器鏡像下載到本地 瀏覽:4
電腦文件夾名有橫杠 瀏覽:154