IO技术(七)Properties、文件切割合并的初步改进

来源:互联网 发布:网络空间安全杂志社 编辑:程序博客网 时间:2024/05/18 18:21

详细见API文档 ,代码:https://pan.baidu.com/s/1hrH7iTI 密码: iegq

package com.demo;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import java.util.Properties;/* * 内容:Properties,文件切割、合并的改进 * 代码运行注意事项: * 运行环境:MAC OS ,Windows下需要修改路径; * 合并前必须切割,而且要保证test.avi存在 * 例子重点:[3-1][4-1] * 不足:[4-1]中配置文件过于简单,而且没有使用BufferedOutputStream,如果需要应用于实际开发,可以改进 *  * *//* *  3.Properties:[3-1] * 引入:需要把一些具备对应关系的数据存放到文件中,如果使用[1-1]中的方式需要[2-1]的方式获取, * 获取方式很不方便; * Map具备对应关系,如果能够把Map中的数据存储到文件中,使用的时候可以使用Map读取,从而快速获得value * 而Map不具备写出数据到文件和从文件读取的功能; * 这个功能是流的功能,如果把Map和流结合就可以方便完成这个功能,API中使用Properties可以完成 *  * 介绍:Map集合下有一个类叫Hashtable,后来被HashMap代替,Hashtable的子类Properties属于Map的类,但是 * 可以和流关联;Properties没有泛型,因为它存储的key value都是String 类型; * 方法: * setProperty集合存放元素 * list打印集合数据 * store存储(save已过时)第二个参数是写入到文件的注释部分 * load加载文件 * getProperty获取属性如果不是String类型的需要转换 *  * 4.合并文件的改进:[4-1] * SequenceInputStream:输入流对象序列化,即把流对象有序排列(Sequence流只有输入流) *  *  * */public class TProperties {public static void main(String[] args) {// [3-1]Properties测试// TestPropertises();// [4-1]合并文件改进// advancedMergeFile();}private static void advancedMergeFile() {// MAC OS路径,Windows需要更改格式File file = new File("/Users/lxt/eclipse/06IO");File config = getFileConfig(file);Properties properties = new Properties();try {properties.load(new FileInputStream(config));// properties.list(System.out);String filename = properties.getProperty("filename");int count = Integer.parseInt(properties.getProperty("count"));// 判断文件是否丢失File[] files = file.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {return pathname.isFile()&& pathname.getName().endsWith(".abcd");}});if (files.length < count) {throw new RuntimeException("文件丢失");}// 合并文件merFile(file, filename, count);} catch (IOException e) {e.printStackTrace();}}private static void merFile(File dir, String filename, int count)throws FileNotFoundException {// 存放用于和碎文件关联的流ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();// 获取文件碎片,并和输入流关联for (int i = 1; i <= count; i++) {File file = new File(dir, "file" + i + ".abcd");list.add(new FileInputStream(file));}// 通过集合获取Enumeration,创建对象流序列化Enumeration<FileInputStream> en = Collections.enumeration(list);// 创建流对象的序列化对象SequenceInputStream sis = new SequenceInputStream(en);// 创建输出流对象//如果需要高效,可以用BufferedOutputStreamFileOutputStream fos = new FileOutputStream(new File(dir, "3"+ filename));byte[] buf = new byte[1024];int len = 0;try {while ((len = sis.read(buf)) != -1) {fos.write(buf, 0, len);fos.flush();}sis.close();fos.close();} catch (IOException e) {e.printStackTrace();}}private static File getFileConfig(File tmp) {File[] files = tmp.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {return pathname.isFile()&& pathname.getName().endsWith(".properties");}});if (files.length != 1) {throw new RuntimeException("配置文件信息不唯一或者不存在");}return files[0];}private static void TestPropertises() {save();// 保存read();}private static void read() {Properties properties = new Properties();try {// MAC OS路径,Windows需要更改格式properties.load(new FileReader(new File("info.properties")));properties.list(System.out);System.out.println("————————————file name:"+ properties.getProperty("filename"));System.out.println("————————————count:"+ properties.getProperty("count"));} catch (IOException e) {e.printStackTrace();}}private static void save() {// 创建集合对象Properties properties = new Properties();// 集合存放元素properties.setProperty("filename", "test.avi");properties.setProperty("count", "5");// 测试properties.list(System.out);// 存储try {// "我勒个去"写入到info.properties的注释部分,按照Unicode编码,汉字编码会变得看不懂properties.store(new FileOutputStream(new File("info.properties")),"我勒个去");} catch (IOException e) {e.printStackTrace();}}}


0 0