public static char[] readChars(String pathFileName) throws FileNotFoundException,IOException{ FileInputStream fin = new FileInputStream(pathFileName); int len = fin.available(); char[] data = new char[len]; InputStreamReader reader = new InputStreamReader(fin); reader.read(data); return data; }
那是csdn个朋友给面子啊中午我来研究一下这个正则表达式怎么写,吃饭了先
buyaowen(外包需要掌握三点) ( ) 信誉:100 Blog =========================你多少分了?
List txtFileList = new ArrayList(); public static void main(String[] args){ FilePathRead t = new FilePathRead(); t.find(new File("C:\\c_folder")); t.find(new File("C:\\B_folder")); ...... // 如果是多个可以考虑循环 }
public void find(File f){ if(f.isDirectory()){ File[] fileList = f.listFiles(); for(int i = 0; i < fileList.length; i++){ find(fileList[i]); } }else{ if(matchResult(f.getName())) // 匹配 txtFileList.add(f.getAbsolutePath()); } } public boolean matchResult(String str) { Pattern p = Pattern.compile("^[0-9a-zA-Z]+.[tT][xX][tT]$"); // 匹配txt文件。 Matcher m = p.matcher(str); return m.find(); }
2.将1找出的文件读出来。
如果是我的话,我有个很不成熟的办法.package org.luyang;import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URI;public class FilePathRead {
public static void main(String[] args){
FilePathRead t = new FilePathRead();
t.find(new File("C:\\c_folder"));
t.find(new File("C:\\B_folder"));
...... // 如果是多个可以考虑循环
}
public void find(File f){
if(f.isDirectory()){
File[] fileList = f.listFiles();
for(int i = 0; i < fileList.length; i++){
find(fileList[i]);
}
}else{
if(f.getName().indexOf(".txt") != -1) // 匹配
System.out.println(f.getAbsolutePath());
}
}
以上事例为找出b,c下面的所有txt文件,并打印出这些文件的路径。
如果把 if(f.getName().indexOf(".txt") != -1) 稍微修改一下,用个证则表达式,应该就以找到英文TXT文本,具体这个证则表达式怎么写,我还没有研究过。
占个沙发先。
FileInputStream fin = new FileInputStream(pathFileName);
int len = fin.available();
char[] data = new char[len];
InputStreamReader reader = new InputStreamReader(fin);
reader.read(data);
return data;
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URI;public class FilePathRead {
List txtFileList = new ArrayList(); public static void main(String[] args){
FilePathRead t = new FilePathRead();
t.find(new File("C:\\c_folder"));
t.find(new File("C:\\B_folder"));
...... // 如果是多个可以考虑循环
}
public void find(File f){
if(f.isDirectory()){
File[] fileList = f.listFiles();
for(int i = 0; i < fileList.length; i++){
find(fileList[i]);
}
}else{
if(matchResult(f.getName())) // 匹配
txtFileList.add(f.getAbsolutePath());
}
}
public boolean matchResult(String str) {
Pattern p = Pattern.compile("^[0-9a-zA-Z]+.[tT][xX][tT]$"); // 匹配txt文件。
Matcher m = p.matcher(str);
return m.find();
}
Pattern 和 Matcher 是什么?所有文件的内容是在txtFileList里吗?thanks