📄 toolskit.java
字号:
package com.main.apps.common;
import java.io.BufferedReader;
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.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.main.apps.control.Config;
public class ToolsKit
{
public static String unzip(String zipFile, String destDir)
{
/* 压缩文件名为空,返回NULL */
if (zipFile == null)
{
return null;
}
File destFile = new File(destDir); // 用来处理文件夹
if (!destFile.exists())
{
destFile.mkdirs(); // 如果目录不存在,创建之
}
String fileName = "";
FileOutputStream fileOut = null; // 用来处理文件
ZipEntry entry = null;
byte[] bytes = new byte[READ_BYTES]; // 缓冲区
int read = 0; // 每次读写的字节数
try
{
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
while ((entry = zin.getNextEntry()) != null)
{
if (entry.isDirectory()) // 目录,需要创建
{
/* 创建目录 */
fileName = destDir + File.separatorChar + entry.getName();
destFile = new File(fileName);
destFile.mkdir();
}
else
// 读写文件
{
fileName = destDir + File.separatorChar + entry.getName();
fileOut = new FileOutputStream(fileName);
while ((read = zin.read(bytes, 0, READ_BYTES)) > 0)
{
fileOut.write(bytes, 0, read);
}
fileOut.close();
}
zin.closeEntry();
}
zin.close();
}
catch (IOException ioe)
{
FileUtil.printError(ioe, "解压文件" + zipFile + "出现异常", Config.errorFile);
}
return destDir;
}
/**
* 拷贝文件并删除源文件
*
* @param srcFileName,
* 源文件名
* @param destFileName,
* 目标文件名
*/
public static void move(String srcFileName, String destFileName)
{
if (srcFileName == null || destFileName == null)
return;
File srcFile = new File(srcFileName);
File destFile = new File(destFileName);
try
{
if (srcFile.exists() && srcFile.canRead()) // 源文件存在并可读
{
FileInputStream in = new FileInputStream(srcFile);
try
{
byte[] bytes = new byte[1024]; // 缓冲区
int read = 0; // 每次读写的字节数
FileOutputStream out = new FileOutputStream(destFile);
try
{
while ((read = in.read(bytes, 0, bytes.length)) != -1)
out.write(bytes, 0, read);
out.flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
out.close();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
in.close();
srcFile.delete();
}
}
}
catch (FileNotFoundException fnfe)
{
FileUtil.printError(fnfe, "找不到文件", Config.errorFile);
}
catch (Exception ioe)
{
FileUtil.printError(ioe, "读写文件出错", Config.errorFile);
}
}
/**
* 处理wml中的文件路径
*
* @param wmlName,
* wml文件名
* @param dir,
* 根路径
*/
public static void handleWml(String wmlName, String url)
{
StringBuffer buffer = new StringBuffer();
String lineIn = "";
int index = 0;
try
{
// 先把文本读到字符缓冲里
BufferedReader in = new BufferedReader(new FileReader(wmlName));
while ((lineIn = in.readLine()) != null)
{
index = lineIn.indexOf("<img");
if (index != -1) // 是图片标签了,要处理
{
index = lineIn.indexOf("src", index); // 找到资源属性
if (index != -1)
{
int firstIndex = lineIn.indexOf("\"", index) + 1;
int lastIndex = lineIn.indexOf("\"", firstIndex);
String fileName = lineIn.substring(firstIndex, lastIndex);
if (fileName == null || fileName.equals(""))
{
// do nothing
}
else
// 得到图片文件名
{
//
index = fileName.lastIndexOf('/');
if (index != -1)
{
fileName = fileName.substring(index + 1);
}
StringBuffer sb = new StringBuffer();
sb.append(url);
sb.append(File.separatorChar);
sb.append(fileName);
fileName = sb.toString();
sb.setLength(0);
sb.append(lineIn);
sb.replace(firstIndex, lastIndex, fileName);
lineIn = sb.toString();
}
}
}
buffer.append(lineIn);
buffer.append("\r\n");
}
in.close();
// 写出文件
FileWriter out = new FileWriter(wmlName, false);
out.write(buffer.toString());
out.close();
}
catch (IOException ioe)
{
FileUtil.printError(ioe, "读写文件出错", Config.errorFile);
}
}
public static void main(String[] args)
{
ToolsKit.handleWml("E:/xx.wml", "E:/test/xml/sss");
}
private static final int READ_BYTES = 1024;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -