package com.org.utils;
import java.io.File;
public class FileUitls {
/**
* 直接過濾圖片所有格式的 重命名文件裏面的名稱
* @param fileDir
* @param sequenceCode
*/
public static void renameFile(File fileDir, String sequenceCode) {
File[] files = fileDir.listFiles();
for (int k = 0; k < files.length; k++) {
StringBuffer sb = new StringBuffer(sequenceCode);
if (files[k].isDirectory()) {
renameFile(files[k], sequenceCode);
} else {
if (k < 10)
sb.append("_000").append(k);
else if (k >= 10 && k < 100)
sb.append("_00").append(k);
else if (k < 1000 && k >= 100)
sb.append("_0").append(k);
else
sb.append("_").append(k);
final int index = files[k].getName().lastIndexOf(".") + 1;
final String fileType = files[k].getName().substring(index);
sb.append(".").append(fileType);
final String name = sb.toString();
final File dirFile = new File(fileDir, name);
System.out.println("Rename File Path:" + files[k].getAbsolutePath());
files[k].renameTo(dirFile);
}
}
}
}
package com.org.domain;
import java.io.File;
import com.org.utils.FileUitls;
/**
*@Author:liangjilong
*@Date:2014-6-8
*@Version:1.0
*/
public class TestFile {
/**
* @param args
*/
public static void main(String[] args) {
String path="E:/窗簾";
FileUitls.renameFile(new File(path), "黑絲磨毛雙面印花");
}
}
package com.org.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*@Author:jilongliang
*@Date:2013-8-11
*@version:1.0
*@Email:[email protected]
*===============================================================================================================
*實際上字節流在操作時本身不會用到緩衝區(內存),是文件本身直接操作的,而字符流在操作時使用了緩衝區,通過緩衝區再操作文件。
*
*字符流和字節流是根據處理數據的不同來區分的。字節流按照8位傳輸,字符流按照16位傳輸
*由於字符流使用Unicode字符集,支持多國文字,因此若流要跨越多種平臺傳輸,應使用字符流。
*===============================================================================================================
*char型是字符型,佔2個字節,默認數值'\u0000',取值範圍'\u0000'~'\uffff'
*byte是字節型,佔1個字節,默認數值0,取值範圍-128~127
*boolean 無符號 8 位
*byte 無符號,8 位
*char 無符號,16 位
*short 有符號,16 位
*int \ 有符號,32 位
*long 有符號,64 位
*float 32 位
*double 64 位
*===============================================================================================================
*字節流輸出--->>程序-->>字節流---->文件(直接操作文件)
*
*字符流輸出--->>程序-->>字符流---->>緩存--->>文件(數據線放在緩存,之後再從緩存寫入文件)
*
*===============================================================================================================
*@Description:字節流
*@InputStream
* FileInputStream, FilterInputStream,BufferedInputStream, CheckedInputStream, CipherInputStream,
* DataInputStream, DeflaterInputStream, DigestInputStream, InflaterInputStream, LineNumberInputStream,
* ProgressMonitorInputStream, PushbackInputStream ,AudioInputStream, ByteArrayInputStream, FileInputStream,
* ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream
*===============================================================================================================
*@OutputStream
* FileOutputStream, FilterOutputStream,BufferedOutputStream, CheckedOutputStream, CipherOutputStream,
* DataOutputStream, DeflaterOutputStream, DigestOutputStream, InflaterOutputStream, LineNumberOutputStream,
* ProgressMonitorOutputStream, PushbackOutputStream ,AudioOutputStream, ByteArrayOutputStream, FileOutputStream,
* ObjectOutputStream, PipedOutputStream, SequenceOutputStream, StringBufferOutputStream
*
*===============================================================================================================
*
*@Description字符流
*@Writer
* BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter
*===============================================================================================================
*@Reader
*
*BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader
*------------------------------------------------------------------------------------------------------------------
*/
@SuppressWarnings("all")
public class IoUtils {
private static List<File> fs = new ArrayList<File>(0);
private static String content = "", line = System.getProperty("line.separator");// 換行相當於\n
private static List fileList = new ArrayList();
private static File tempFile = null;
private static final long KB = 1024;// KB
private static final long MB = KB * KB;// MB
private static final long GB = KB * MB;// GB
private static final int byt = 2048;
private static File f = new File("");
private static BASE64Encoder encoder = new BASE64Encoder();// 加密
private static BASE64Decoder decoder = new BASE64Decoder();// 解密
private static String webRootPath;
private static String rootClassPath;
/**
* 讀文件流
*
* @param formPath從哪裏讀取的文件路徑
* @return
*/
public static String reader(String formPath) {
FileReader read = null;
BufferedReader reader = null;
try {
read = new FileReader(new File(formPath));
reader = new BufferedReader(read);
StringBuffer buffer = new StringBuffer("");
content = reader.readLine();
while (content != null) {
buffer.append(content).append(line);
content = reader.readLine();
}
return content = buffer.toString();// 返回
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
if (read != null)
read.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return "";// 沒值就返回空
}
/**
* 寫文件 BufferedWriter BufferedReader
*
* @param formPath
* @param toPath
*/
public static boolean writer(String formPath, String toPath) {
Reader reader = null;
Writer writer = null;
boolean flag = true;
BufferedWriter buffWriter = null;
BufferedReader buffReader = null;
try {
reader = new FileReader(new File(formPath));
writer = new FileWriter(new File(toPath));// writer不能關閉
buffWriter = new BufferedWriter(writer);// 這個寫完可以關閉
buffReader = new BufferedReader(reader);
content = buffReader.readLine();
while (content != null) {
buffWriter.write(line + content);
content = buffReader.readLine();
buffWriter.flush();// 只要用到緩衝區就flush//其實關閉了緩衝區,就是關閉緩衝區中流的對象.
// //寫一次flush是爲了防止停電就掛了~
}
reader.close();
buffReader.close();
buffWriter.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return flag;
}
/**
* 文件處理 Writer OutputStreamWriter
*
* @param content
* OutputStreamWriter
* @param toPath寫到那個路徑下
* @return
*/
public static boolean Writer(String content, String toPath) {
boolean flag = true;
try {
Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(toPath), "utf-8"));// 設置編碼
out.write(line + content);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return flag;
}
/**
* 處理文件大小
*/
public static String fileSize(long file) {
if (file <= 0) {
return "";
} else if (file < MB) {
BigDecimal b = new BigDecimal((double) file / KB);
return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "K";
} else if (file < GB) {
BigDecimal b = new BigDecimal((double) file / MB);
return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "M";
} else {
BigDecimal b = new BigDecimal((double) file / GB);
return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "G";
}
}
/**
* 刪除目錄下的所有子目錄和文件
*
* @param filepath
* @throws IOException
*/
protected static void deleteDirs(String filepath) throws IOException {
File f = new File(filepath);// 定義文件路徑
if (f.exists()) {// 判斷是否存在
if (f.isDirectory()) {// 判斷是文件還是目錄
if (f.listFiles().length == 0) {// 若目錄下沒有文件則直接刪除
f.delete();// 刪除目錄
} else {// 若有則把文件放進數組,並判斷是否有下級目錄
File delFile[] = f.listFiles();
int i = f.listFiles().length;
for (int j = 0; j < i; j++) {
if (delFile[j].isDirectory()) {
deleteDirs(delFile[j].getAbsolutePath());// 遞歸調用delDirs方法並取得子目錄路徑
} else {
delFile[j].delete();// 刪除文件
}
}
f.delete();// 刪除目錄
}
} else {
f.delete();// 刪除文件
}
}
}
/**
* 直接過濾圖片所有格式的 重命名文件裏面的名稱
*
* @param fileDir
* @param sequenceCode
*/
public static void renameFile(File fileDir, String sequenceCode) {
File[] files = fileDir.listFiles();
for (int k = 0; k < files.length; k++) {
StringBuffer sb = new StringBuffer(sequenceCode);
if (files[k].isDirectory()) {
renameFile(files[k], sequenceCode);
} else {
if (k < 10)
sb.append("_000").append(k);
else if (k >= 10 && k < 100)
sb.append("_00").append(k);
else if (k < 1000 && k >= 100)
sb.append("_0").append(k);
else
sb.append("_").append(k);
final int index = files[k].getName().lastIndexOf(".") + 1;
final String fileType = files[k].getName().substring(index);
sb.append(".").append(fileType);
final String name = sb.toString();
final File dirFile = new File(fileDir, name);
System.out.println("Rename File Path:" + files[k].getAbsolutePath());
files[k].renameTo(dirFile);
}
}
}
/**
* 讀取文件裏面的所有文件
*
* @param filePath
*/
public static void readFileList(String filePath) {
File srcFile = new File(filePath);
boolean flag = srcFile.exists();
/**
* @flag判斷文件是否存在
* @isDirectory測試此抽象路徑名錶示的文件是否是一個目錄
* @canWrite測試應用程序是否可以讀取此抽象路徑名錶示的文件
*/
if (!flag || !srcFile.isDirectory() || !srcFile.canRead()) {
try {
srcFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
File[] file = srcFile.listFiles();
for (int i = 0; i < file.length; i++) {
System.out.println(file[i].getAbsolutePath());
}
}
}
/**
* 以數組形式列出所有文件包括子類的文件
*
* @param path
*/
public static List findFiles(String path) {
try {
File file = new File(path);
File[] files = file.listFiles();
String[] filenames = file.list();
if (filenames == null)
return fileList;
for (int i = 0; i < filenames.length; i++) {
if (files[i].isFile()) {
fileList.add(files[i].getPath());
} else if (files[i].isDirectory()) {
findFiles(files[i].getPath());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fileList;
}
/**
* 壓縮文件
*
* @param path壓縮文件的路徑
* @param toDir指定壓縮文件到那個目錄磁盤下
*/
public static void compressionZip(String path, String toDir) {
File f = new File(path);
try {
InputStream fis = new FileInputStream(f);
InputStream bis = new BufferedInputStream(fis);
byte[] buf = new byte[1024 * 1024];
int index = f.getName().lastIndexOf("\\") + 1;
String fileName = f.getName().substring(index);
if (fileName.matches(".*\\.(?i)gif")) { // 匹配gif格式文件
fileName = fileName.replaceAll(".gif", "");
} else if (fileName.matches(".*\\.(?i)java")) {// 匹配.java文件
fileName = fileName.replaceAll(".java", "");
}
int len;
OutputStream fos = new FileOutputStream(toDir + fileName + ".zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);// 壓縮包
ZipEntry ze = new ZipEntry(f.getName());// 這是壓縮包名裏的文件名
zos.putNextEntry(ze);// 寫入新的 ZIP 文件條目並將流定位到條目數據的開始處
while ((len = bis.read(buf)) != -1) {
zos.write(buf, 0, len);
zos.flush();
}
bis.close();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解壓zip文件
*
* @param path解壓文件路徑
* @param flag標識
*/
public static void decompressZip(String path, boolean flag) {
int count = -1;
int index = -1;
String savePath = "";
savePath = path.substring(0, path.lastIndexOf("\\")) + "\\";
try {
BufferedOutputStream bos = null;
ZipEntry entry = null;
InputStream fis = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(fis));
while ((entry = zis.getNextEntry()) != null) {
byte data[] = new byte[byt];
String temp = entry.getName();
flag = fileFilter(temp);
if (!flag)
continue;
index = temp.lastIndexOf("/");
if (index > -1)
temp = temp.substring(index + 1);
temp = savePath + temp;
File f = new File(temp);
if (!f.exists()) {// 不存在就創建一個新的文件
f.createNewFile();
// f.mkdirs();
}
OutputStream fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos, byt);
while ((count = zis.read(data, 0, byt)) != -1) {
bos.write(data, 0, count);
}
bos.flush();// flush緩衝區
fos.flush();
bos.close();
fos.close();
fis.close();
}
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param zip壓縮文件文件路徑
* @param zipName壓縮文件裏面的文件名稱
* @param srcFiles多文件
* @throws IOException
*/
public static void ZipFiles(File zip, String zipName, File... srcFiles)
throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
ZipFiles(out, zipName, srcFiles);
out.close();
System.out.println("*****************壓縮完畢*******************");
}
/**
* 壓縮文件-File
*
* @param zipFile
* zip文件
* @param srcFiles
* 被壓縮源文件
* @author isea533
*/
public static void ZipFiles(ZipOutputStream zis, String path,
File... srcFiles) {
path = path.replaceAll("\\*", "/");
if (!path.endsWith("/")) {
path += "/";
}
byte[] buf = new byte[1024];
try {
for (int i = 0; i < srcFiles.length; i++) {
if (srcFiles[i].isDirectory()) {
File[] files = srcFiles[i].listFiles();
String srcPath = srcFiles[i].getName();
srcPath = srcPath.replaceAll("\\*", "/");
if (!srcPath.endsWith("/")) {
srcPath += "/";
}
zis.putNextEntry(new ZipEntry(path + srcPath));
ZipFiles(zis, path + srcPath, files);
} else {
FileInputStream in = new FileInputStream(srcFiles[i]);
System.out.println(path + srcFiles[i].getName());
zis
.putNextEntry(new ZipEntry(path
+ srcFiles[i].getName()));
int len;
while ((len = in.read(buf)) > 0) {
zis.write(buf, 0, len);
}
zis.closeEntry();
in.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param zipPath
* @param descDir
* @throws IOException
*/
public static void unZipFiles(String zipPath, String descDir)
throws IOException {
unZipFiles(new File(zipPath), descDir);
}
/**
* 解壓文件到指定目錄
*
* @param zipFile
* @param descDir
* @author isea533
*/
public static void unZipFiles(File zipFile, String descDir)
throws IOException {
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (descDir + zipEntryName).replaceAll("\\*", "/");
// 判斷路徑是否存在,不存在則創建文件路徑
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
}
// 判斷文件全路徑是否爲文件夾,如果是上面已經上傳,不需要解壓
if (new File(outPath).isDirectory()) {
continue;
}
// 輸出文件路徑信息
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
in.close();
out.close();
}
System.out.println("******************解壓完畢********************");
}
/**
* 以數組形式存放圖片,只拿jpg格式的
*
* @param dirName
* @return
*/
public static List readDirFiles(String dirName) {// dirName目錄全路徑
try {
File file = new File(dirName);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File tempFile = files[i];
if (tempFile.isDirectory()) {
String path = tempFile.getPath();
List list = readDirFiles(path);// 遞歸
for (int j = 0; j < list.size(); j++) {
fileList.add(list.get(j));
}
} else {
String fileName = tempFile.getName();
if (!tempFile.isFile()) {
continue;
}
// 判斷是jpg格式的就添加進去
if (files[i].getName().endsWith(".gif")) {
fileList.add(tempFile);
}
}
if (i == (files.length - 1)) {
return fileList;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fileList;
}
/**
* 創建文件並且拷貝文件
*
* @param dirFrom從那個文件裏面拷貝
* @param dirTo拷貝到那個文件裏面
*/
public static void copyFiles(File dirFrom, File dirTo) {
File[] files = dirFrom.listFiles();
for (File f : files) {
String tempFrom = f.getAbsolutePath();
String tempTo = tempFrom.replace(dirFrom.getAbsolutePath(), dirTo
.getAbsolutePath()); // 後面的路徑 替換前面的路徑名
if (f.isDirectory()) {
tempFile = new File(tempTo);
if (!tempFile.exists()) {
tempFile.mkdirs();
} else {
copyFiles(dirFrom, dirTo);
}
} else {
System.out.println("源文件:" + f.getAbsolutePath());
int index = tempTo.lastIndexOf("\\");// 找到"/"所在的位置
String mkdirPath = tempTo.substring(0, index);
tempFile = new File(mkdirPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
} else {
System.out.println("目標點:" + tempTo);
readFiles(tempFrom, tempTo);
}
}
}
}
/**
*
* @param from從那個路徑
* @param to到那個路徑
*/
public static void readFiles(String from, String to) {
try {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
byte[] buff = new byte[1024 * 1024];
int len = 0;
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* xml格式化
*
* @param document
* @param filePath
* @return
*/
/**
* 過濾器
*/
private static FileFilter fileFilter = new FileFilter() {
public boolean accept(File fileName) {
String f = fileName.getName();
if (f.endsWith(".png") || f.endsWith(".gif")) {
return true;
}
return false;
}
};
/**
* 文件過濾
*
* @param filName
* @return
*/
public static boolean fileFilter(String filName) {
if (filName.endsWith(".jpg") || filName.endsWith(".gif")
|| filName.endsWith(".bmp") || filName.endsWith(".png")) {
return false;
} else {
return true;
}
}
/***
* ==============================================路徑的封裝begin================
* 獲取當前工程路徑
*
* @return
*/
public static String getSysPath() {
// String path =
// Thread.currentThread().getContextClassLoader().getResource("").toString();
String path = Thread.currentThread().getContextClassLoader()
.getResource(".").toString();
String temp = path.replaceFirst("file:/", "").replaceFirst(
"WEB-INF/classes/", "");
String separator = System.getProperty("file.separator");
String resultPath = temp.replaceAll("/", separator + separator);
return resultPath;
}
/**
* Thread.currentThread().getContextClassLoader().getResource("")
* 的getResource裏面空串或者點或者是/輸出的路徑是一致 "" D:\Eclipse3.7\JTool\build\classes\ .
* D:\Eclipse3.7\JTool\build\classes\ -/ D:\Eclipse3.7\JTool\build\classes\
*
* @return
*/
public static String getClassPath() {
// String path =
// Thread.currentThread().getContextClassLoader().getResource("").toString();
// String path
// =Thread.currentThread().getContextClassLoader().getResource(".").toString();
String path = Thread.currentThread().getContextClassLoader()
.getResource(".").toString();
String temp = path.replaceFirst("file:/", "");
String separator = System.getProperty("file.separator");
String resultPath = temp.replaceAll("/", separator + separator);
return resultPath;
}
/**
* getClassLoader().getResource()方法參數空串和點都是輸出相同的路徑唯有 /是報空指針 ""
* D:\Eclipse3.7\JTool\build\classes\ . D:\Eclipse3.7\JTool\build\classes\
* -/ 報空指針
*
* @return
*/
private String getClassesAbsolutePath() {
// 得到的是 項目的絕對路徑
String path = this.getClass().getClassLoader().getResource("")
.getPath();
// String
// path=this.getClass().getClassLoader().getResource(".").getPath();
// String
// path=this.getClass().getClassLoader().getResource("/").getPath();//報空指針
String temp = path.replaceFirst("/", "");
String separator = System.getProperty("file.separator");
String resultPath = temp.replaceAll("/", separator + separator);
return resultPath;
}
/**
*得到的是當前類 文件的URI目錄,不包括自己
* ""D:\Eclipse3.7\JTool\build\classes\javax\org\path\ .
* D:\Eclipse3.7\JTool\build\classes\javax\org\path\ - /
* D:\Eclipse3.7\JTool\build\classes\
*
* @return
*/
private String getCurrentClassPath() {
// String path=this.getClass().getResource("").getPath();
// String path=this.getClass().getResource(".").getPath();
String path = this.getClass().getResource("/").getPath();
String temp = path.replaceFirst("/", "");
String separator = System.getProperty("file.separator");
String resultPath = temp.replaceAll("/", separator + separator);
return resultPath;
}
public static String getPath(Class clazz) {
String path = clazz.getResource("").getPath();
return new File(path).getAbsolutePath();
}
public static String getPath(Object object) {
String path = object.getClass().getResource("").getPath();
return new File(path).getAbsolutePath();
}
public static String getRootClassPath() {
if (rootClassPath == null) {
try {
String path = IoUtils.class.getClassLoader().getResource("").toURI().getPath();
rootClassPath = new File(path).getAbsolutePath();
} catch (Exception e) {
String path = IoUtils.class.getClassLoader().getResource("").getPath();
rootClassPath = new File(path).getAbsolutePath();
}
}
return rootClassPath;
}
public static String getPackagePath(Object object) {
Package p = object.getClass().getPackage();
return p != null ? p.getName().replaceAll("\\.", "/") : "";
}
public static File getFileFromJar(String file) {
throw new RuntimeException("Not finish. Do not use this method.");
}
public static String getWebRootPath() {
if (webRootPath == null)
webRootPath = detectWebRootPath();
return webRootPath;
}
public static void setWebRootPath(String webRootPath) {
if (webRootPath == null)
return;
if (webRootPath.endsWith(File.separator))
webRootPath = webRootPath.substring(0, webRootPath.length() - 1);
IoUtils.webRootPath = webRootPath;
}
private static String detectWebRootPath() {
try {
String path = IoUtils.class.getResource("/").toURI().getPath();
return new File(path).getParentFile().getParentFile()
.getCanonicalPath();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* =================================加密文件=====begin======================
* @param f
* @param path
*/
private static String encryptFile(File f, String path) {
InputStream in = null;
OutputStream out = null;
String key = "";
try {
f = new File(path);
in = new FileInputStream(f);
out = new ByteArrayOutputStream();
// System.out.println(f.getAbsolutePath());
// System.out.println(f.length());
encoder.encodeBuffer(in, out);
key = out.toString();
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return key;
}
/**
*解密
*
* @param f
* @param path
*/
private static String decryptFile(File f, String path) {
InputStream in = null;
OutputStream out = null;
String key = "";
try {
f = new File(path);
in = new FileInputStream(f);
out = new ByteArrayOutputStream();
decoder.decodeBuffer(in, out);
key = out.toString();
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return key;
}
/**
* 加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(String inputStr) {
String value = "";
try {
byte[] key = inputStr.getBytes();
value = encoder.encodeBuffer(key);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 解密
*
* @param key
* @return
* @throws Exception
*/
public static String decryptBASE64(String outputStr) {
String value = "";
try {
byte[] key = decoder.decodeBuffer(outputStr);
value = new String(key);
} catch (Exception e) {
}
return value;
}
/**
* 遞歸文件
*
* @param file
* @return
*/
public static List<File> getFiles(File dir) {
if (dir == null) {
return null;
}
if (!dir.isDirectory()) {
return null;
}
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
fs.add(file);
} else if (file.isDirectory()) {
getFiles(file); // 如果遇到目錄則通過遞歸調用繼續統計
}
}
return fs;
}
/**
* ================================end================================
*/
public static void main(String[] args) throws Exception {
String path1 = "D:/data";
List<File> files=getFiles(new File(path1));
for(File f:files){
System.out.println(f.getCanonicalPath());
}
/**
* 壓縮文件
*/
/*
* File[] files = new File[]{new File("d:/t/a.txt"),new
* File("d:/t/a.xls")}; File zip = new File("d:/t/壓縮.zip");
* ZipFiles(zip,"ZipFiles",files);
*//**
* 解壓文件
*/
/*
* File zipFile = new File("d:/t/壓縮.zip"); String path = "d:/";
* //unZipFiles(zipFile, path);
*/
// String separator=f.separator;//與系統有關的默認名稱分隔符,爲了方便,它被表示爲一個字符串
// String path = "D:/test/a.txt";
// String s = reader(path);
// System.out.println(s);
}
}
"; FileUitls.renameFile(new File(path), "黑絲磨毛雙面印花"); } }