**磁盘文件的检索**
package com.jalor.others;
import java.io.File;
import java.io.FileNotFoundException;
public class SearchFile {
public static void main(String[] args) throws FileNotFoundException {
File files = new File("D:/"); // 创建File对象,指向F盘根目录
String[] names = files.list(); // 获取D盘根目录所有文件和路径,并以字符串数组返回
System.out.println(files.getPath() + files.getName());
for (String s : names) { // 遍历字符串数组
boolean a = s.startsWith("so"); // 文件名前缀带有so的返回true,没有则返回false
boolean b = (new File(files.getAbsolutePath() + s)).isFile(); // 判断本次循环的字符串所指向的内容是否是文件,是则返回true.否则返回false
boolean c = s.contains("va"); // 文件名是否包含"va",包含则返回true,否则false
if (a && b) { // 此处条件根据需要进行修改
System.out.println(s); // 打印出符合条件的文件
}
}
}
}
**单例模式**
由于时间关系,笔者就不贴代码,您可以参考一下:
https://blog.csdn.net/qq_40424244/article/details/82890503
**身份证号码验证**
package com.jalor.tky;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TKY_20180704 {
public static void main(String[] args) {
String idCard = "142401196008310352你好啊";
// 检测身份证是否包含中文
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");
Matcher aMatcher = pattern.matcher(idCard);
boolean isZH = aMatcher.find();
System.out.println(isZH);
// 检测身份证位数是否是正常是法定位数(15,17,18)
boolean amount = (idCard.length() == 15 || idCard.length() == 17 || idCard.length() == 18);
System.out.println(amount);
// 两方面条件都满足时,返回true
final boolean existed = (isZH == false) && (amount == true);
System.out.println(existed);
}
}
**split的使用**
package com.jalor.tky;
import java.util.HashMap;
import java.util.Set;
public class TKY_20180327 {
public static void main(String[] args) {
HashMap<String,String> hm = new HashMap<>();
String str = "lily@sohu.com,tom@163.com,rock@sina.com";
//分割后存入数组
String[] strArr = str.split(",");
for (String tmp : strArr) {
int index = tmp.indexOf("@");
String key = tmp.substring(0, index);
String value = tmp.substring(index);
hm.put(key, value);
}
//遍历集合
Set<String>set = hm.keySet();
System.out.println("邮箱名\t地址名");
for (String key : set) {
System.out.println(key+"\t"+hm.get(key));
}
}
}
**读取压缩包ZIP中的文件**
package com.jalor.sinponet;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class AnalyzeZIP {
public static void main(String[] args) {
String str = "D:\\1.zip";
try {
readZipFile(str);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void readZipFile(String file) throws Exception {
ZipFile zf = new ZipFile(file);
InputStream in = new BufferedInputStream(new FileInputStream(file));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
} else {
System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
System.out.println();
}
}
zin.closeEntry();
}
}
demo
package com.jalor.sinponet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.imageio.stream.FileImageInputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class AA {
public static void main(String[] args) {
try {
String zipPath = "D:\\UnZipOrRar\\photo\\photos.zip";
File zipFile = new File(zipPath);
String descDir = "D:\\UnZipOrRar\\photo\\temp1";
List<String> urlList = new ArrayList<>();
String type = zipPath.substring(zipPath.lastIndexOf(".") + 1);
if (type.equals("zip")) {
// UnZipOrRarUtils.unzip(zipPath, descDir, urlList);
unZipFiles(zipPath, descDir);
} else {
throw new Exception("只支持zip和rar格式的压缩包!");
}
// System.out.println(urlList);
// unZip(zipFile, descDir, urlList);
// readZipFile("c:\\photo\\photos.zip");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static boolean unZipFiles(String zipFileName, String extPlace) throws Exception {
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName, "GBK"); // 处理中文文件名乱码的问题
if ((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = tempFile.getAbsolutePath();
Enumeration<?> e = zipFile.getEntries();
while (e.hasMoreElements()) {
ZipEntry zipEnt = (ZipEntry) e.nextElement();
String str = String.valueOf(zipEnt);
String s = str.substring(str.lastIndexOf("/") + 1, str.length());
System.out.println("这是一个从zip中解压出来的文件:" + s);
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else { // 读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;// 建目录
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ((c = bis.read()) != -1) {
bos.write((byte) c);
}
bos.close();
fos.close();
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// 图片到byte数组
public static byte[] image2byte(String path) {
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
System.out.println("test" + data);
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
}
**读取压缩包RAR中的文件**
package com.jalor.sinponet;
import java.io.File;
import java.io.FileOutputStream;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
public class UnRARTools {
public static void main(String[] args) {
File file1 = new File("D:\\1.rar");
File file2 = new File("D:\\");
try {
unrar(file1, file2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void unrar(File sourceRar, File destDir) throws Exception {
Archive archive = null;
FileOutputStream fos = null;
System.out.println("Starting...");
try {
archive = new Archive(sourceRar);
FileHeader fh = archive.nextFileHeader();
int count = 0;
File destFileName = null;
while (fh != null) {
System.out.println((++count) + ") " + fh.getFileNameString());
String compressFileName = fh.getFileNameString().trim();
destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);
if (fh.isDirectory()) {
if (!destFileName.exists()) {
destFileName.mkdirs();
}
fh = archive.nextFileHeader();
continue;
}
if (!destFileName.getParentFile().exists()) {
destFileName.getParentFile().mkdirs();
}
fos = new FileOutputStream(destFileName);
archive.extractFile(fh, fos);
fos.close();
fos = null;
fh = archive.nextFileHeader();
}
archive.close();
archive = null;
System.out.println("Finished !");
} catch (Exception e) {
throw e;
} finally {
if (fos != null) {
try {
fos.close();
fos = null;
} catch (Exception e) {
// ignore
}
}
if (archive != null) {
try {
archive.close();
archive = null;
} catch (Exception e) {
// ignore
}
}
}
}
}
**读取压缩包ZIP和RAR的工具类**
package com.jalor.sinponet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
/**
*
* @author liuBf 类说明:解压文件公用类 *
*/
public class UnZipOrRarUtils {
public static void main(String[] args) {
// throw new Exception("只支持zip和rar格式的压缩包!")
try {
// untar("D:\\1.rar", "D:\\png");
unRarFile("D:\\UnZipOrRar\\1.rar", "D:\\UnZipOrRar\\");
unzip("D:\\UnZipOrRar\\2.zip", "D:\\UnZipOrRar\\");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*** 这里用到了synchronized ,也就是防止出现并发问题 ***/
public static synchronized void untar(String tarFileName, String extPlace) throws Exception {
unRarFile(tarFileName, extPlace);
}
public static synchronized void unzip(String zipFileName, String extPlace) throws Exception {
unZipFiles(zipFileName, extPlace);
}
/**
* 解压zip格式的压缩文件到指定位置
*
* @param zipFileName
* 压缩文件
* @param extPlace
* 解压目录
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static boolean unZipFiles(String zipFileName, String extPlace) throws Exception {
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new ZipFile(zipFileName, "GBK"); // 处理中文文件名乱码的问题
if ((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = tempFile.getAbsolutePath();
Enumeration<?> e = zipFile.getEntries();
while (e.hasMoreElements()) {
ZipEntry zipEnt = (ZipEntry) e.nextElement();
System.out.println("这是一个从zip中解压出来的文件:" + zipEnt.getName());
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else { // 读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;// 建目录
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ((c = bis.read()) != -1) {
bos.write((byte) c);
}
bos.close();
fos.close();
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据原始rar路径,解压到指定文件夹下.
*
* @param srcRarPath
* 原始rar路径
* @param dstDirectoryPath
* 解压到的文件夹
*/
public static void unRarFile(String srcRarPath, String dstDirectoryPath) {
if (!srcRarPath.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return;
}
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
Archive a = null;
try {
a = new Archive(new File(srcRarPath));
if (a != null) {
// a.getMainHeader().print(); // 打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
// 防止文件名中文乱码问题的处理
String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW();
System.out.println("这是一个从rar解压出来的文件:" + fileName);
if (fh.isDirectory()) { // 文件夹
File fol = new File(dstDirectoryPath + File.separator + fileName);
fol.mkdirs();
} else { // 文件
File out = new File(dstDirectoryPath + File.separator + fileName.trim());
try {
if (!out.exists()) {
if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}