本文透過實例講述了Java對檔案讀寫與壓縮的實作方法,具體程式碼如下:
package com.toone.iform.action.common;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; .FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Random; import java.util.zip.ZipEntry; import java.Random; import java.util.zip.ZipEntry; import java. ZipException; import java.util.zip.ZipFile;public class TestFileIO { static String s = File.separator; private static void testInput() { // D碟下有個Welcome.java文件,現在以位元組讀入: int a = 0; // int counter=0; FileInputStream f11; // 輸入流try { f11 = new FileInputStream("D:" + s + "Welcome.java"); while ((a = f11.read()) != -1) System.out.print((char) a); // 這裡是按位元組輸出,中文字元無法正常輸出,因為一個中文字元時兩個字節。 System.out.println("/n/n--------------------------------------- -----------/n"); FileReader f12 = new FileReader("D:" + s + "Welcome.java"); while ((a = f12.read()) != - 1) System.out.print((char) a);//這裡是按字元輸出,中文字元都可以正常輸出System.out.println("/n/n--------------------------- -----------------------/n"); f11.close();// 讀完後要關閉檔案f12.close();//讀完後要關閉檔案} catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void testOutput() { // D碟下有Welcome.java文件,現在按位元組讀入: int a = 0; // 輸出流File f21 = new File("D:" + s + "testFile" + s + "test1.txt"); //定義一個新的檔案f21,然後判斷在這一目錄下是否存在,如果不存在,則創建之。 if (!f21.exists()) { f21.getParentFile().mkdirs(); try { f21.createNewFile(); // 複製「Welcome.java」的內容到f21 FileOutputStream fos = new FileOutputStream(f21); FileInputStream fis = new FileInputStream("D:" + s + "Welcome.java");// 讀入「Welcome.java」檔案while ((a = fis.read()) != -1) fos.write(a);// 將讀入的記憶體寫入fos中,現在得到的test1.txt就是複製Welcome.java的// writer類別FileWriter f22 = new FileWriter("D:" + s + "testFile" + s + "test2.txt"); for (int i = 0; i < 65535; i++) f22.write(i);//寫入到test2.txt中。由這裡也可以看出,上面35-38行判斷文件是否存在的語句也可以不要。 // 寫入字串FileWriter f23 = new FileWriter("D:" + s + "testFile" + s + "test3.txt"); f23.write("Hello, world!"); fos.close (); fis.close(); f22.close(); f23.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private static void testBufferring() { // D碟下有Welcome.java文件,現在按位元組讀入: int a = 0, counter = 0; / / 緩衝字符,實現高效寫入// BufferedWriter f31=new BufferedWriter(newFileWriter("D"+s+"testFile"+s+"test4.txt")); BufferedWriter f31; try { f31 = new BufferedWriter(new FileWriter("D:" + s + "testFile" + s + "test4 .txt")); for (int i = 1; i <= 100; i++) { f31.write(String.valueOf(new Random().nextInt(100)) + " "); if (i % 10 == 0) f31.newLine(); } f31.flush();// 刷新緩衝f31 .close(); BufferedReader f32 = new BufferedReader(new FileReader("D:" + s + "testFile" + s + "test4.txt")); String s32; System.out.println("輸出檔案f32的內容:"); while ((s32 = f32.readLine()) != null) System.out.println( s32); f32.close(); System.out.println("/n-------------------------------- ------------------/n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void testZip() { try { File f1 = new File("D:/test.zip"); File f2 = new File("D:/testFile/ testzip"); ZipFile zf = new ZipFile(f1); testZipToUnzip(zf, f2); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 將壓縮包zipfile解壓縮到file中public static void testZipToUnzip(ZipFile zipfile, File file) { ZipEntry zentry = null; File zout ; InputStream zis = null; FileOutputStream fos = null; Enumeration e = zipfile.entries();// zipfile的目錄while (e.hasMoreElements()) { zentry = (ZipEntry) e.nextElement(); System.out.println(zentry.getName());// zipfile下方有哪些文件?可是為什麼不依序輸出呢? ? // 將解壓縮後的檔案放到file資料夾下: zipout = new File(file + s + zentry.getName()); if (!zentry.isDirectory()) { try { zis = zipfile.getInputStream(zentry) ; if (!zipout.exists()) zipout.getParentFile().mkdirs(); fos = new FileOutputStream(zipout); byte[] b = new byte[1024]; int length; while ((length = zis.read(b)) > 0) { fos.write(b, 0, length); } fos.close (); zis.close(); } catch (ZipException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } public static void main(String[] args) throws ZipException { // TODO Auto-generated method stub testInput(); testOutput(); testBufferring(); testZip(); }}