欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)文件讀寫(xiě)與壓縮實(shí)例

 更新時(shí)間:2014年07月23日 16:16:04   投稿:shichen2014  
這篇文章主要介紹了java實(shí)現(xiàn)文件讀寫(xiě)與壓縮實(shí)例,有助于讀者加深對(duì)文件操作的理解,需要的朋友可以參考下

本文通過(guò)實(shí)例講述了Java對(duì)文件讀寫(xiě)與壓縮的實(shí)現(xiàn)方法,具體代碼如下:

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; 
import java.io.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.util.zip.ZipException; 
import java.util.zip.ZipFile;
public class TestFileIO {
  static String s = File.separator;
 
  private static void testInput() {
    // D盤(pán)下有個(gè)Welcome.java文件,現(xiàn)在按字節(jié)讀入:     
   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); // 這里是按字節(jié)輸出,中文字符無(wú)法正常輸出,因?yàn)橐粋€(gè)中文字符時(shí)兩個(gè)字節(jié)。       
       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();// 讀完之后要關(guān)閉文件      
      f12.close();// 讀完之后要關(guān)閉文件    
      } 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盤(pán)下有個(gè)Welcome.java文件,現(xiàn)在按字節(jié)讀入:    
   int a = 0;
    // 輸出流     
   File f21 = new File("D:" + s + "testFile" + s + "test1.txt");
   // 定義一個(gè)新的文件f21,然后判斷在這一目錄下是否存在,如果不存在,則創(chuàng)建之。    
   if (!f21.exists()) {
      f21.getParentFile().mkdirs();
      try {
        f21.createNewFile();
        // 將“Welcome.java”的內(nèi)容復(fù)制到f21         
        FileOutputStream fos = new FileOutputStream(f21);
        FileInputStream fis = new FileInputStream("D:" + s + "Welcome.java");// 讀入“Welcome.java”文件         
        while ((a = fis.read()) != -1)
          fos.write(a);// 將讀入的內(nèi)存寫(xiě)到fos中,現(xiàn)在得到的test1.txt就是復(fù)制Welcome.java的
        // writer類(lèi)        
        FileWriter f22 = new FileWriter("D:" + s + "testFile" + s + "test2.txt");
        for (int i = 0; i < 65535; i++)
          f22.write(i);//寫(xiě)入到test2.txt中。由這里也可以看出,上面35-38行判斷文件是否存在的語(yǔ)句也可以不要。         
        // 向文件中寫(xiě)入字符串         
        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盤(pán)下有個(gè)Welcome.java文件,現(xiàn)在按字節(jié)讀入:     
   int a = 0, counter = 0;
    // 緩沖字符,實(shí)現(xiàn)高效寫(xiě)入     
   // 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的內(nèi)容:");
      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 zipout;
    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();
  }
}

相關(guān)文章

  • maven pom中內(nèi)置變量及引用的實(shí)現(xiàn)

    maven pom中內(nèi)置變量及引用的實(shí)現(xiàn)

    maven其實(shí)有很多內(nèi)置變量供開(kāi)發(fā)著在開(kāi)發(fā)中使用,本文主要介紹了maven pom中內(nèi)置變量及引用的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • java如何將一個(gè)float型數(shù)的整數(shù)部分和小數(shù)分別輸出顯示

    java如何將一個(gè)float型數(shù)的整數(shù)部分和小數(shù)分別輸出顯示

    這篇文章主要介紹了java如何將一個(gè)float型數(shù)的整數(shù)部分和小數(shù)分別輸出顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Apache DolphinScheduler完全設(shè)置東八區(qū)時(shí)區(qū)

    Apache DolphinScheduler完全設(shè)置東八區(qū)時(shí)區(qū)

    這篇文章主要為大家介紹了Apache DolphinScheduler完全設(shè)置東八區(qū)配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • MyBatis緩存功能原理及實(shí)例解析

    MyBatis緩存功能原理及實(shí)例解析

    這篇文章主要介紹了MyBatis緩存功能原理及實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作

    Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作

    這篇文章主要介紹了Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中為什么不同的返回類(lèi)型不算方法重載

    Java中為什么不同的返回類(lèi)型不算方法重載

    這篇文章主要介紹了Java中為什么不同的返回類(lèi)型不算方法重載,方法重載是指在同一個(gè)類(lèi)中,定義了多個(gè)同名方法,但每個(gè)方法的參數(shù)類(lèi)型或者是參數(shù)個(gè)數(shù)不同就是方法重載,下文詳細(xì)介紹,需要的小伙伴可以參考一下
    2022-05-05
  • java中redis增刪查以及清理緩存的案例

    java中redis增刪查以及清理緩存的案例

    這篇文章主要介紹了java中redis增刪查以及清理緩存的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • centos7如何通過(guò)systemctl啟動(dòng)springboot服務(wù)代替java -jar方式啟動(dòng)

    centos7如何通過(guò)systemctl啟動(dòng)springboot服務(wù)代替java -jar方式啟動(dòng)

    這篇文章主要介紹了centos7如何通過(guò)systemctl啟動(dòng)springboot服務(wù)代替java -jar方式啟動(dòng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • Java二叉樹(shù)的遍歷思想及核心代碼實(shí)現(xiàn)

    Java二叉樹(shù)的遍歷思想及核心代碼實(shí)現(xiàn)

    今天小編就為大家分享一篇關(guān)于Java二叉樹(shù)的遍歷思想及核心代碼實(shí)現(xiàn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Java使用wait和notify實(shí)現(xiàn)線程之間的通信

    Java使用wait和notify實(shí)現(xiàn)線程之間的通信

    Java 線程通信是將多個(gè)獨(dú)立的線程個(gè)體進(jìn)行關(guān)聯(lián)處理,使得線程與線程之間能進(jìn)行相互通信,下面這篇文章主要給大家介紹了關(guān)于Java使用wait和notify實(shí)現(xiàn)線程之間通信的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04

最新評(píng)論