package com.jredu.week;
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;
public class FileOUtil {
public static BufferedInputStream bis = null;
public static BufferedOutputStream bos = null;
public static byte[] b = null;
public static File file = null;
public static int fileSize = 0;
public static void main(String[] args) {
// copy("F:/demoio/copy1.txt","F:/demoio/copy2.txt");
move("F:/demoio/copy3.txt","F:/copy1.txt");
// delete("F:/demoio/qq.png");
// creat("F:/ssss.txt");
// file=new File("F:/demoio");
// size(file);
// System.out.println("所有文件的大小为"+(fileSize/1024));
// log("F:/demoio/abc");
}
/**
* 文件拷贝
* @param file
*/
public static void copy(String oldFile,String newFile) {
try {
bis = new BufferedInputStream(new FileInputStream(oldFile));
b = new byte[bis.available()];
bis.read(b);
System.out.println("复制成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bos = new BufferedOutputStream(new FileOutputStream(newFile));
bos.write(b);
System.out.println("写入成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bos.flush();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 文件移动 剪切
*/
public static void move(String oldFile,String newFile) {
File file=new File(oldFile);
if(file.renameTo(new File(newFile))){
System.out.println("文件移动成功!");
}else{
System.out.println("移动失败!");
}
}
/**
* 删除文件
*/
public static void delete(String fileName) {
file = new File(fileName);
if(file.exists()){
System.out.println(file.delete() ? "删除成功!" : "删除失败");
}
}
/**
* 创建文件
*/
public static void creat(String fileName) {
file = new File(fileName);
if(file.exists()){
System.out.println("文件已存在");
}else{
try {
System.out.println(file.createNewFile() ? "创建成功" : "创建失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 文件或文件夹大小
*/
public static void size(File file) {
File[] files = file.listFiles();
for (File f : files) {
if (!f.isDirectory()) {
fileSize += f.length();//不是是目录,计算文件大小
} else {
size(f);//是目录递归调用自己
}
}
}
/**
* 文件目录信息显示
*/
public static void log(String f) {
file = new File(f);
System.out.println("文件目录名为" + file.getParent());
}
}