複製文件夾
public class CopyFolder {
public void copyFolder(String srcFolder, String destFolder){//拷貝文件夾及子文件夾到目標路徑
File file=new File(srcFolder); //創建目標路徑的文件
File file2=new File(destFolder);
try {
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream(file2);
BufferedOutputStream bos=new BufferedOutputStream(fos);
// if(file.isDirectory()){
// File[]
// }
byte[] buf=new byte[1024*5];
int len=0;
while((len=bis.read())!=-1){
bos.write(buf,0,len);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
複製文件
// String src="d:/123.txt";
// String dest="c:/abc.vbh";
public class CopyTool {
public void copy(String src,String dest) {
try {
long start=System.currentTimeMillis();
FileInputStream fls=new FileInputStream(new File(src)); //定義輸入流
FileOutputStream fos=new FileOutputStream(new File(dest)); //定義輸出流
BufferedInputStream bis=new BufferedInputStream(fls);
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte[] buf=new byte[1024];
int len=0;
while((len=bis.read(buf))!=-1){
bos.write(buf,0,len);
bos.flush();
}
// int readData=-1;
// while((readData=fls.read())!=-1){
// fos.write(readData);
// }
// fos.flush(); //流動
// //誰後打開,誰先關閉
// fos.close();
// fls.close();
long end=System.currentTimeMillis();
System.out.println("所用的時間爲:"+(end-start));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
刪除文件中含.txt格式的文件
public class DeleteFileText {
public static void main(String[] args) {
File file=new File("d:/abc");
if(file.isDirectory()){ //如果是file的目錄
File[] sunFiles=file.listFiles();
String str[]=file.list();
for (int i = 0; i < sunFiles.length; i++) {
System.out.println("sunFiles["+i+"]"+sunFiles[i].getName());
if(sunFiles[i].getName().equals(str[i])){
System.out.println("nihso");
}
System.out.println("str["+i+"]"+str[i]);
}
// for(File subFile:sunFiles){
// if(subFile.getName().endsWith(".txt")){
//// if(subFile.getName().contains(".txt")){
// subFile.delete();
// }
// }
}
}
}
複製操作
public class CopyDir {
public void copyDir(String srcPath, String desPath) {
try {
FileInputStream fis = new FileInputStream(new File(srcPath));
FileOutputStream fos = new FileOutputStream(new File(desPath));
int readdate = -1;
while ((readdate = fis.read()) != -1) {
fos.write(readdate);
}
fos.flush();
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//
// public static void main(String[] args) {
// CopyDir copydir = new CopyDir();
// String desPath1 = "c:/";
// String srcPath = "d:/test";
public void CopyFolder(String srcFolder, String desFolder) {
// TODO 拷貝文件夾
File filesrcFolder = new File(srcFolder);
CopyDir copydir = new CopyDir();
if (filesrcFolder.isDirectory()) {
File filedesFolder = new File(desFolder);
if (!filedesFolder.exists()) { // 判斷文件夾是不是存在
filedesFolder.mkdir(); // 創建文件夾
}
File[] subfiles = filesrcFolder.listFiles();
for (int i = 0; i < subfiles.length; i++) {
copydir.copyDir(subfiles[i].toString(), desFolder + "/"
+ subfiles[i].getName());
}
} else {
copydir.copyDir(srcFolder, desFolder);
}
}
public void DeleteFile(String filename) {
// 刪除指定名稱的文件
File file = new File(filename);
if (file.exists()) {
if (file.isFile()) {
file.delete();
} else {
System.out.println("這不是文件");
}
} else {
System.out.println("此文件不存在");
}
}
public void DeleteFolder(String foldername) {
// 刪除指定名稱的文件和文件夾
File file = new File(foldername);
if (file.exists()) {
if (file.isDirectory()) {
File[] subfiles = file.listFiles();
for (File file2 : subfiles) {
DeleteFile(file2.toString());
file2.delete();
}
} else {
file.delete();
}
} else {
file.delete();
System.out.println("此文件不存在");
}
}
public void CutFolder(String srcname, String destname) {
// 剪切文件夾
CopyFolder(srcname, destname);
DeleteFolder(srcname);
System.out.println("剪切已完成");
}
public void Download(String url, String destpath) {
// 下載給定鏈接的文件
try {
URL urlname = new URL(url);
String t = urlname.toString();
FileOutputStream out = new FileOutputStream(t);
FileInputStream in = new FileInputStream(new File(destpath));
int readdata = 0;
while ((readdata = in.read()) != -1) {
out.write(readdata);
}
out.flush();
out.close();
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}