1-5-6 Java工具類--IO
阿新 • • 發佈:2020-08-27
File
isDirectory()是否是目錄
isFile()是否是檔案
exists()檔案是否存在
mkdir()建立單級目錄
mkdirs()建立多級目錄
createNewFile()建立檔案
isAbsolute()是否絕對目錄
getPath()獲取相對路徑
getAbsolutePath()獲取絕對路徑
getName()獲取檔名
1 public class FileDemo { 2 3 public static void main(String[] args) { 4 File file = new File("E:\\workspace\\leetcode");5 System.out.println("是否是目錄:"+file.isDirectory()); 6 File file1 = new File(file, "src\\Test.java"); 7 System.out.println("是否是檔案:"+file1.isFile()); 8 9 //建立目錄 10 File file2 = new File("E:\\workspace\\leetcode\\ori\\file"); 11 if(!file2.exists()) { 12file2.mkdirs(); 13 } 14 if(!file1.exists()) { 15 try { 16 file1.createNewFile(); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 } 21 //是否是絕對路徑 22 System.out.println(file1.isAbsolute());23 //獲取相對路徑 24 System.out.println(file1.getPath()); 25 //獲取絕對路徑 26 System.out.println(file1.getAbsolutePath()); 27 //獲取檔名 28 System.out.println(file1.getName()); 29 } 30 31 }
位元組流
FileInputStream
直接讀取
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.IOException; 4 5 public class FileInputDemo { 6 7 public static void main(String[] args) { 8 FileInputStream fis = null; 9 int n = 0; 10 try { 11 fis = new FileInputStream("E:\\workspace\\leetcode\\src\\Main.java"); 12 while ((n = fis.read()) != -1) { 13 System.out.print((char)n); 14 } 15 } catch (FileNotFoundException e) { 16 e.printStackTrace(); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } finally { 20 if (fis != null) { 21 try { 22 fis.close(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 } 27 } 28 } 29 }
byte陣列緩衝
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.IOException; 4 5 public class FileInputDemo2 { 6 7 public static void main(String[] args) { 8 FileInputStream fis = null; 9 10 try { 11 fis = new FileInputStream("E:\\workspace\\leetcode\\src\\Main.java"); 12 byte[] bytes = new byte[100]; 13 fis.read(bytes, 0, 5); 14 System.out.println(new String(bytes)); 15 } catch (FileNotFoundException e) { 16 e.printStackTrace(); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } finally { 20 if (fis != null) { 21 try { 22 fis.close(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 } 27 } 28 } 29 }
FileOutputStream
直接寫入
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 6 public class FileOutputDemo { 7 8 public static void main(String[] args) { 9 FileInputStream fis = null; 10 FileOutputStream fos = null; 11 String path = "E:\\workspace\\leetcode\\src\\Test.txt"; 12 try { 13 fis = new FileInputStream(path); 14 fos = new FileOutputStream(path, true); 15 fos.write(50); 16 fos.write('a'); 17 System.out.println(fis.read()); 18 System.out.println((char)fis.read()); 19 } catch (FileNotFoundException e) { 20 e.printStackTrace(); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } finally { 24 if (fis != null) { 25 try { 26 fis.close(); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } 30 } 31 if (fos != null) { 32 try { 33 fos.close(); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 } 39 } 40 }
緩衝陣列(檔案拷貝)
1 import java.io.*; 2 3 public class FileOutputDemo2 { 4 5 public static void main(String[] args) { 6 FileInputStream fis=null; 7 FileOutputStream fos=null; 8 int n=0; 9 byte[] bytes = new byte[1024]; 10 11 try { 12 fis = new FileInputStream("E:\\workspace\\leetcode\\src\\Test.txt"); 13 fos = new FileOutputStream("E:\\workspace\\leetcode\\src\\TestCopy.txt"); 14 while ((n=fis.read(bytes))!=-1) { 15 fos.write(bytes,0,n); 16 } 17 } catch (FileNotFoundException e) { 18 e.printStackTrace(); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } finally { 22 if (fis != null) { 23 try { 24 fis.close(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 if (fos != null) { 30 try { 31 fos.close(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 } 37 } 38 }
BufferedInputStream & BufferedOutputStream
1 import java.io.*; 2 3 public class BufferedDemo { 4 5 public static void main(String[] args) { 6 FileInputStream fis=null; 7 BufferedInputStream bis=null; 8 FileOutputStream fos=null; 9 BufferedOutputStream bos=null; 10 String path = "E:\\workspace\\leetcode\\src\\Test.txt"; 11 try { 12 fis = new FileInputStream(path); 13 bis = new BufferedInputStream(fis); 14 fos = new FileOutputStream(path); 15 bos = new BufferedOutputStream(fos); 16 long startTime=System.currentTimeMillis(); 17 bos.write(50); 18 bos.write('a'); 19 bos.flush(); 20 System.out.println(bis.read()); 21 System.out.println((char)bis.read()); 22 long endTime=System.currentTimeMillis(); 23 System.out.println(endTime-startTime); 24 } catch (FileNotFoundException e) { 25 e.printStackTrace(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } finally { 29 if (fis != null) { 30 try { 31 fis.close(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 if (bis != null) { 37 try { 38 bis.close(); 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 } 43 if (fos != null) { 44 try { 45 fos.close(); 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } 49 } 50 if (bos != null) { 51 try { 52 bos.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 } 58 } 59 }
字元流
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.io.OutputStreamWriter; 9 10 public class ReaderDemo { 11 12 public static void main(String[] args) { 13 try { 14 FileInputStream fis=new FileInputStream("imooc.txt"); 15 InputStreamReader isr=new InputStreamReader(fis,"GBK"); 16 BufferedReader br=new BufferedReader(isr); 17 FileOutputStream fos=new FileOutputStream("imooc1.txt"); 18 OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK"); 19 BufferedWriter bw=new BufferedWriter(osw); 20 int n=0; 21 char[] cbuf=new char[10]; 22 // while((n=isr.read())!=-1){ 23 // System.out.print((char)n); 24 // } 25 // while((n=isr.read(cbuf))!=-1){ 26 // String s=new String(cbuf,0,n); 27 // System.out.print(s); 28 // } 29 while((n=br.read(cbuf))!=-1){ 30 //String s=new String(cbuf,0,n); 31 bw.write(cbuf, 0, n); 32 bw.flush(); 33 } 34 fis.close(); 35 fos.close(); 36 isr.close(); 37 osw.close(); 38 br.close(); 39 bw.close(); 40 } catch (FileNotFoundException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 catch (IOException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 49 50 } 51 52 }
物件的序列化和反序列化
1 import java.io.Serializable; 2 3 public class Goods implements Serializable{ 4 private String goodsId; 5 private String goodsName; 6 private double price; 7 public Goods(String goodsId,String goodsName,double price){ 8 this.goodsId=goodsId; 9 this.goodsName=goodsName; 10 this.price=price; 11 } 12 public String getGoodsId() { 13 return goodsId; 14 } 15 public void setGoodsId(String goodsId) { 16 this.goodsId = goodsId; 17 } 18 public String getGoodsName() { 19 return goodsName; 20 } 21 public void setGoodsName(String goodsName) { 22 this.goodsName = goodsName; 23 } 24 public double getPrice() { 25 return price; 26 } 27 public void setPrice(double price) { 28 this.price = price; 29 } 30 @Override 31 public String toString() { 32 return "商品資訊 [編號:" + goodsId + ", 名稱:" + goodsName 33 + ", 價格:" + price + "]"; 34 } 35 36 }
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 8 public class GoodsTest { 9 10 public static void main(String[] args) { 11 // 定義Goods類的物件 12 Goods goods1 = new Goods("gd001", "電腦", 3000); 13 try { 14 FileOutputStream fos = new FileOutputStream("good.txt"); 15 ObjectOutputStream oos = new ObjectOutputStream(fos); 16 FileInputStream fis = new FileInputStream("good.txt"); 17 ObjectInputStream ois = new ObjectInputStream(fis); 18 // 將Goods物件資訊寫入檔案 19 oos.writeObject(goods1); 20 oos.writeBoolean(true); 21 oos.flush(); 22 // 讀物件資訊 23 try { 24 Goods goods = (Goods) ois.readObject(); 25 System.out.println(goods); 26 } catch (ClassNotFoundException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 System.out.println(ois.readBoolean()); 31 32 fos.close(); 33 oos.close(); 34 fis.close(); 35 ois.close(); 36 } catch (FileNotFoundException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 44 } 45 46 }