JAVA提取不同磁盘下文件的问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 07:29:44
就是我要提取2个不同磁盘下的2个TXT文档进行比较,让其自动搜索出我想要的文件名(该文件可能在100个目录下!)
现在发出我的代码,大家帮我看看应该怎么改?
package bijiao;

import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.charset.Charset;

public class compare
{

private String txtfile1 = "D:/1.txt";
private String txtfile2 = "D:/2.txt";

private List<String> list1 = new ArrayList<String>();
private List<String> list2 = new ArrayList<String>();

public compare()
{
try
{
run();
}
catch (Exception e)
{
e.printStackTrace();
}
}

private void run() throws Exception
{

BufferedReader in1 = new BufferedReader(new FileReader(txtfile1));
BufferedReader in2 = new BufferedReader(new FileReader(txtfile2));
String str1 = "", str2 = "";
int i = 0;

以下是找到C盘下所有"1.txt"文件的demo,希望对你有用

import java.io.File;
public class a{
public static void main(String[] args) {
File cRoot=new File("c:\\");
String []res=getFileByName(cRoot,"1.txt");
if(res==null) return;
for(int i=0;i<res.length;i++)
System.out.println(res[i]);
}
public static String[] getFileByName(File f,String name){
String res[]=null;
File []son=f.listFiles();
String tmpRes[]=null;
String sonRes[]=null;
String findedFileName=null;
for(int i=0;i<son.length;i++){
if(son[i].isFile()&&son[i].getName().equals(name)){
if(res==null){
res=new String[1];
res[0]=son[i].getAbsolutePath();
}else{
tmpRes=new String[res.length+1];
for(int j=0;j<res.length;j++)
tmpRes[j]=res[j];
tmpRes[res.length]=son[i].getAbsolutePath();
}
}