java代码--通过某路径读取数据
来源:互联网 发布:windows模拟器中文版 编辑:程序博客网 时间:2024/04/30 12:55
/**
* 获取一个文件夹下的所有文件
* 要求:后缀名为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();
}
}
}
阅读全文
0 0
- java代码--通过某路径读取数据
- java通过相对路径读取properties数据
- JAVA代码:通过Socket读取返回的数据
- JAVA代码:通过Socket读取返回的数据
- 通过Java读取HDFS数据
- java通过串口读取数据
- iOS 通过路径对文件写入数据 读取数据
- java代码读取excel数据
- java SDK ,上传、读取 基本类型数据通过服务器(客户端代码)
- java通过SOAP读取天气预报数据
- 通过java读取HDFS的数据
- java通过URL读取json数据
- java通过URL读取json数据
- java通过URL读取json数据
- 通过java读取HDFS的数据
- Java通过串口读取数据例子
- 【java基础】java通过相对路径读取文件
- 通过mysqli读取数据
- linux--Ubuntu安装jdk
- ubuntu16.04编译hadoop-1.0.0
- css里px、em、rem的区别和应用
- 从前端到全端:JavaScript逆袭之路
- 搭建自己的ss服务器
- java代码--通过某路径读取数据
- 十一月一号课堂笔记
- 设计23式—模板方法
- 10月个人总结
- ESP8266编译脚本
- 蓝桥杯 入门训练 序列求和
- Jacoco覆盖率工具使用(已测试)
- ES6的let 与 const 的用法
- vivado IP操作