——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——
1:文件的合并SequenceInputStream(读取多个文件,写入一个文件中)
获取Enumeration有两种方式
1.1:Vector ve=new Vector();
Enumeration enu = ve.elements();
1.2:List lst=new ArrayList();
Iterator ite =lst.iterator();
第二种执行效率要高
2:文件的分割(读取一个文件,读一byte新建一个File)
3:io之properties常见的方法
Properties pro=new Properties();
setProperty();
getProperty();
pro.load(new FileInputStream(new File(“xxx.properties”)));
pro.store(new FileOutputStream(new File(“xxx.properties”)));
package com.itheima.www3;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.InputStreamReader;
import java.io.PrintStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
/**
* io 基础
*
* @author myluo
*
*/
public class IOFile2 {
public static void main(String[] args) {
//文件清单
//method();
//properties 类常见方法
//method3();
//method5();
//method6();
//method7();
//method8();
//method9();
// method10();
method11();
}
//合并文件,第二中写法(枚举),list效率比vector高
private static void method11() {
// TODO Auto-generated method stub
List<FileInputStream> lst=new ArrayList<FileInputStream>();
for(int i=1;i<=4;i++){
try {
lst.add(new FileInputStream("e:/abc/"+i+".part"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建一个迭代器
final Iterator<FileInputStream> iter=lst.iterator();
Enumeration<FileInputStream> enu=new Enumeration<FileInputStream>() {
@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return iter.hasNext();
}
@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return iter.next();
}
};
//开始合并
SequenceInputStream sls=new SequenceInputStream(enu);
FileOutputStream fos=null;
BufferedOutputStream bos=null;
try {
fos= new FileOutputStream(new File("E:/abc/luo.CHM"));
bos=new BufferedOutputStream(fos);
byte [] bt=new byte[1024];
int len = -1;
try {
while((len=sls.read(bt))!=-1){
bos.write(bt, 0, len);
}
bos.flush();
if(sls!=null)
sls.close();
if(bos!=null)
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//文件的切割,一个读取流,对应多个输出流
private static void method10() {
// TODO Auto-generated method stub
FileInputStream fls=null;
FileOutputStream fos=null;
try {
fls=new FileInputStream(new File("C:/Users/Administrator/Desktop/JDK_API_1_6_zh_CN.CHM"));
byte [] bt=new byte[1024*1024*10];
int len=0;
int num=1;
try {
while((len=fls.read(bt))!=-1){
fos=new FileOutputStream(new File("e:/abc/"+num+".part"));
fos.write(bt,0,len);
fos.close();
num++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(fls!=null)
fls.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//(文件合并)合并流的应用,用SequenceInputStream 同时读取多个文件,多个读取流,对应一个输出流
private static void method9() {
// TODO Auto-generated method stub
//创建矢量对象
Vector <FileInputStream> fls=new Vector<FileInputStream>();
BufferedWriter bw=null;
SequenceInputStream sls=null;
try {
fls.add(new FileInputStream("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\count.txt"));
// fls.add(new FileInputStream("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\luo.txt"));
fls.add(new FileInputStream("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\count.properties"));
//创建收集对象
Enumeration<FileInputStream> elements = fls.elements();
//合并对象流
sls=new SequenceInputStream(elements);
try {
bw=new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\fei.txt"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte [] bt=new byte[1024];
int len=-1;
try {
while((len=sls.read(bt))!=-1){
bw.write(new String(bt,0,len));
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(sls!=null)
sls.close();
if(bw!=null){
bw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//打印流,常见方法PrintStream PrintWriter
private static void method8() {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintStream ps=null;
try {
ps = new PrintStream(new FileOutputStream("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\count.txt"),true);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String len=null;
try {
while((len=br.readLine())!=null){
ps.println(len.toUpperCase());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(br!=null)
br.close();
if( ps!=null)
ps.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//应用程序计数器
private static void method7() {
// TODO Auto-generated method stub
Properties pro=new Properties();
File file=new File("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\count.properties");
FileInputStream fls=null;
FileOutputStream fos=null;
try {
fls=new FileInputStream(file);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
pro.load(fls);
System.out.println(pro);
String value=pro.getProperty("number");
System.out.println(value);
int count=0;
fos=new FileOutputStream(file);
if(value!=null){
count=Integer.parseInt(value);
}
count++;
pro.setProperty("number", count+"");
pro.store(fos, "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(fos!=null)
fos.close();
if(fls!=null){
fls.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//properties对象的load方法 和 store方法
private static void method6() {
// TODO Auto-generated method stub
Properties pro=new Properties();
FileInputStream fls=null;
FileOutputStream fos=null;
try {
fls=new FileInputStream("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\luo.txt");
//properties 对象中load可以直接加载字节流
try {
pro.load(fls);
pro.setProperty("mini", "100");
fos=new FileOutputStream("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\luo.txt");
pro.store(fos, "zhushi");
System.out.println("方法1"+pro);
pro.list(System.out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(fos!=null)
fos.close();
if( fls!=null)
fls.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//读取文件,把键值对保存到properties对象,演示properties的load原理用的都是字节流
private static void method5() {
// TODO Auto-generated method stub
Properties pro=new Properties();
// 读取src目录下luo.txt
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\System1\\src\\luo.txt"));
String len=null;
try {
while((len=br.readLine())!=null){
String [] ss=len.split("=");
System.out.println(ss[0]+":"+ss[1]);
pro.setProperty(ss[0], ss[1]);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("pro==="+pro);
}
//properties 类常见方法
private static void method3() {
// TODO Auto-generated method stub
//配置对象
Properties pro=new Properties();
//pro的set方法
pro.setProperty("jack", "18");
pro.setProperty("kaka", "20");
System.out.println("键值对obj=="+pro);
//get方法,根据key ,获取value
String value = pro.getProperty("jack");
System.out.println("value==="+value);
//获取properties所有的key
Set<String> names=pro.stringPropertyNames();
//遍历names
for(String str:names){
System.out.println("name=="+str);
}
}
private static void method() {
File file=new File("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\");
List<File> list=new ArrayList<File>();
method1(file,list);
//String filepath="";
File file1=new File("C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\myluo.txt");
method2(list,file1.toString());
/* System.out.println("length=="+list.size());
for(File f:list){
System.out.println("==="+f);
}*/
}
/**
*
* @param list 存储的文件
* @param string 文件清单保存的路径
*
*/
private static void method2(List<File> list, String string) {
// TODO Auto-generated method stub
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(string));
for(File lst:list){
bw.write(lst.getAbsolutePath());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(null!=bw)
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param file 读取的文件
* @param list 读取的文件保存到list集合
*/
private static void method1(File file, List<File> list) {
// TODO Auto-generated method stub
File [] f=file.listFiles();
for(File ff:f){
if(ff.isDirectory()){
//如果是文件夹,继续递归
method1(ff,list);
}else{
//如果文件是后缀是.java 就保存到集合
if(ff.toString().endsWith(".java")){
list.add(ff);
}
}
}
}
}
时间: 2024-10-11 19:16:30