第二十讲 Randomaccessfile使用、Properties工具类使用、文件压缩例子、装饰者模式、Path接口与Files工具类

来源:互联网 发布:数据库的日志不可用 编辑:程序博客网 时间:2024/05/17 06:37

导读

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】

原创粉丝点击