谁有空帮我写一个文件操作的小程序吧 http://www.csdn.net/develop/read_article.asp?id=19064 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package mytool;import java.io.*;import java.util.*;public class FileOperator { public FileOperator() {} /** * Function: move file(only move file) * parameter: strSourceFileName:specified completed file path * strDestDir: move to specified folder * return: success true; faild false */ public static boolean copyTo(String strSourceFileName, String strDestDir) { File fileSource = new File(strSourceFileName); File fileDest = new File(strDestDir); //if the source file don't exist or it is folder if (!fileSource.exists() || fileSource.isDirectory()) { System.out.println("error: FileOperator.java copyTo function,\nreasons: source file[" + strSourceFileName + "],don't exist or it is folder!"); return false; } // if target folder dosn't exist if (!fileDest.isDirectory() || !fileDest.exists()) { if (!fileDest.mkdirs()) { System.out.println( "error: FileOperator.java copyTo function,\nreasons:target" + " folder dosn't exit, the operation faild when create target folder!"); return false; } } try { String strAbsFilename = strDestDir + File.separator + fileSource.getName(); FileInputStream fileInput = new FileInputStream(strSourceFileName); FileOutputStream fileOutput = new FileOutputStream(strAbsFilename); //System.out.println("begin copy file:"); int i = 0; int count = -1; long nWriteSize = 0; long nFileSize = fileSource.length(); byte[] data = new byte[BUFFER]; //System.out.println("FILE NAME \t\t FILE SIZE"); System.out.println(fileSource.getName() + "\t\t" + nFileSize + " byte"); while ( -1 != (count = fileInput.read(data, 0, BUFFER))) { fileOutput.write(data, 0, count); nWriteSize += count; long size = (nWriteSize * 100) / nFileSize; long t = nWriteSize; String msg = null; if (size <= 100 && size >= 0) { msg = "\rcopy process: " + size + "% \t" + "\t copied: " + t; System.out.print(msg); } else if (size > 100) { msg = "\rcopy process: " + 100 + "% \t" + "\t copied: " + t; System.out.print(msg); } } fileInput.close(); fileOutput.close(); System.out.println("\ncopy success!"); return true; } catch (Exception e) { System.out.println("exception message["); e.printStackTrace(); System.out.println("exception message]"); return false; } } /** * functin: delete specified file * parameter: completed file path's file name strFileName * retrun: delete success true faild false; */ public static boolean delete(String strFileName) { File fileDelete = new File(strFileName); if (!fileDelete.exists() || fileDelete.isFile()) { System.out.println("error: " + strFileName + "unexist!"); return false; } return fileDelete.delete(); } /** * function: move file(only move file) * parameter: strSourceFileName: specified completed file path name * strDestDir: target folder * return : success true; faild false */ public static boolean moveFile(String strSourceFileName, String strDestDir) { if (copyTo(strSourceFileName, strDestDir)) { return FileOperator.delete(strSourceFileName); } else { return false; } } /** * function: create folder * parameter: strDir folder name of by created * return : success true; fail false */ public static boolean makedir(String strDir) { File fileNew = new File(strDir); if (!fileNew.exists()) { return fileNew.mkdirs(); } else { return true; } } /** * function: delete folder * parameter: strDir deleted folder name * return: success true; fail false */ public static boolean rmdir(String strDir) { File rmDir = new File(strDir); if (rmDir.isDirectory() && rmDir.exists()) { String[] fileList = rmDir.list(); int size = fileList.length; for (int i = 0; i < size; i++) { String subFile = strDir + File.separator + fileList[i]; File tmp = new File(subFile); if (tmp.isFile()) { tmp.delete(); } else if (tmp.isDirectory()) { rmdir(subFile); } else { System.out.println("error!"); } } rmDir.delete(); } else { return false; } return true; } // member variable private static final int BUFFER = 1024; // member variable} import java.io.*;public class FindCheckFile{ public static void main(String[] args) { if(args.length==0) args=new String[]{"c:\\"}; try { File pathName=new File(args[0]); String[] fileNames=pathName.list(); //enumerate all files in the directory for (int i=0;i<fileNames.length;i++) { File f=new File(pathName.getPath(),fileNames[i]); if(f.getCanonicalPath().endsWith("system32\\drivers\\etc\\hosts")) { System.out.println("找到"+f.getCanonicalPath()); try { String s=null; boolean flg=false; BufferedReader br = new BufferedReader(new FileReader(f.getCanonicalPath())); s = br.readLine(); while(s != null) { if(s.indexOf("127.0.0.1 flame")!=-1) { flg=true; } s = br.readLine(); } br.close(); if(!flg) { PrintWriter out=new PrintWriter(new FileWriter(f.getCanonicalPath())); BufferedReader in=new BufferedReader(new FileReader(f.getCanonicalPath())); StringBuffer bf=new StringBuffer(); s=in.readLine(); while(s!=null) { bf.append(s); s=br.readLine(); } in.close(); bf.append("127.0.0.1 flame"); out.print(new String(bf)); out.close(); } } catch(IOException e){} System.exit(0); } //if the file is again directory,call the main method recursively if(f.isDirectory()) { main(new String[]{f.getPath()}); } } } catch(IOException e) { e.printStackTrace(); } }} 效率与简洁书写之争,求解释 swing事件的处理 StreamTokenizer 输入字符串问题 新手:关于JDK运行问题 IO 流问题!!! thinking in java中的一句话,不是很明白。 怎样获得本周的日期是几号到几号? helloworld问题,搞不定:package system does not exist 一个关于jdbc的问题!急用! 代码中的细节问题 两个类调用出现问题,请指教 Ascii码转换,字符转换,怎么把汉字转换成Ascii码?
import java.util.*;public class FileOperator {
public FileOperator() {} /**
* Function: move file(only move file)
* parameter: strSourceFileName:specified completed file path
* strDestDir: move to specified folder
* return: success true; faild false
*/
public static boolean copyTo(String strSourceFileName,
String strDestDir) {
File fileSource = new File(strSourceFileName);
File fileDest = new File(strDestDir); //if the source file don't exist or it is folder
if (!fileSource.exists() || fileSource.isDirectory()) {
System.out.println("error: FileOperator.java copyTo function,\nreasons: source file[" +
strSourceFileName + "],don't exist or it is folder!");
return false;
} // if target folder dosn't exist
if (!fileDest.isDirectory() || !fileDest.exists()) {
if (!fileDest.mkdirs()) {
System.out.println(
"error: FileOperator.java copyTo function,\nreasons:target"
+ " folder dosn't exit, the operation faild when create target folder!");
return false;
}
} try {
String strAbsFilename = strDestDir + File.separator +
fileSource.getName(); FileInputStream fileInput = new FileInputStream(strSourceFileName);
FileOutputStream fileOutput = new FileOutputStream(strAbsFilename); //System.out.println("begin copy file:"); int i = 0;
int count = -1; long nWriteSize = 0;
long nFileSize = fileSource.length(); byte[] data = new byte[BUFFER]; //System.out.println("FILE NAME \t\t FILE SIZE");
System.out.println(fileSource.getName() + "\t\t" + nFileSize +
" byte"); while ( -1 != (count = fileInput.read(data, 0, BUFFER))) { fileOutput.write(data, 0, count); nWriteSize += count; long size = (nWriteSize * 100) / nFileSize;
long t = nWriteSize; String msg = null; if (size <= 100 && size >= 0) {
msg = "\rcopy process: " + size + "% \t" + "\t copied: " + t;
System.out.print(msg);
} else if (size > 100) {
msg = "\rcopy process: " + 100 + "% \t" + "\t copied: " + t;
System.out.print(msg);
} } fileInput.close();
fileOutput.close(); System.out.println("\ncopy success!");
return true; } catch (Exception e) {
System.out.println("exception message[");
e.printStackTrace();
System.out.println("exception message]");
return false;
}
} /**
* functin: delete specified file
* parameter: completed file path's file name strFileName
* retrun: delete success true faild false;
*/
public static boolean delete(String strFileName) {
File fileDelete = new File(strFileName); if (!fileDelete.exists() || fileDelete.isFile()) {
System.out.println("error: " + strFileName + "unexist!");
return false;
} return fileDelete.delete();
} /**
* function: move file(only move file)
* parameter: strSourceFileName: specified completed file path name
* strDestDir: target folder
* return : success true; faild false
*/
public static boolean moveFile(String strSourceFileName,
String strDestDir) {
if (copyTo(strSourceFileName, strDestDir)) {
return FileOperator.delete(strSourceFileName);
} else {
return false;
}
} /**
* function: create folder
* parameter: strDir folder name of by created
* return : success true; fail false
*/
public static boolean makedir(String strDir) {
File fileNew = new File(strDir); if (!fileNew.exists()) {
return fileNew.mkdirs();
} else {
return true;
}
} /**
* function: delete folder
* parameter: strDir deleted folder name
* return: success true; fail false
*/
public static boolean rmdir(String strDir) {
File rmDir = new File(strDir);
if (rmDir.isDirectory() && rmDir.exists()) {
String[] fileList = rmDir.list(); int size = fileList.length;
for (int i = 0; i < size; i++) {
String subFile = strDir + File.separator + fileList[i];
File tmp = new File(subFile);
if (tmp.isFile()) {
tmp.delete();
} else if (tmp.isDirectory()) {
rmdir(subFile);
} else {
System.out.println("error!");
}
}
rmDir.delete();
} else {
return false;
}
return true;
} // member variable private static final int BUFFER = 1024; // member variable
}
{
public static void main(String[] args)
{
if(args.length==0) args=new String[]{"c:\\"};
try
{
File pathName=new File(args[0]);
String[] fileNames=pathName.list();
//enumerate all files in the directory
for (int i=0;i<fileNames.length;i++)
{
File f=new File(pathName.getPath(),fileNames[i]);
if(f.getCanonicalPath().endsWith("system32\\drivers\\etc\\hosts"))
{
System.out.println("找到"+f.getCanonicalPath());
try
{
String s=null;
boolean flg=false;
BufferedReader br = new BufferedReader(new FileReader(f.getCanonicalPath()));
s = br.readLine();
while(s != null)
{
if(s.indexOf("127.0.0.1 flame")!=-1)
{
flg=true;
}
s = br.readLine();
}
br.close(); if(!flg)
{
PrintWriter out=new PrintWriter(new FileWriter(f.getCanonicalPath()));
BufferedReader in=new BufferedReader(new FileReader(f.getCanonicalPath()));
StringBuffer bf=new StringBuffer();
s=in.readLine();
while(s!=null)
{
bf.append(s);
s=br.readLine();
}
in.close();
bf.append("127.0.0.1 flame");
out.print(new String(bf));
out.close();
}
}
catch(IOException e){}
System.exit(0);
}
//if the file is again directory,call the main method recursively
if(f.isDirectory())
{
main(new String[]{f.getPath()});
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}