http://mahua.jser.me/
别人的IO流学习小结 http://blog.csdn.net/zzp_403184692/article/details/8057693
IO流文件的复制删除
1.UI界面:
2.类结构
3.代码
package Copytool;类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileTool {
// 文件的复制
public static void copyFile(String srcPath, String destPath, String srcKey, String destKey) {
copyFile(new File(srcPath), new File(destPath), srcKey, destKey);
}
public static void copyFile(File srcFile, File destFile, String srcKey, String destKey) {
InputStream in = null;// 定义 声明 初始化
OutputStream out = null;
try {
// 异常一
/** 第一步:流 **/
in = new FileInputStream(srcFile);// 子类转成父类?没问题
out = new FileOutputStream(destFile);
/** 第二步:边写边读,,不能一次性读,内存不够 **/
byte[] buffer = new byte[512];
int length = in.read(buffer);// 有效字节个数
/** 第三步:关键字替换 验证关键字!! **/
if (srcFile == null || "".equals(srcFile)) {
// 不进行替换
while (length != -1) {
out.write(buffer, 0, length);
length = in.read(buffer);
}
} else {
// 进行替换
if (destKey == null) {
destKey = "";
}
// 进行替换
while (length != -1) {
String string = new String(buffer, 0, length);//
string = string.replace(srcKey, destKey);// 关键字替换
out.write(string.getBytes());// string -> byte[]
length = in.read(buffer);
}
}
// 异常二
// in.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
/** 第三步: 关闭 **/
try {
if (in != null) {
// int a = 1/0;
// 异常三
in.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
in = null;// 交给gc处理
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
out = null;
}
}
}
public static void copyFolder(String srcPath, String destPath, String srcKey, String destKey) {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
copyFolder(srcFile, destFile, srcKey, destKey);
}
public static void copyFolder(File srcFile, File destFile, String srcKey, String destKey) {
/** 第一步: 判断是否存在 **/
if (srcFile != null && srcFile.exists()) {
/** 第二步: 判断是否是文件夹 **/
if (srcFile.isDirectory()) {
// File流只能帮你创建文件,,不能创建一个文件夹
if (!destFile.exists()) {
destFile.mkdirs();
}
/** 第三步: 拿到文件夹里面的文件或文件夹的那个文件数组!!!,, **/
File[] listFiles = srcFile.listFiles();
/** 第四步:遍历递归 **/
for (File f : listFiles) // 增强for循环,1.5后
{
// 由于srcFile 路径变长了->destFile 也要变长
String name = f.getName(); // 变长的部分!!!
// 把目标文件也不变长
File file = new File(destFile, name);
copyFolder(f, file, srcKey, destKey);// 递归自己
}
} else {
// 必定是文件了!!
copyFile(srcFile, destFile, srcKey, destKey);
}
}
}
public static void deleteFolder(String path) {
deleteFolder(new File(path));
}
public static void deleteFolder(File file) {
/** 第一步: 判断是否存在 **/
if (file != null && file.exists()) {
/** 第二步: 判断是否是文件夹 **/
if (file.isDirectory()) {
File[] listFiles = file.listFiles();
if (listFiles.length != 0) {
// 删除孩子!!
for (int i = 0; i < listFiles.length; i++) {
File file2 = listFiles[i];
deleteFolder(file2);
}
}
// 自己 空的文件夹
file.delete();
} else {
file.delete();
}
}
}
public static void saveFile(String path, String content) {
//
saveFile(new File(path), content);
}
public static void saveFile(File file, String content) {
//
if (file != null) {
/** 第一步: 判断父类文件夹存不存在 **/
// c:\\aa\\bb\cc
File parentFile = file.getParentFile();// 拿到父类文件夹!!!
if (!parentFile.exists()) {
parentFile.mkdirs();
}
/** 第二步: 流写 out **/
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(content.getBytes());// string->byte
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
out = null;
}
}
}
}
}
MyFrame类
***package Copytool;
import javax.swing.JFrame;
public class MyFrame extends JFrame {
public MyFrame() {
// TODO Auto-generated constructor stub
this.setTitle("文件拷贝");
MyPanel jp = new MyPanel();
this.add(jp);
this.setSize(550, 400);
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}***
MyPanel类
package Copytool;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyPanel extends JPanel {
public String srcPath = "";
public String destPath = "";
public String destname;
public String savename;
public String srcKey = "", destKey = "";
public String path, content;
public MyPanel() {
// TODO Auto-generated constructor stub
init();
}
/**
* 面板内容
*/
private void init() {
this.setLayout(null);
// 复制选项
JLabel jl = new JLabel("复制选项:");
jl.setBounds(45, 20, 80, 20);// 205
this.add(jl);
JLabel jl1 = new JLabel("原来路径");
jl1.setBounds(45, 50, 80, 20);
this.add(jl1);
JTextField tf1 = new JTextField();
tf1.setBounds(100, 50, 250, 20);
this.add(tf1);
JButton jb1 = new JButton("选择复制路径");
jb1.setBounds(360, 50, 60, 20);
this.add(jb1);
JLabel jl2 = new JLabel("目标路径");
jl2.setBounds(45, 80, 80, 20);
this.add(jl2);
JTextField tf2 = new JTextField();
tf2.setBounds(100, 80, 250, 20);
this.add(tf2);
JButton jb2 = new JButton("选择目标路径");
jb2.setBounds(360, 80, 60, 20);
this.add(jb2);
// 关键字转换
JLabel jl0 = new JLabel("关键字转换:");
jl0.setBounds(45, 110, 80, 20);// 200
this.add(jl0);
JTextField tf3 = new JTextField();
tf3.setBounds(50, 140, 120, 20);
this.add(tf3);
JLabel jb3 = new JLabel("转成");
jb3.setBounds(217, 140, 60, 20);
this.add(jb3);
JTextField tf4 = new JTextField();
tf4.setBounds(300, 140, 120, 20);
this.add(tf4);
// String s;
// 删除与保存
JLabel jl01 = new JLabel("删除与保存:");
jl01.setBounds(45, 175, 80, 20);// 200
this.add(jl01);
// 删除
JLabel jl3 = new JLabel("删除路径");
jl3.setBounds(45, 210, 60, 20);
this.add(jl3);
JTextField tf5 = new JTextField();
tf5.setBounds(110, 210, 220, 20);
this.add(tf5);
JButton jb4 = new JButton("选择删除路径");
jb4.setBounds(360, 210, 60, 20);
this.add(jb4);
JLabel jl4 = new JLabel("保存路径");
jl4.setBounds(45, 250, 60, 20);
this.add(jl4);
JTextField tf6 = new JTextField();
tf6.setBounds(110, 250, 220, 20);
this.add(tf6);
JButton jb5 = new JButton("选择保存路径");
jb5.setBounds(360, 250, 60, 20);
this.add(jb5);
JButton jb6 = new JButton("确定");
jb6.setBounds(380, 310, 60, 20);
this.add(jb6);
JLabel jl5 = new JLabel("保存内容");
jl5.setBounds(45, 290, 60, 20);
this.add(jl5);
JTextField tf7 = new JTextField();
tf7.setBounds(110, 290, 220, 20);
this.add(tf7);
ActionListener action_listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// System.out.println("str1=" + str1);
String str = e.getActionCommand();
switch (str) {
case "选择复制路径":
JFileChooser fileChooser1 = new JFileChooser("C:\\Users\\黄家鹏\\Desktop\\新建文件夹");
fileChooser1.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser1.showDialog(null, "选择");
File selectFile1 = fileChooser1.getSelectedFile();
tf1.setText(selectFile1.getPath());
srcPath = tf1.getText();
destname = selectFile1.getName();
break;
case "选择目标路径":
JFileChooser fileChooser2 = new JFileChooser("C:\\Users\\黄家鹏\\Desktop\\新建文件夹");
fileChooser2.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser2.showDialog(null, "选择");
File selectFile2 = fileChooser2.getSelectedFile();
tf2.setText(selectFile2.getPath() + "\\" + destname);
destPath = tf2.getText();
System.out.println(destPath);
break;
case "选择删除路径":
JFileChooser fileChooser4 = new JFileChooser("C:\\Users\\黄家鹏\\Desktop\\新建文件夹");
fileChooser4.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser4.showDialog(null, "选择");
File selectFile4 = fileChooser4.getSelectedFile();
tf5.setText(selectFile4.getPath());
break;
case "选择保存路径":
JFileChooser fileChooser5 = new JFileChooser("C:\\Users\\黄家鹏\\Desktop\\新建文件夹");
fileChooser5.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser5.showDialog(null, "选择");
File selectFile5 = fileChooser5.getSelectedFile();
tf6.setText(selectFile5.getPath());
System.out.println(" " + tf6.getText());
System.out.println(" " + tf7.getText());
break;
case "确定":
System.out.println("destPath=" + destPath);
srcKey = tf3.getText();
destKey = tf4.getText();
FileTool.copyFolder(srcPath, destPath, srcKey, destKey);
System.out.println("deleteFolder=" + tf5.getText());
FileTool.deleteFolder(tf5.getText());
FileTool.saveFile(tf6.getText(), tf7.getText());
break;
}
}
};
jb1.addActionListener(action_listener);
jb2.addActionListener(action_listener);
jb4.addActionListener(action_listener);
jb5.addActionListener(action_listener);
jb6.addActionListener(action_listener);
}
}