IO流作業

1:遞歸刪除帶內容的目錄
package wbb;


import java.io.File;


public class test {
   public static void main(String[] args) {  
       File f = new File("c://1");      
       di(f);  
   }   
   private static void di(File f) {         
       File[] fileArray = f.listFiles();    
       if (fileArray != null) {         
           for (File file : fileArray) {  
               if (file.isDirectory()) {  
                   di(file);  
               } else {  
                   System.out.println(file.getName() + "---" + file.delete());  
               }  
           }  
 
          
System.out  
                   .println(f.getName() + "---" + f.delete());  
       }  
   }  

1.需求:請大家把E:\JavaSE目錄下所有的java結尾的文件的絕對路徑給輸出在控制檯


import java.io.File;  
  
public class test4 {  
    public static void main(String[] args) {          
        File file = new File("c:\\JavaSE");     
        File[] listFiles = file.listFiles();        
        for (File lf : listFiles) {           
            if (lf.isFile()) {  
                if (lf.toString().endsWith(".java")) {  
                    System.out.println(lf);  
                }  
            }  
  
        }  
  
    }  
}  


3:複製文本文件:由五種方式
package wbb;


import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.IOException;   
public class test1 {  
    public static void main(String[] args) throws IOException {  
        String s1 = "c:\\a.txt";  
        String s2 = "c:\\copy.txt";  
        // method1(s1, s2);  
        // method2(s1, s2);  
        // method3(s1, ds2);  
        // method4(s1, s2);  
        method5(s1, s2);  
    }  
  
    // 字符緩衝流一次讀寫一個字符串  
    private static void method5(String s1, String s2)  
            throws IOException {  
        BufferedReader br = new BufferedReader(new FileReader(s1));  
        BufferedWriter bw = new BufferedWriter(new FileWriter(s2));  
  
        String line = null;  
        while ((line = br.readLine()) != null) {  
            bw.write(line);  
            bw.newLine();  
            bw.flush();  
        }  
  
        bw.close();  
        br.close();  
    }  
  
    // 字符緩衝流一次讀寫一個字符數組  
    private static void method4(String s1, String s2)  
            throws IOException {  
        BufferedReader br = new BufferedReader(new FileReader(s1));  
        BufferedWriter bw = new BufferedWriter(new FileWriter(s2));  
  
        char[] chs = new char[1024];  
        int len = 0;  
        while ((len = br.read(chs)) != -1) {  
            bw.write(chs, 0, len);  
        }  
  
        bw.close();  
        br.close();  
    }  
  
    // 字符緩衝流一次讀寫一個字符  
    private static void method3(String s1, String s2)  
            throws IOException {  
        BufferedReader br = new BufferedReader(new FileReader(s1));  
        BufferedWriter bw = new BufferedWriter(new FileWriter(s2));  
  
        int ch = 0;  
        while ((ch = br.read()) != -1) {  
            bw.write(ch);  
        }  
  
        bw.close();  
        br.close();  
    }  
  
    // 基本字符流一次讀寫一個字符數組  
    private static void method2(String s1, String s2)  
            throws IOException {  
        FileReader fr = new FileReader(s1);  
        FileWriter fw = new FileWriter(s2);  
  
        char[] chs = new char[1024];  
        int len = 0;  
        while ((len = fr.read(chs)) != -1) {  
            fw.write(chs, 0, len);  
        }  
  
        fw.close();  
        fr.close();  
    }  
  
    // 基本字符流一次讀寫一個字符  
    private static void method1(String s1, String s2)  
            throws IOException {  
        FileReader fr = new FileReader(s1);  
        FileWriter fw = new FileWriter(s2);  
  
        int ch = 0;  
        while ((ch = fr.read()) != -1) {  
            fw.write(ch);  
        }  
  
        fw.close();  
        fr.close();  
    }  
}  
4:複製圖片:4種方式
package wbb;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class test2 {

public static void main(String[] args) throws IOException {

File start = new File("C://高圓圓.jpg");
File end = new File("C://copy.jpg");
//method1(start,end);
//method2(start,end);
//method3(start,end);
method4(start,end);

    }


private static void method1(File start, File end) throws IOException {
//基本字節流讀取一個字節
 FileInputStream fi = new FileInputStream(start);
 FileOutputStream fo = new FileOutputStream(end);
 int len = 0;
  while((len = fi.read()) != -1){
  fo.write(len);
         }
         fi.close();
       fo.close();        
   }




 private static void method2(File start, File end) throws IOException {
//基本字符流讀取一個字符數組
FileInputStream fi = new FileInputStream(start);
 FileOutputStream fo = new FileOutputStream(end);
byte[] by = new byte[1024];
 int len = 0;
while((len = fi.read(by)) != -1){
 fo.write(by,0,len);
}
fi.close();
fo.close();
}

 private static void method3(File start, File end) throws IOException {
// 緩衝字節流一次讀取一個字節
BufferedInputStream bfi = new BufferedInputStream(new FileInputStream(start));
BufferedOutputStream bfo  = new BufferedOutputStream(new FileOutputStream(end));
 int len = 0;
 while((len = bfi.read()) != -1){
 bfo.write(len);
}
bfi.close();
bfo.close();
 }

private static void method4(File start, File end) throws IOException {
  // 緩衝字節流一次讀取一個字節數組
 BufferedInputStream bfi = new BufferedInputStream (new FileInputStream(start));
BufferedOutputStream bfo = new BufferedOutputStream(new FileOutputStream(end));
byte[] by = new byte[1024];
int len = 0;
while((len = bfi.read(by)) != -1){
bfo.write(by,0,len);
     }
      bfi.close();
bfo.close();
       
 }
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章