📄 filetool.java
字号:
package net.meybo.util;
import java.io.*;
import java.util.*;
/**
* 主要是对文件的基本操作
* <p>Title: </p>
* <p>Description: file类操作</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: 脉博软件</p>
* @author 阚吉彬
* @version 1.0
*/
public class FileTool {
public FileTool() {
}
/**
* 读文件
* @param fileName:文件名,可以带路径
* @return
*/
public static String read(String fileName) {
File file=new File(fileName);
FileInputStream in=null;
String fileContent="";
try{
int size=(int)file.length();
in=new FileInputStream(file);
byte[] b=new byte[size];
in.read(b);
fileContent=new String(new String(b,"GB2312").getBytes("GB2312"));
// System.out.println("file"+fileContent);
}catch(Exception e){
fileContent = "";
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
}
catch (IOException ex) {
}
}
}
return fileContent;
}
/**
* 写文件
* @param fileName:文件各,可以带路径
* @param value:文件内容
*/
public static void writeFile(String fileName, String value){
FileOutputStream outfile = null;
OutputStreamWriter out=null;
try{
outfile = new FileOutputStream(fileName);
out=new OutputStreamWriter(outfile);
out.write(value);
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(out != null){
out.close();
}
} catch (IOException ex1) {
}
if(outfile != null){
try {
outfile.close();
}
catch (IOException ex) {
}
}
}
}
/**
* 生成配置文件
* @param cfgHT:新的配置文件内容
*/
public static void writeConfig(Hashtable cfgHT){
StringBuffer fileContentSB = new StringBuffer();
Enumeration cfgEN = cfgHT.keys();
while (cfgEN.hasMoreElements()) {
String key = (String)cfgEN.nextElement();
fileContentSB.append(key+"="+cfgHT.get(key)+"\r\n");
}
writeFile("xmlconfig.ini", fileContentSB.toString());
}
/**
* 记录日志
* @param content:日志内容
*/
public static void writeLog(String content){
StringBuffer tmpSB = new StringBuffer();
tmpSB.append(read("log/log.txt"));//读取原来
tmpSB.append("\r\n"+content);
writeFile("log/log.txt", tmpSB.toString());
}
public static boolean MoveFile(String SourcePath, String targetPath) {
boolean MoveFile = false;
String sPath, tPath;
sPath = SourcePath.trim();
tPath = targetPath.trim();
java.io.File f = new java.io.File(sPath);
try {
if (f.exists()) {
java.io.File file = new java.io.File(tPath);
MoveFile = f.renameTo(file);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return MoveFile;
}
public static boolean delDir(String dirPath)
{
File f=new File(dirPath);
if(f.exists())
{
if(f.isDirectory())
{
String[] dirs=f.list();
if(dirs!=null){
for(int i=0;i<dirs.length;i++)
{
File f1=new File(dirPath+"/"+dirs[i]);
if(f1.isDirectory()) delDir(f1.getAbsolutePath());
else
{
f1.delete();
}
}
}
}
f.delete();
return true;
}
else
return false;
}
public static void main(String[] args) {
// FileTool untitled11 = new FileTool();
// Hashtable cfgHT = new Hashtable();
// cfgHT.put("aaa", "12345");
// cfgHT.put("vvv", "3r464");
// writeConfig(cfgHT);
// FileTool.read("xmlconfig.ini");
// FileTool.writeFile("xmlconfig.ini","");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -