【java资源库:http://www.gxcode.top/code】
一.代码:
/**
* 统计文件夹的大小和文件个数
*/
public class DirCount {
public static long len =0l; //大小
public static int fileSize=0; //文件的个数
public static int dirSize=0;//文件夹的个数
private DirCount() {}
public static DirCount counDriNum(String path) {
File src = new File(path);
count(src);
return new DirCount();
}
/*
* 统计大小和文件的个数
*/
private static void count(File src) {
if(null!=src && src.exists()) {
if(src.isFile()) {
len+=src.length(); //文件大小
fileSize++; //文件个数
}else{
dirSize++; //文件夹的个数
for(File s:src.listFiles()) {count(s);}
}
}
}
}
-------------------
public class DriInfo {
//根据文件路径显示文件下所有子孙文件
public static Map<String,String[]> getFilePath(String filepath, Map<String,String[]> map) {
File f = new File(filepath);
String[] listAry ;
String parentPath ="";
if(f.isDirectory()) { //目录
listAry = f.list();
map.put(f.getPath(), listAry);
parentPath = f.getPath()+"\\";
for(String str : listAry) {
String newPath = parentPath+str;
if(new File(newPath).isDirectory()) {getFilePath(newPath, map); }
}
}else if(f.isFile()) { //文件
parentPath = f.getParent();//获取父目录;
listAry = new File(parentPath).list();
map.put(parentPath, listAry);
//判断文件下没有目录就结束递归
boolean isdirtory = false;
for(String str : listAry) {
String newPath = parentPath+"\\"+str;
isdirtory = new File(newPath).isDirectory();
if(isdirtory) {
isdirtory = true;
getFilePath(newPath, map);
}
}
if(!isdirtory) {return map;}
}
return map;
}
}
--------------------------------------
public class IoFile {
// 1.文件的拷贝
public static void copy(String srcPath,String destPath) {
//1、创建源
File src = new File(srcPath); //源头
File dest = new File(destPath);//目的地
//2、选择流
InputStream is =null;
OutputStream os =null;
try {
is =new FileInputStream(src);
os = new FileOutputStream(dest);
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
os.write(flush,0,len); //分段写出
}
os.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
close(os,is); //4、释放资源 分别关闭 先打开的后关闭
}
}
//2.字节数组到文件
public static OutputStream byteToFile(byte[] datas, String strFile) {
OutputStream os = null ;
try {
InputStream is = new ByteArrayInputStream(datas);
os = new FileOutputStream(strFile);
copy(is,os);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return os;
}
//3.文件到字节数组
public static byte[] fileToByte(InputStream is) {
byte[] datas = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
copy(is,os);
datas = os.toByteArray();
return datas;
}
//4.字符流数据写入指定文件
public static void writeStrFile(String cont, String path, boolean b) {
OutputStream os =null;
try {
File dest = new File(path); //1、创建源
os = new FileOutputStream(dest,b); //2、选择流
byte[] datas =cont.getBytes(); //字符串->字节数组(编码 )
os.write(datas,0,datas.length); //3、操作(写出)
os.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
close(os); //4、释放资源
}
}
//5.字节流读取指定文件内容
public static String readStrFile(String pathFile) {
StringBuilder sb = new StringBuilder("");
InputStream is =null; //2、选择流
try {
is =new FileInputStream(new File(pathFile)); //1、创建源
//3、操作 (分段读取)
byte[] flush = new byte[1024*10]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) { //字节数组-->字符串 (解码)
String str = new String(flush,0,len);
sb.append(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(is); //4、释放资源
}
return sb.toString();
}
//6.字节数组写到指定文件内容
public static void readByteFile(byte[] src,String filePath) {
//1、创建源
File dest = new File(filePath);
//2、选择流
InputStream is =null;
OutputStream os =null;
try {
is =new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
os.write(flush,0,len); //写出到文件
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(os); //4、释放资源
}
}
//7.指定文件获取字节数组
public static byte[] writeByteFile(String filePath) {
InputStream is =null;//2、选择流
ByteArrayOutputStream baos =null;
try {
is =new FileInputStream(new File(filePath)); //1、创建源与目的地
baos = new ByteArrayOutputStream();
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
baos.write(flush,0,len); //写出到字节数组中
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(is); //4、释放资源
}
return null;
}
//8.把对象转字节数组
public static byte[] objToByte(Object obj) {
byte[] datas = null;
//写出 -->序列化
ByteArrayOutputStream baos =new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos= new ObjectOutputStream(new BufferedOutputStream(baos));
oos.writeObject(obj);
oos.flush();
datas =baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}finally{
close(oos); //4、释放资源 分别关闭 先打开的后关闭
}
return datas;
}
//9.把字节数组转对象
public static Object byteToObj(byte[] datas) {
ObjectInputStream ois = null;
Object obj = null;
try {
ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
obj = ois.readObject();
} catch (IOException e) {
System.out.println("转对象输入流错误!!!");
}catch (ClassNotFoundException e) {
System.out.println("转对象错误!!!");
}finally {
close(ois); //4、释放资源 分别关闭 先打开的后关闭
}
return obj;
}
//对接输入输出流
public static void copy(InputStream is,OutputStream os) {
try {
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
os.write(flush,0,len); //分段写出
}
os.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
close(os,is); //4、释放资源 分别关闭 先打开的后关闭
}
}
// 释放资源
public static void close(Closeable... ios) {
for(Closeable io:ios) {
try {
if(null!=io) {io.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
------------
public class SplitFile {
/*
* 分割:
* 源文件路径:srcPath| 目标文件路径:destDir|分割每块大小(字节):blockSize
*/
public static List<String> split(String srcPath, String destDir, int blockSize) {
/* 1.init初始信息 */
File src =new File(srcPath);
List<String> destPaths =new ArrayList<String>();
long len = src.length(); //总长度
int size =(int) Math.ceil(len*1.0/blockSize); //块数: 多少块
for(int i=0;i<size;i++) { //路径
destPaths.add(destDir +"/"+i+"-"+src.getName());
}
/*2.起始位置和实际大小*/
int beginPos = 0;
int actualSize = (int)(blockSize>len?len:blockSize);
for(int i=0;i<size;i++) {
beginPos = i*blockSize;
if(i==size-1) { //最后一块
actualSize = (int)len;
}else {
actualSize = blockSize;
len -=actualSize; //剩余量
}
/*3.指定第i块的起始位置 和实际长度*/
RandomAccessFile raf = null;
RandomAccessFile raf2 = null;
try {
raf =new RandomAccessFile(src,"r");
raf2 =new RandomAccessFile(destPaths.get(i),"rw");
raf.seek(beginPos); //随机读取
//读取 3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len1 = -1; //接收长度
while((len1=raf.read(flush))!=-1) {
if(actualSize>len1) { //获取本次读取的所有内容
raf2.write(flush, 0, len1);
actualSize -=len1;
}else {
raf2.write(flush, 0, actualSize);
break;
}
}
}catch (IOException e) {
System.out.println("详细分割信息错误");
}finally {
close(raf2,raf);
}
}
return destPaths;
}
/*
* 文件的合并
* 合并的多个文件路径:destPaths | 合并后新文件:destPath
*/
public static void merge(String destPath, List<String> destPaths) {
OutputStream os = null;
SequenceInputStream sis =null;
try {
//输出流
os =new BufferedOutputStream( new FileOutputStream(destPath,false));//true:表示追加
Vector<InputStream> vi = new Vector<InputStream>();
//输入流
for(int i=0;i<destPaths.size();i++) {
vi.add(new BufferedInputStream(new FileInputStream(destPaths.get(i))));
}
sis =new SequenceInputStream(vi.elements());
//拷贝
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=sis.read(flush))!=-1) {
os.write(flush,0,len); //分段写出
}
os.flush();
}catch(IOException e) {
System.out.println("文件和的时候报错!!!");
}finally {
close(sis,os);
}
}
/* 释放资源*/
public static void close(Closeable... ios) {
for(Closeable io:ios) {
try {
if(null!=io) {io.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
----------
/**
* 字符编码和解密工具
*/
public class StrinDE {
//字符编码工具
public static byte[] getStrToByte(String str,String charset) {
try {
if(str!=null && str.length()>0) {
byte[] ary = str.getBytes(charset);
return ary;
}
} catch (UnsupportedEncodingException e) {
System.out.println("字符串:编码错误");
}
return null ;
}
//字符解密码工具
public static String getByteToStr(String charset,byte[] ary) {
try {
if(ary!=null && ary.length>0) {
String str = new String(ary,0,ary.length,charset);
return str;
}
} catch (UnsupportedEncodingException e) {
System.out.println("字节数组:解密错误");
}
return null ;
}
}
----------
public class URLToString {
public static String getStrToURL(String url,String charset,String desPath) {
if(charset==null) { charset= "UTF-8";}
if(url==null) { url = "http://www.baidu.com" ;}
if(desPath==null) { desPath = System.getProperty("user.dir") ; }
String count = "";
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(new URL(url).openStream(),charset));
writer =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(desPath),charset));
while((count=reader.readLine())!=null) {
writer.write(count); //字符集不统一不够出现乱码
writer.newLine();
}
writer.flush();
}catch(IOException e) {
System.out.println("获取网络流数据错误!!!");
}finally {
close(reader,writer);
}
return count;
}
public static void close(Closeable... ios) {
for(Closeable io:ios) {
try {
if(null!=io) {io.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二.工具类说明:
1.com.djh.utils.io.StrinDE
1.1编码方法:
byte[] ary1 = StrinDE.getStrToByte(“你好,我是…”, “GBK”);
1.2 解码方法:
String newStr = StrinDE.getByteToStr(“GBK”, ary1);
System.out.println(newStr);
2.com.djh.utils.io.DirCount
根据目录统计:
DirCount.counDriNum(“D:\other”);
文件夹大小:
DirCount.len;
文件夹下目录多少:
DirCount.fileSize);
文件夹下文件多少:
DirCount.dirSize);
3.com.djh.utils.io.DriInfo
根据文件路径,获取该文件下所有文件:【说明key:文件夹路径 ,value:文件夹路径下文件(文件夹和文件)】
Map<String,String[]> resultMap = DriInfo.getFilePath(“D:\other”,new HashMap<String,String[]>());
4.com.djh.utils.io.IoFile
4.1 拷贝单个文件:
IoFile.copy(String srcPath,String destPath);
4.2 文件到字节数组:
byte[] ary1 = IoFile.fileToByte(new FileInputStream(“D:\other\H.java”));
System.out.println(ary1.length);
4.3 字节数组到文件:
IoFile.byteToFile(ary11, “D:\other\p.java”);
4.4数据写入指定文件
IoFile.writeStrFile("要写内容","D:\\OO.txt",true); //文件末尾追加
4.5 指定文件路径读取内容
IoFile.readStrFile("D:\\\\OO.txt");
4.6 文件到字节数组中
byte[] byteAry = IoFile.writeByteFile("D:\\\\111.jpg");
4.7 字节数组到文件中
IoFile.readByteFile(byteAry,"D:\\\\333.jpg");
4.8 对象序列化为字节数组
//注意People类需要实现Serializable接口。
byte[] dates = IoFile.objToByte(new People("nn",10));
4.9 字节数组反序列化为对象
Object o = IoFile.byteToObj(dates);
5.com.djh.SplitFile
5.1文件分割: 返回分割后多个文件路径名。
List destPaths = SplitFile.split(“D:\tupain.jpg”, “D:\”,1024*10);
5.2文件合并
SplitFile.merge(“D:\newtupain.jpg”, destPaths);
6.com.djh.URLToString
6.1 根据网络URL获取和编码获取网络资源信息
URLToString.getStrToURL(“www.baidu.com”,“UTF-8”,“D:\page.html”);