/**
* 获取一个文件夹下的所有文件
* 要求:后缀名为txt (可自己修改) 不用
* @param file
* @return
*/
public static List<String> getFileList(File file) {
List<String> result = new ArrayList<String>();
if (!file.isDirectory()) {
System.out.println(file.getAbsolutePath());
result.add(file.getAbsolutePath());
} else {
//内部匿名类,用来过滤文件类型,放在listFiles(); 扩号中
/* new FileFilter(){
public boolean accept(File file) {
if (file.isFile()) {// && file.getName().indexOf("txt") > -1
return true;
} else {
return false;
}
}
}*/
File[] directoryList=file.listFiles();
for(int i=0;i<directoryList.length;i++){
result.add(directoryList[i].getAbsolutePath());
}
}
return result;
}
/**
* 以gbk编码方式读取文件内容
* @param path
* @return
* @throws IOException
*/
public List<String> getContentList (String file_name, String charset) throws IOException{
List<String> Oline =null;
try {
this.fis = new FileInputStream(file_name);
this.isr = new InputStreamReader(fis, charset);
this.br = new BufferedReader(isr);
Oline=new ArrayList<String>();
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
if ("".equals(line)) {
continue;
}
Oline.add(line);
}
} finally {
this.close();
}
return Oline;
}
/*关闭流
* */
public void close() {
try {
if (this.br != null) {
this.br.close();
}
} catch (Exception e) {}
try {
if (isr != null) {
isr.close();
}
} catch (Exception e) {}
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {}
* 获取一个文件夹下的所有文件
* 要求:后缀名为txt (可自己修改) 不用
* @param file
* @return
*/
public static List<String> getFileList(File file) {
List<String> result = new ArrayList<String>();
if (!file.isDirectory()) {
System.out.println(file.getAbsolutePath());
result.add(file.getAbsolutePath());
} else {
//内部匿名类,用来过滤文件类型,放在listFiles(); 扩号中
/* new FileFilter(){
public boolean accept(File file) {
if (file.isFile()) {// && file.getName().indexOf("txt") > -1
return true;
} else {
return false;
}
}
}*/
File[] directoryList=file.listFiles();
for(int i=0;i<directoryList.length;i++){
result.add(directoryList[i].getAbsolutePath());
}
}
return result;
}
/**
* 以gbk编码方式读取文件内容
* @param path
* @return
* @throws IOException
*/
public List<String> getContentList (String file_name, String charset) throws IOException{
List<String> Oline =null;
try {
this.fis = new FileInputStream(file_name);
this.isr = new InputStreamReader(fis, charset);
this.br = new BufferedReader(isr);
Oline=new ArrayList<String>();
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
if ("".equals(line)) {
continue;
}
Oline.add(line);
}
} finally {
this.close();
}
return Oline;
}
/*关闭流
* */
public void close() {
try {
if (this.br != null) {
this.br.close();
}
} catch (Exception e) {}
try {
if (isr != null) {
isr.close();
}
} catch (Exception e) {}
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {}
}
/*调用
* */
@Test
public void test1() {
File d = new File("F:/a");
List<String> file_List = getFileList(d);
for (String file_Name : file_List) {
//文件内容列表
try {
List<String> line_List=getContentList(file_Name,"gbk");
for (String line_str : line_List) {
System.out.println("一行数据为"+line_str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}