1 Properties
Properties 实现了Map接口, 存放的是键值对数据,而且键同样不能重复(重复会覆盖);不同的是,Properties的键值数据类型都是字符串类。Properties可以和流操作,快速生成含键值对的配置文件,和快速解析配置文件。
1.1 生成配置文件
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
setValue();
System.out.println("保存成功");
}
private static void setValue() throws IOException{
Properties pro = new Properties();
pro.setProperty("蜀", "刘备");
pro.setProperty("魏", "曹操");
pro.setProperty("吴", "孙权");
pro.setProperty("蜀", "孔明");
FileOutputStream out = new FileOutputStream("G:\\myFileOutputStream.properties");
FileWriter writer = new FileWriter("G:\\myWriter.properties");
pro.store(out, "FileOutputStream");
pro.store(writer, "FileWriter");
out.close();
}
}
myFileOutStream.properties内容: #FileOutputStream
#Tue May 24 11:55:27 CST 2016
\u8700=\u5B54\u660E
\u5434=\u5B59\u6743
\u9B4F=\u66F9\u64CD
myWriter,properties内容: #FileWriter
#Tue May 24 11:55:27 CST 2016
蜀=孔明
吴=孙权
魏=曹操
说明:以Stream存储的数据是以unicode进行编码的, 不能直接打开。以Writer存储的数据则可以直接用文本工具打开。
1.2 读取配置文件的信息
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
getValue();
}
private static void getValue() throws IOException{
Properties pro = new Properties();
FileReader inStream = new FileReader("G:\\myFileOutputStream.properties");
pro.load(inStream);
Set<String> keys = pro.stringPropertyNames();
for(String key : keys){
System.out.println(key+" : "+pro.getProperty(key));
}
}
}
运行结果: 蜀 : 孔明
吴 : 孙权
魏 : 曹操
2 文件分割合并
当我们在存储文件或传输文件时, 当文件过大时, 我们通常会将一个大文件按多个小文件存储, 发送完毕后,再重新合并成原来文件。合并文件用到流SequenceInputStream
2.1 文件分割
文件分割时,我们通常会生成一个配置文件, 用来存储原文件信息(比如文件后缀名)。我们将生成的文件配置信息和切分的文件块存放同一个文件夹
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Properties;
public class SequenceInputStreamDemo {
public static final int SPLIT_SIZE = 1024 * 1024 ;//定义切割1M的大小
public static void main(String[] args) throws IOException {
splitFile("G:\\easy_ui\\jquery-easyui-1.3.4.zip");
}
private static void splitFile(String path) throws IOException{
File fileOrg = new File(path);
String dirPath = fileOrg.getParent();//获取父目录路径。
String fileName = fileOrg.getName(); //获取文件名
String prefix=fileName.substring(0, fileName.lastIndexOf("."));//文件名称不含后缀名
String suffix=fileName.substring(fileName.lastIndexOf(".")+1);//后缀名 .zip
//=============1 校验文件夹信息,将相关配置信息读入配置文件(info.property)============
File dir = new File(new File(dirPath), prefix+"_part");
if(!dir.exists()){
dir.mkdirs();
}
Properties pros = new Properties();
pros.setProperty("dir", dir.getName()); //目录非文件名
pros.setProperty("fileName", prefix);
pros.setProperty("suffix", suffix);
int partcount =(int) Math.ceil((double)fileOrg.length()/SPLIT_SIZE);
pros.setProperty("partNumber", partcount + "");
pros.setProperty("备注", "拆分配置文件");
FileWriter fileInfo = new FileWriter(new File(dir , "info.property"));
pros.store(fileInfo, "文件拆分");
fileInfo.close();
//=============2 拆分文件 ===========================================
FileInputStream fileStreamOrg = new FileInputStream(fileOrg);
byte[] buf = new byte[SPLIT_SIZE];
FileOutputStream fos = null;
int len = 0;
int count = 1;
while ((len = fileStreamOrg.read(buf)) != -1) {
fos = new FileOutputStream(new File(dir, (count++) + ".part"));
fos.write(buf, 0, len);
}
fileStreamOrg.close();
fos.close();
}
}
运行结果:
2.1 合并文件
通过SequenceInputStream(Enumeration<? extends InputStream> e) 将多个文件流合并成单个流,再写入到硬盘
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
public class SequenceInputStreamDemo {
public static final int SPLIT_SIZE = 1024 * 1024 ;//定义切割1M的大小
public static void main(String[] args) throws IOException {
mergeFile("G:\\easy_ui\\jquery-easyui-1.3.4_part");
}
private static void mergeFile(String dirPath) throws IOException{
File dir = new File(dirPath);
File[] files = dir.listFiles(new MyFilter(".part"));
Properties pro = new Properties();
FileReader fReader = new FileReader(new File(dir,"info.property"));
pro.load(fReader);
if(!(files.length+"").equals(pro.getProperty("partNumber"))){//文件和配置信息出错
throw new RuntimeException("源文件出错!");
}
ArrayList<FileInputStream> lsInputStream = new ArrayList<FileInputStream>();
for(int i=0,len=files.length; i<len; i++){
lsInputStream.add(new FileInputStream(files[i]));
}
Enumeration<FileInputStream> en = Collections.enumeration(lsInputStream);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(new File(dir,pro.getProperty("fileName")+"."+pro.getProperty("suffix")));
byte[] buf = new byte[1024];
int len;
while((len=sis.read(buf))!=-1){
fos.write(buf,0,len);
}
sis.close();
fos.close();
}
}
class MyFilter implements FilenameFilter {
private String suffix;
public MyFilter(String suffix){
this.suffix = suffix;
}
@Override
public boolean accept(File dir, String name) {
if(name.endsWith(suffix))
return true;
return false;
}
}
运行结果:合并生成文件 jquery-easyui-1.3.4.zip