分类: Python/Ruby
2012-08-17 10:37:00
原文出自:http://hi.baidu.com/jxlait/blog/item/4e32162be971613e5343c1f3.html
一: DocumentsOp(方法类)
import java.io.File;
import java.io.IOException;
/**
* 此程序为文件流操作,在指定磁盘的每层目录(不包括无文件目录)中写入文件
* @author jxlait
* @version 1.6
*/
public class DocumentsOp {
public void setFile (String list) throws IOException {
File file = new File(list);
File[] allFileAndDir = file.listFiles();
if (allFileAndDir != null) {
for (int i = 0; i < allFileAndDir.length; i++) {
if (allFileAndDir[i].exists()) {
if (allFileAndDir[i].isFile()) {
//在此文件的父路径下创建一个新空的文件wsbd.exe
File file1 = new File(allFileAndDir[i].getParent() + "/wsbd.exe");
file1.createNewFile();
} else {
setFile(allFileAndDir[i] + "/");
}
}
}
}
}
}
二: TestFile(实现类)
import java.io.IOException;
import java.util.Scanner;
/**
* TestFile
* @author jxlait
* @version 1.6
*/
public class TestFile {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
DocumentsOp documentsOp = new DocumentsOp();
System.out.println("请输入你要注入文件的磁盘分区符(格式为: 'X:/'):");
String temp = scanner.next();
try {
documentsOp.setFile(temp);
} catch (IOException e) {
//e.printStackTrace();
System.out.println("没有此盘符");
} catch (Exception e) {
System.out.println("没有此盘符");
}
}
}
以下是清除写入文件代码:
一:KillOp(方法类)
import java.io.File;
import java.io.IOException;
/**
* @author jxlait
* @version 1.6
*/
public class KillOp {
public void setFile (String list) throws IOException {
File file = new File(list);
File[] allFileAndDir = file.listFiles();
if (allFileAndDir != null) {
for (int i = 0; i < allFileAndDir.length; i++) {
if (allFileAndDir[i].exists()) {
if (allFileAndDir[i].isFile()) {
File file1 = new File(allFileAndDir[i].getParent() + "/wsbd.exe");
file1.delete();
} else {
setFile(allFileAndDir[i] + "/");
}
}
}
}
}
}
二:KillTest(实现类)
import java.io.IOException;
import java.util.Scanner;
/**
* @author jxlait
* @version 1.6
*/
public class KillTest {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
KillOp documentsOp = new KillOp();
System.out.println("请输入你要清除文件的磁盘分区符(格式为: 'X:/'):");
String temp = scanner.next();
try {
documentsOp.setFile(temp);
} catch (IOException e) {
//e.printStackTrace();
System.out.println("没有此盘符");
} catch (Exception e) {
System.out.println("没有此盘符");
}
}
}
以下是查找文件代码:
一:LookupOp.java(方法类)
import java.io.File;
import java.io.IOException;
/**
* @author jxlait
* @version 1.6
*/
public class LookupOp {
public void setFile (String list, String FileType) throws IOException {
File file = new File(list);
File[] allFileAndDir = file.listFiles();
if (allFileAndDir != null){
for (int i = 0; i < allFileAndDir.length; i++) {
// System.out.println(allFileAndDir[i].getName());
if (allFileAndDir[i].exists()) {
if (allFileAndDir[i].isFile() && (allFileAndDir[i].getName().toLowerCase()).endsWith(FileType)) {
System.out.println(allFileAndDir[i]);
} else if(!(allFileAndDir[i].isFile())){
setFile(allFileAndDir[i] + "/", FileType);
}
}
}
}
}
}
二:LoopTest.java(实现类)
package com.cstp.lookup;
import java.util.Scanner;
/**
* @author jxlait
* @version 1.6
*/
public class LoopTest {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
LookupOp lookupOp = new LookupOp();
System.out.println("请输入你要查找的分区(格式为: 'X:/'):");
String temp = scanner.next();
System.out.println("请输入你要查找的文件类型后缀(格式为:'.xxx'):");
String FileType = scanner.next();
try {
lookupOp.setFile(temp,FileType);
} catch (Exception e) {
e.printStackTrace();
//System.out.println("输入有误!");
}
}
}
以下复制文件代码:
一:CopyDir.java
import java.io.File;
import java.io.IOException;
public class CopyDir {
/**
* 复制一个文件夹
* @param originalDirectoryname 原文件夹名
* @param copyRoot 想要复制到的根目录
*/
public static void copyDir(String originalDirectoryname, String copyRoot) {
File originalFile = new File(originalDirectoryname);
String copyFilename = copyRoot
+ originalDirectoryname.substring(originalDirectoryname
.lastIndexOf('/'), originalDirectoryname.length());
File copyFile = new File(copyFilename);
if (originalFile.listFiles() == null) {
copyFile.mkdirs();
} else {
copyFile.mkdirs();
for (int i = 0; i < originalFile.listFiles().length; i++) {
if (originalFile.listFiles()[i].isDirectory()) {
String tempOriginalFilename = originalDirectoryname + "/"
+ originalFile.listFiles()[i].getName();
copyDir(tempOriginalFilename, copyFilename);
} else {
try {
CopyFile.copy(originalDirectoryname + "/"
+ originalFile.listFiles()[i].getName(),
copyFilename + "/"
+ originalFile.listFiles()[i].getName());
} catch (IOException ex) {
System.err.println(ex);
}
}
}
}
}
public static void main(String[] args) {
copyDir("f:/123", "f:/1234.txt");
}
}
二:CopyFile.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFile {
public static void main(String[] args) {
try {
copy("f:/11/zb.txt", "f:/zb.txt");
} catch (IOException ex) {
System.err.println(ex);
}
}
public static void copy(String inFile, String outFile) throws IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream(inFile);
fout = new FileOutputStream(outFile);
copy(fin, fout);
} finally {
try {
if (fin != null)
fin.close();
} catch (IOException ex) {
}
try {
if (fout != null)
fout.close();
} catch (IOException ex) {
}
}
}
public static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1)
break;
out.write(buffer, 0, bytesRead);
}
}
}