第二十講 Randomaccessfile使用、Properties工具類使用、文件壓縮例子、裝飾者模式、Path接口與Files工具類

導讀

RandomAccessFile類:隨機文件訪問類,可以讀取文件任意位置的開始到結束位置結束之間的所有內容。

Properties類:用以配置項目或模塊的配置信息,該類可以讀取以“.properties”文件。

文件壓縮類:ZipInputStream和ZipOutputStream類的使用。

裝飾者模式:對象的組合設計方式,增強業務處理能力。

Path接口與Files類:前者是代表一個路徑的接口,後者是代表對文件或者文件夾的處理工具類。(十分好用)


①、RandomAccessFile類的代碼如下:

import java.io.IOException;
import java.io.RandomAccessFile;

public class TestRandomAccessFile {
public static void main(String[] args) throws IOException {
RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");
for (int i = 0; i < 10; i++) {
//寫入基本類型double數據
rf.writeDouble(i * 1.414);
}
rf.close();
rf = new RandomAccessFile("rtest.dat", "rw");
//直接將文件指針移到第5個double數據後面
rf.seek(5 * 8);
//覆蓋第6個double數據
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("rtest.dat", "r");
for (int i = 0; i < 10; i++) {
System.out.println("Value " + i + ": " + rf.readDouble());
}
rf.close();
}


②、Properties工具類使用的代碼如下:

package com.main.util;  
  
import java.io.IOException;  
import java.util.Properties;  
/** 
 * 讀取properties文件的工具類 
 */  
public class Tools {  
    private static Properties p = new Properties();  
    /** 
     * 讀取properties配置文件信息 
     */  
    static{  
        try {  
            p.load(Tools.class.getClassLoader().getResourceAsStream("data.properties"));  
        } catch (IOException e) {  
            e.printStackTrace();   
        }  
    }  
    /** 
     * 根據key得到value的值 
     */  
    public static String getValue(String key)  
    {  
        return p.getProperty(key);  
    }  
}  


③、文件壓縮的代碼如下:

public class TestFile
{
    public static void main ( String [ ] args ) throws IOException
    {
        // new a file input stream
        FileInputStream fis = new FileInputStream (
        "/home/liangruihua/ziptest/1.txt" ) ;
        BufferedInputStream bis = new BufferedInputStream ( fis ) ;
        // new a zipPutputStream
        // /home/liangruihua/ziptest/1.zip -- the out put file path and
        // name
        ZipOutputStream zos = new ZipOutputStream (
                new FileOutputStream (
                "/home/liangruihua/ziptest/1.zip" ) ) ;
        BufferedOutputStream bos = new BufferedOutputStream ( zos ) ;

        // set the file name in the .zip file
        zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ;
        // set the declear
        zos.setComment ( "by liangruihua test!" ) ;

        byte [ ] b = new byte [ 100 ] ;
        while ( true )
        {
            int len = bis.read ( b ) ;
            if ( len == - 1 )
                break ;
            bos.write ( b , 0 , len ) ;
        }
        fis.close ( ) ;
        zos.close ( ) ;
    }
}

文件解壓的代碼如下:

public class TestZipInputStream
{
    public static void main ( String [ ] args ) throws ZipException ,
            IOException
    {
        // get a zip file instance
        File file = new File ( "/home/liangruihua/ziptest/test.zip" )

        // get a ZipFile instance
        ZipFile zipFile = new ZipFile ( file ) ;

        // create a ZipInputStream instance
        ZipInputStream zis = new ZipInputStream ( new FileInputStream(file ) ) ;

        // create a ZipEntry instance , lay the every file from
        // decompress file temporarily
        ZipEntry entry = null ;

        // a circle to get every file
        while ( ( entry = zis.getNextEntry ( ) ) != null )
        {
            System.out.println ( "decompress file :"+ entry.getName ( ) ) ;

            // define the path to set the file
            File outFile = new File ( "/home/liangruihua/ziptest/"+ entry.getName ( ) ) ;

            // if the file's parent directory wasn't exits ,than
            // create the directory
            if ( ! outFile.getParentFile ( ).exists ( ) )
            {
                outFile.getParentFile ( ).mkdir ( ) ;
            }

            // if the file not exits ,than create the file
            if ( ! outFile.exists ( ) )
            {
                outFile.createNewFile ( ) ;
            }

            // create an input stream
            BufferedInputStream bis = new BufferedInputStream (zipFile.getInputStream ( entry ) ) ;


            // create an output stream
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream ( outFile ) ) ;
            byte [ ] b = new byte [ 100 ] ;
            while ( true )
            {
                int len = bis.read ( b ) ;
                if ( len == - 1 )
                    break ;
                bos.write ( b , 0 , len ) ;
            }
            // close stream
            bis.close ( ) ;
            bos.close ( ) ;
        }
        zis.close ( ) ;
    }
}
 

④、裝飾者模式的代碼如下:

//定義被裝飾者  
public interface Human {  
    public void wearClothes();  
    public void walkToWhere();  
}  
  
//定義裝飾者  
public abstract class Decorator implements Human {  
    private Human human;    
    public Decorator(Human human) {  
        this.human = human;  
    }  
  
    public void wearClothes() {  
        human.wearClothes();  
    }  
  
    public void walkToWhere() {  
        human.walkToWhere();  
    }  
}  
  
//下面定義三種裝飾,這是第一個,第二個第三個功能依次細化,即裝飾者的功能越來越多  
public class Decorator_zero extends Decorator {  
    public Decorator_zero(Human human) {  
        super(human);  
    }  
  
    public void goHome() {  
        System.out.println("進房子。。");  
    }  
  
    public void findMap() {  
        System.out.println("書房找找Map。。");  
    }  
  
    @Override  
    public void wearClothes() {  
        // TODO Auto-generated method stub  
        super.wearClothes();  
        goHome();  
    }  
  
    @Override  
    public void walkToWhere() {  
        // TODO Auto-generated method stub  
        super.walkToWhere();  
        findMap();  
    }  
}  
  
public class Decorator_first extends Decorator {    
    public Decorator_first(Human human) {  
        super(human);  
    }  
  
public void goClothespress() {  
        System.out.println("去衣櫃找找看。。");  
    }  
  
public void findPlaceOnMap() {  
        System.out.println("在Map上找找。。");  
  }  
  
@Override  
public void wearClothes() {  
        // TODO Auto-generated method stub  
        super.wearClothes();  
        goClothespress();  
    }  
  
@Override  
public void walkToWhere() {  
        // TODO Auto-generated method stub  
        super.walkToWhere();  
        findPlaceOnMap();  
    }  
}  
  
public class Decorator_two extends Decorator {    
    public Decorator_two(Human human) {  
        super(human);  
    }  
  
public void findClothes() {  
        System.out.println("找到一件D&G。。");  
    }  
  
public void findTheTarget() {  
        System.out.println("在Map上找到神祕花園和城堡。。");  
  }  
  
    @Override  
    public void wearClothes() {  
        // TODO Auto-generated method stub  
        super.wearClothes();  
        findClothes();  
    }  
  
    @Override  
    public void walkToWhere() {  
        // TODO Auto-generated method stub  
        super.walkToWhere();  
        findTheTarget();  
    }  
}  
  
//定義被裝飾者,被裝飾者初始狀態有些自己的裝飾  
public class Person implements Human {  
  
    @Override  
    public void wearClothes() {  
        // TODO Auto-generated method stub  
        System.out.println("穿什麼呢。。");  
    }  
  
    @Override  
    public void walkToWhere() {  
        // TODO Auto-generated method stub  
        System.out.println("去哪裏呢。。");  
    }  
}  
//測試類,看一下你就會發現,跟java的I/O操作有多麼相似  
public class Test {  
    public static void main(String[] args) {  
        Human person = new Person();  
        Decorator decorator = new Decorator_two(new Decorator_first(  
                new Decorator_zero(person)));  
        decorator.wearClothes();  
        decorator.walkToWhere();  
    }  


關於Path接口與Files類的概述與使用,本質內容是——

【網址:https://www.cnblogs.com/ixenos/p/5851976.html】

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