?
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import http://www.doczj.com/doc/718149fd910ef12d2af9e702.html.MalformedURLException;
import http://www.doczj.com/doc/718149fd910ef12d2af9e702.html.URL;
import http://www.doczj.com/doc/718149fd910ef12d2af9e702.html.URLConnection;
public class FileOperator {
//删除指定路径下所有txt文件 删除TXT文件
public void deleteAllTxt(String path){
File folder = new File(path);
String fileName=null;
if(folder.isDirectory()){
//列出所有文件保存发到File[]
File[] files = folder.listFiles();
//遍历得到的文件信息File[]
for (File file : files) {
//得到文件名然后判断是否带有.txt后缀 有则删除
fileName = file.getName();
if(http://www.doczj.com/doc/718149fd910ef12d2af9e702.htmlstIndexOf(".txt")!=-1){
file.delete();
}else{
System.out.println("没有txt文件或者已被删除");
}
}
}else{
System.out.println("不能对非文件夹进行操作!!!");
}
if(-1==http://www.doczj.com/doc/718149fd910ef12d2af9e702.htmlstIndexOf(".txt")){
System.out.println("没有txt文件或者已被删除");
}
}
//删除文件
public void deleteFile(String path){
File file=new File(path);
if(file.exists()){
if(file.isFile()){
file.delete();
}else{
System.out.println("输入有误,不是文件!");
}
}else{
System.out.println("文件不存在");
}
}
//删除文件夹
public void deleteFolder(String path){
File Folder = new File(path); //用File类表示出源文件夹
File[] Folders =Folder.listFiles(); //用File类型数组表示文件夹下的文件
if(Folder.isDirectory()){
for (File file : Folders) {
if(file.isDirectory()){
deleteFolder(path+File.separator+file.getName());
}else{
// deleteFile(file); //这是个经验 参数类型不匹配 利用类库方法 减少错误
file.delete(); //delete()删除文件或空文件夹 删除 自己以外的 空子文件夹
}
}
Folder.delete(); //删除自己 最后一个空文件夹
// System.out.println("删除成功");
}else
System.out.println("不是文件夹或者文件夹不存在!");
}
// 以字节为单位 根据源文件和目标文件的路径 复制文件
//fromPath:源文件路径 toPath:目标文件路径
public void copyFile(String fromPath,String toPath){
//用File表示出源文件和目标文件
File fromFile = new File(fromPath);
File toFile = new File(toPath);
InputStream is = null;
OutputStream os = null;
try {
//建立输入输出流 分别连接一个文件
is = new FileInputStream(fromFile);
os = new FileOutputStream(toFile);
int readByte = 0;
while( (readByte = is.read()) != -1){//循环读取