java拷貝文件代碼

 
package Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copyfile {

	public static String copyFile(String input, String to) throws IOException {
		File fromFile;
		File toFile;

		int lineIndex = input.lastIndexOf("/");
		int len = input.length();
		String output = to + "/" + input.substring(lineIndex + 1, len);

		fromFile = new File(input);
		System.out.println("file name is " + fromFile.getName());

		toFile = new File(output);

		if (toFile.exists()) {
			return "fileexist";
		}

		String dirStr = toFile.getAbsolutePath();
		File dirFile;
		dirStr = dirStr.substring(0, dirStr.lastIndexOf("\\"));
		dirFile = new File(dirStr);
		if (dirFile.exists() && dirFile.isDirectory())
			System.out.println("dir exist!");
		else {
			if (!dirFile.mkdirs())
				return "error";
		}

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(fromFile);
			fos = new FileOutputStream(toFile);
			int bytesRead;
			byte[] buf = new byte[4 * 1024]; // 4K buffer
			while ((bytesRead = fis.read(buf)) != -1) {
				fos.write(buf, 0, bytesRead);
			}
			fos.flush();
		} catch (IOException e) {
			throw new IOException(e.getMessage());
		} finally {
			if (fos != null)
				fos.close();
			if (fis != null)
				fis.close();
		}
		toFile.setLastModified(fromFile.lastModified());
		return "ok";
	}
	public static  void deletefile(String file)
	{
		File deletefile = new File(file);
		deletefile.delete();
		
	
	}
}

package Test;

import java.io.IOException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;

public class CopyFileSwt {

	private static Text text_1;
	private static Text text;

	public static void main(final String[] args) {
		final Display display = Display.getDefault();
		final Shell shell = new Shell();
		shell.setImage(SWTResourceManager.getImage(CopyFileSwt.class,
				"/org/eclipse/jface/dialogs/images/message_info.gif"));
		shell.setSize(500, 375);
		shell.setText("導入文件");
		shell.open();

		text = new Text(shell, SWT.BORDER);
		text.setEnabled(false);
		text.setBounds(155, 58, 192, 25);

		text_1 = new Text(shell, SWT.BORDER);
		text_1.setEnabled(false);
		text_1.setBounds(155, 176, 192, 25);

		final Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				// 選擇文件
				FileDialog fileSelect = new FileDialog(shell, SWT.SINGLE);
				fileSelect.setText("選擇文件");
				fileSelect.setFilterNames(new String[] { "*.*" });
				fileSelect.setFilterExtensions(new String[] { "*.*" });
				String url = "";
				url = fileSelect.open();

				String[] fileNames = fileSelect.getFileNames();
				if (url != null) {
					for (int i = 0; i < fileNames.length; i++) {
						if (i == 0) {
							fileNames[i] = fileSelect.getFilterPath() + "\\"
									+ fileNames[i];
							String sentfile = fileNames[i].replace('\\', '/');
							System.out.println("選擇的文件是:" + sentfile);
							text.setText(sentfile);
						} else {
							text.append(";" + fileNames[i]);// 多個文件之間用;隔開
						}
					}
				}
			}
		});
		button.setText("文件");
		button.setBounds(394, 56, 42, 27);

		final Button button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				final Shell shell = new Shell();
				DirectoryDialog directoryDialog = new DirectoryDialog(shell);
				directoryDialog.setText("目錄選擇");
				directoryDialog.setFilterPath("C:\\");
				directoryDialog.setMessage("選擇存放目錄");
				String directory = directoryDialog.open();
				if (directory != null) {
					String receivedir = directory.replace('\\', '/');
					System.out.println("選擇的目錄是:" + receivedir);
					text_1.setText(receivedir);
				}
			}
		});
		button_1.setText("選擇");
		button_1.setBounds(394, 174, 42, 27);

		final Button button_2 = new Button(shell, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				String filepath = text.getText();
				String dir = text_1.getText();
				if (filepath.equalsIgnoreCase("")) {
					MessageDialog md = new MessageDialog(null, "信息提示", null,
							"請選擇文件!", MessageDialog.INFORMATION,
							new String[] { "確定" }, 0);
					md.open();
				} else if (dir.equalsIgnoreCase("")) {
					MessageDialog md = new MessageDialog(null, "信息提示", null,
							"請選擇目錄!", MessageDialog.INFORMATION,
							new String[] { "確定" }, 0);
					md.open();
				} else {
					while (true) {
						try {
							String todir = dir.replace('\\', '/');
							String re = Copyfile.copyFile(filepath, todir);
							if (re.equalsIgnoreCase("ok")) {
								MessageDialog md = new MessageDialog(null,
										"信息提示", null, "導入成功!",
										MessageDialog.INFORMATION,
										new String[] { "確定" }, 0);
								md.open();
								shell.dispose();
								return;
							}
							if (re.equalsIgnoreCase("fileexist")) {
								MessageDialog md = new MessageDialog(null,
										"信息提示", null, "文件已存在!是否覆蓋原文件?",
										MessageDialog.INFORMATION,
										new String[] { "確定" }, 0);
								md.open();

								int lineIndex = filepath.lastIndexOf("/");
								int len = filepath.length();
								String defile = todir
										+ "/"
										+ filepath
												.substring(lineIndex + 1, len);
								Copyfile.deletefile(defile);
								continue;
							}
							if (re.equalsIgnoreCase("error")) {
								MessageDialog md = new MessageDialog(null,
										"信息提示", null, "創建目錄失敗!",
										MessageDialog.INFORMATION,
										new String[] { "確定" }, 0);
								md.open();
								shell.dispose();
								return;
							}
						} catch (IOException ioe) {
							MessageDialog md = new MessageDialog(null, "信息提示",
									null, "IO異常!", MessageDialog.INFORMATION,
									new String[] { "確定" }, 0);
							md.open();
							return;
						}
					}
				}
			}
		});
		button_2.setText("確定");
		button_2.setBounds(194, 261, 52, 27);

		final Button button_3 = new Button(shell, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		button_3.setText("取消");
		button_3.setBounds(276, 261, 52, 27);

		final Label label = new Label(shell, SWT.NONE);
		label.setText("請選擇文件");
		label.setBounds(58, 61, 74, 22);

		final Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("選擇存放目錄");
		label_1.setBounds(58, 179, 72, 22);
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章