io包中有不少輸入輸出流的實現類,經常使用的就有:Buffered輸入輸出、ReaderWriter、File輸入輸出、InputStreamReader與OutputStreamWriter、對象序列反序列用的ObjectInputStream與ObjectOutputStream……。 html
不過用法也大同小異,遠沒有看上去那麼困難,不過是裝飾者模式的又一具體應用。只要明白節點流、處理流的概念,加上裝飾者模式,基本就沒什麼問題。 java
輸入流經常使用方法: 數組
void | close() 關閉此輸入流並釋放與該流關聯的全部系統資源。 |
abstract int | read() 從輸入流中讀取數據的下一個字節。 |
int | read(byte[] b) 從輸入流中讀取必定數量的字節,並將其存儲在緩衝區數組b中。 |
int | read(byte[] b, int off, int len) |
輸出流方法 ide
方法摘要 | |
---|---|
void | close() 關閉此輸出流並釋放與此流有關的全部系統資源。 |
void | flush() 刷新此輸出流並強制寫出全部緩衝的輸出字節。 |
void | write(byte[] b) 將b.length個字節從指定的 byte 數組寫入此輸出流。 |
void | write(byte[] b, int off, int len) 將指定 byte 數組中從偏移量off開始的len個字節寫入此輸出流。 |
abstract void | write(int b) 將指定的字節寫入此輸出流。 |
這是一個Demo: spa
/**
* 拷貝文件夾(目前僅能從文件夾拷貝到文件夾,不然出錯。)
* 刪除文件夾
*
*
* @author garview
*
* @Date 2013-11-3上午10:59:52
*/
public class IOUtil { .net
public static void main(String[] args) throws IOException {
// testCopyFile() ;
// testCopyDir();
testDeleteDir();
} xml
public static void testCopyDir() throws IOException{
String srcPath = "C:/KuGou/test/FeiqCfg.xml";
String destPath = "C:/KuGou/test/test";
copyDir(srcPath,destPath);
}
public static void testDeleteDir(){
File file = new File("C:/復件 復件 test");
deleteDir(file);
}
//拷貝文件夾及裏面全部文件
//1.肯定要拷貝的源文件夾路徑及目標文件夾路徑
//2.遍歷源文件夾——>1)文件——>拷貝2)文件夾——>遞歸調用本身
public static void copyDir(String srcPath, String destPath) throws IOException{
File src = new File(srcPath);
File dest = new File(destPath);
copyDir(src, dest);
}
public static void copyDir(File src, File dest) throws IOException {
//確保文件夾src,dest存在
if(!src.exists()){
throw new FileNotFoundException(src.getAbsolutePath()+"找不到文件路徑");
}
//確保文件夾src,dest不在同一路徑下,防止死遞歸。
if(dest.getAbsolutePath().contains(src.getAbsolutePath())){
System.out.println("文件夾src,dest不能在同一路徑下");
return;
} htm
//目前僅能從文件夾拷貝到文件夾,不然出錯。
/* if(src.isFile()) {
copyFile(src, dest);
return;
}*/
if(!dest.exists()) {
dest.mkdirs();
}
/**
* 遍歷src文件夾
* 1)文件——>拷貝
* 2)文件夾——>遞歸調用本身
*/
File[] files = src.listFiles();
for(File temp : files) {
File tempDest = new File(dest.getAbsolutePath(),temp.getName());
System.out.println(tempDest.getAbsolutePath());
if(temp.isFile()){
//新的源及新的目標文件
copyFile(temp, tempDest);
}else if(temp.isDirectory()){
//File tempSrc = new File(src.getAbsolutePath(),temp.getPath());
// File tempDest = new File(dest.getAbsolutePath(),temp.getName());
//System.out.println(tempDest.getAbsolutePath());
copyDir(temp,tempDest);
}
}
}
public static void copyFile(File src, File dest) throws FileNotFoundException,IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length = 0;
while(-1!=(length = fis.read(buffer))){
fos.write(buffer, 0, length);
}
fos.flush();
fis.close();
fos.close();
}
public static void testCopyFile() throws FileNotFoundException, IOException{
File src = new File("C:/HelloWorld2.java");
File dest = new File("C:/HelloWorld3.java");
copyFile(src, dest);
}
public static void deleteFile(File file){
boolean flag = file.delete();
String msg = file.getAbsolutePath();
if(flag){
msg += " delete success!";
}else {
msg += " delete failed!";
}
System.out.println(msg);
}
public static void deleteDir(File file){
if(file.isFile()) {
deleteFile(file);
return;
}
if(file.isDirectory()){
File[] files = file.listFiles();
/*File[] files = file.listFiles(new FileFilter() { 對象
@Override
public boolean accept(File pathname) {
boolean flag = pathname.getAbsolutePath().contains("HelloWorld");
return flag;
}
});*/
for(File temp : files) {
File tempDest = new File(file.getAbsolutePath(),temp.getName());
if(temp.isFile()) {
deleteFile(tempDest);
} else if(temp.isDirectory()) {
deleteDir(tempDest);
}
}
}
if(file.exists()) {
deleteFile(file);
}
} 遞歸
}