FileUtil.java工具类
/*
* Copyright (c) 2006 - 2022, wzt.cn All rights reserved.
*
*/
package cn.wzt;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
/**
* <p>Project: app2022 - FileUtil
* <p>Powered by lenovo on 2022-02-06 11:11
* <P>Created by IntelliJ IDEA
*
* @author lenovo
* @version 1.0
* @since 17
*/
public class FileUtil {
public Map<String, List<String>> map=new HashMap<>();
public int wenjian=0;
public int zonghang=0;
public int konghang=0;
public int zhushi=0;
/**
* @Description: 统计java文件行数
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 14:34
*/
public void tjjavahs(String file){
tjjavahs(new File(file));
}
/**
* @Description: 统计java文件行数
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 14:34
*/
public void tjjavahs(File file){
select(file);
System.out.printf("共有%d个java文件,总行%d行,空行%d行,注释行%d行", wenjian,zonghang,konghang,zhushi);
}
/**
* @Description: 递归遍历文件
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/1/21 17:04
*/
public void select(File file){
if (file.isDirectory()){
for (File t : file.listFiles()) {
if(t.isDirectory()){
select(t);
}else {
panduan(t);
}
}
}else {
panduan(file);
}
}
/**
* @Description: 判断记录行号
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/1/21 17:05
*/
public void panduan(File file) {
try (var bu = new BufferedReader(new FileReader(file))){
if (file.isFile()&&file.getName().endsWith(".java")){
//java文件数
++wenjian;
//总行
zonghang+=bu.lines().count();
//空行
BufferedReader bu2 = new BufferedReader(new FileReader(file));
konghang+=bu2.lines().filter(e->e.trim().length()==0).count();
//注释行
BufferedReader bu3 = new BufferedReader(new FileReader(file));
//"/\\*|\\*|//"表示/*或*或//
zhushi+=bu3.lines().filter(e->e.trim().matches("((/\\*|\\*|//).*|.*//[a-zA-Z_0-9\\u4e00-\\u9fa5\\s]*$)")).count();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Description: 合并文件
* @Param: [dq读取的目录或文件, xr写入到的文件]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 14:20
*/
public void hebing(String dq,String xr){
hebing(new File(dq),new File(xr));
}
/**
* @Description: 合并文件
* @Param: [dq读取的目录或文件, xr写入到的文件]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 14:20
*/
public void hebing(File dq,File xr){
if (dq.isDirectory()) {
for (File t : dq.listFiles()) {
if (t.isDirectory()) {
hebing(t,xr);
}else {
if (t.isFile()&&t.getName().endsWith(".java")){
dqxr(t,xr);
}
}
}
}else {
if (dq.isFile()&&dq.getName().endsWith(".java")){
dqxr(dq,xr);
}
}
}
/**
* @Description: 读取写入、合并
* @Param: [dq读取的目录或文件, xr写入到的文件]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 14:16
*/
public void dqxr(File dq,File xr){
try {
BufferedReader bur = new BufferedReader(new FileReader(dq));
BufferedWriter buw = new BufferedWriter(new FileWriter(xr, true)) ;
StringBuilder sb=new StringBuilder();
sb.append("---------------------------------------------------------------------------------\n");
sb.append(String.format("--文件名:%s%n",dq.getName()));
sb.append(String.format("--文件路径:%s%n",dq.getPath()));
sb.append("---------------------------------------------------------------------------------\n");
buw.write(String.valueOf(sb));
//方法一,无行号
// String s=bur.lines().collect(Collectors.joining("\n"));
// buw.write(s+"\n");
//方法二,有行号
int num=0;
while (bur.ready()){
String s=String.format("%d %s",++num,bur.readLine());
buw.write(s+"\n");
}
buw.close();
// System.out.println(s);
// System.out.println(dq.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Description: 复制文件
* @Param: [ywj原文件, scwj生成文件]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 14:05
*/
public void fzwj(String ywj,String scwj){
fzwj(new File(ywj),new File(scwj));
}
/**
* @Description: 复制文件
* @Param: [ywj原文件, scwj生成文件]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 14:05
*/
public void fzwj(File ywj,File scwj){
try {
FileInputStream fi = new FileInputStream(ywj);
FileOutputStream fi2 = new FileOutputStream(scwj);
fi2.write(fi.readAllBytes());//=fi.transferTo(fi2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Description: 文件种类个数
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/1/21 17:10
*/
public void fenlei(String file){
fenlei(new File(file));
}
/**
* @Description: 文件种类个数
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/1/21 17:10
*/
public void fenlei(File file){
diguimulu(file);
map.forEach((k, v) -> System.out.printf("%s = %d %n", k, v.size()));//k表示key
}
/**
* @Description: 递归查看目录下文件
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/1/21 17:10
*/
public void diguimulu(File file){
if (file.isDirectory()){
for (File o : file.listFiles()) {
if (o.isDirectory()) {
diguimulu(o);
}else {
chuli(o);
}
}
}else {
chuli(file);
}
}
/**
* @Description: 统计文件类型
* @Param: [file]
* @return: void
* @Author: wzt
* @Date: 2022/1/21 17:10
*/
public void chuli(File file){
String name= file.getName();
String lei=name.lastIndexOf(".")==-1?"类型未知":name.substring(name.lastIndexOf(".")+1);
if (map.containsKey(lei)) {
List<String> list=map.get(lei);
list.add(file.getAbsolutePath());
map.put(lei,list);
}else {
List<String> list=new ArrayList<>();
list.add(file.getAbsolutePath());
map.put(lei,list);
}
}
/**
* @Description: 递归拷贝目录和文件
* @Param: [yuan, fuzhi]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 13:28
*/
public void copyDir(String yuan,String fuzhi){
copyDir(new File(yuan),new File(fuzhi));
}
/**
* @Description: 递归拷贝目录和文件
* @Param: [yuan, fuzhi]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 13:28
*/
public void copyDir(File yuan, File fuzhi){
if(!fuzhi.exists()){
fuzhi.mkdirs();
if (yuan.isDirectory()){
for (File t : yuan.listFiles()) {
if (t.isDirectory()){
copyDir(t,new File(fuzhi.getPath(),t.getName()));//File(String,String)从父路径名字符串和子路径名字符串创建一个新的 File 实例
}else {
copyFile(t,fuzhi);
}
}
}else {
copyFile(yuan,fuzhi);
}
}
}
/**
* @Description: 拷贝文件
* @Param: [yuan, fuzhi]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 13:28
*/
public void copyFile(File yuan,File fuzhi){
try {
//toPath()返回从这个抽象路径构造的 java.nio.file.Path 对象
//getPath()将此抽象路径名转换为路径名字符串
Files.copy(yuan.toPath(), Paths.get(fuzhi.getPath()+"/"+yuan.getName()));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Description: 读取java文件内容
* @Param: [file]
* @return: java.lang.String
* @Author: wzt
* @Date: 2022/2/6 11:44
*/
public String duqu(String file){
return duqu(new File(file));
}
/**
* @Description: 读取java文件内容
* @Param: [file]
* @return: java.lang.String
* @Author: wzt
* @Date: 2022/2/6 11:44
*/
public String duqu(File file){
if(file.exists()&&file.isFile()){
BufferedReader fr = null;
try {
fr = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return fr.lines().collect(Collectors.joining("\n"));
}
return "读取失败";
}
/**
* @Description: 创建并写入文件
* @Param: [mc名称, nr内容]
* @return: void
* @Author: wzt
* @Date: 2022/2/6 11:29
*/
public void xieru(String mc,String nr){
File file = new File(mc);
try {
if (!file.exists()){
file.createNewFile();
}else {
// //方法一
// FileOutputStream fop = new FileOutputStream(mc,true);
// fop.write(String.format("%s%n",nr).getBytes(StandardCharsets.UTF_8));
// fop.flush();//刷新此输出流并强制写出任何缓冲的输出字节
// fop.close();//关闭此文件输出流并释放与此流关联的所有系统资源
//方法二
FileWriter fw = new FileWriter(mc, true);
fw.write(nr+"\n");
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Description: 文件类型识别
* @Param: [file]
* @return: java.lang.String
* @Author: wzt
* @Date: 2022/2/6 11:07
*/
public String getFileType(String file){
return getFileType(new File(file));
}
/**
* @Description: 文件类型识别
* @Param: [file]
* @return: java.lang.String
* @Author: wzt
* @Date: 2022/2/6 11:07
*/
public String getFileType(File file){
String lx="unknow";
HashMap<String, String> ft = new HashMap<>();
ft.put("jpg", "FFD8FF"); //JPEG (jpg)
ft.put("png", "89504E47"); //PNG (png)
ft.put("gif", "47494638"); //GIF (gif)
ft.put("tif", "49492A00"); //TIFF (tif)
ft.put("bmp", "424D"); //Windows Bitmap (bmp)
ft.put("dwg", "41433130"); //CAD (dwg)
ft.put("html", "68746D6C3E"); //HTML (html)
ft.put("rtf", "7B5C727466"); //Rich Text Format (rtf)
ft.put("xml", "3C3F786D6C");
ft.put("zip", "504B0304");
ft.put("rar", "52617221");
ft.put("psd", "38425053"); //Photoshop (psd)
ft.put("eml", "44656C69766572792D646174653A"); //Email [thorough only] (eml)
ft.put("dbx", "CFAD12FEC5FD746F"); //Outlook Express (dbx)
ft.put("pst", "2142444E"); //Outlook (pst)
ft.put("xls", "D0CF11E0"); //MS Word
ft.put("doc", "D0CF11E0"); //MS Excel 注意:word 和 excel的文件头一样
ft.put("mdb", "5374616E64617264204A"); //MS Access (mdb)
ft.put("wpd", "FF575043"); //WordPerfect (wpd)
ft.put("eps", "252150532D41646F6265");
ft.put("ps", "252150532D41646F6265");
ft.put("pdf", "255044462D312E"); //Adobe Acrobat (pdf)
ft.put("qdf", "AC9EBD8F"); //Quicken (qdf)
ft.put("pwl", "E3828596"); //Windows Password (pwl)
ft.put("wav", "57415645"); //Wave (wav)
ft.put("avi", "41564920");
ft.put("ram", "2E7261FD"); //Real Audio (ram)
ft.put("rm", "2E524D46"); //Real Media (rm)
ft.put("mpg", "000001BA"); //
ft.put("mov", "6D6F6F76"); //Quicktime (mov)
ft.put("asf", "3026B2758E66CF11"); //Windows Media (asf)
ft.put("mid", "4D546864"); //MIDI (mid)
String ext = getFileHeaderInfo(file, 14);
for (String k : ft.keySet()) {
if (ext.startsWith(ft.get(k))) {
lx = k;
break;
}
}
return lx;
}
/**
* @Description: 读取文件流
* @Param: [file, size]
* @return: java.lang.String
* @Author: wzt
* @Date: 2022/2/6 11:08
*/
public String getFileHeaderInfo(String file, int size){
return getFileHeaderInfo(new File(file),size);
}
/**
* @Description: 读取文件流
* @Param: [file, size]
* @return: java.lang.String
* @Author: wzt
* @Date: 2022/2/6 11:08
*/
public String getFileHeaderInfo(File file, int size) {
StringBuilder sbu = new StringBuilder();
byte[] buf = new byte[size];
try (FileInputStream fis = new FileInputStream(file)) {
fis.read(buf);
} catch (IOException e) {
e.printStackTrace();
}
for (byte b : buf) {
sbu.append(String.format("%02X", b));
}
return sbu.toString();
}
}
使用方法如下例所示:File7.java
/*
* Copyright (c) 2006 - 2022, wzt.cn All rights reserved.
*
*/
package cn.wzt;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
/**
* <p>Project: app2022 - File7
* <p>Powered by lenovo on 2022-02-06 10:35
* <P>Created by IntelliJ IDEA
*
* @author lenovo
* @version 1.0
* @since 17
*/
public class File7 {
public static void main(String[] args) {
FileUtil fu = new FileUtil();
System.out.println(fu.getFileType("D:\\桌面\\28.jj"));//识别28.是什么类型
System.out.println(fu.duqu("F:\\ideaxm\\app2022\\08io\\src\\main\\java\\cn\\wzt\\FileUtil.java"));//读取FileUtil.java内容
fu.fenlei("F:\\ideaxm\\app2022");//分类统计app2022下各类型文件
}
}