使用IO流實現一個簡單的小Dome

(一) 在電腦D盤下建立一個文件爲HelloWorld.txt文件,判斷他是文件仍是目錄,在建立一個目
錄IOTest,以後將HelloWorld.txt移動到IOTest目錄下去;以後遍歷IOTest這個目錄下的文
java

 

爲了提升代碼的安全性,這裏全部的異常,我都處理了,而不是拋出,若是想方便的話,能夠都拋出。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;安全


public class Demo1 {spa

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
         File file = new File("C:\\Users\\junjiejie\\Desktop\\HelloWorld.txt");
         File file4 = new File("C:\\Users\\junjiejie\\Desktop\\Haha.txt");
         FileInputStream fileInputStream = null;
         FileOutputStream fileOutputStream = null;
         File file2 = null ;
         try {
   System.out.println(file.createNewFile());
   System.out.println(file4.createNewFile());
   if (file.isFile()) {
    System.out.println("是文件!");
   }else{
    System.out.println("不是文件!");
   }
   file2 = new File("C:\\Users\\junjiejie\\Desktop\\IOTest");
   System.out.println(file2.mkdir());
   File file3 = new File("C:\\Users\\junjiejie\\Desktop\\IOTest\\HelloWorld.txt");
   System.out.println(file3.createNewFile());
    fileInputStream = new FileInputStream(file);
    fileInputStream = new FileInputStream(file4);
    fileOutputStream = new FileOutputStream(file3);
   byte[] b = new byte[1024];
   while (fileInputStream.read(b) != -1) {
    fileOutputStream.write(b);
    
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   throw new RuntimeException(e);
  }finally{
   try {
    fileOutputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    throw new RuntimeException(e);
   }
   try {
    fileInputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    throw new RuntimeException(e);
   }
   file.delete();
   file4.delete();
   String[] arr= file2.list();
   for (String string : arr) {
    System.out.println(string);
   }
  }
 }string

}it

相關文章
相關標籤/搜索