Java读取文件
来源:互联网 发布:php简单论坛源码 编辑:程序博客网 时间:2024/06/01 20:32
Java读取文件
项目开发的时候经常会遇到有读取文件的情况,下面简单描述直接上代码
/** * 读取某个文件夹下的所有文件 * filepath 文件夹路径 */ public static boolean readfile(String filepath) throws FileNotFoundException, IOException { try { File file = new File(filepath); if (!file.isDirectory()) { System.out.println("文件"); System.out.println("路径=" + file.getPath()); System.out.println("绝对路径=" + file.getAbsolutePath()); System.out.println("文件名=" + file.getName()); } else if (file.isDirectory()) { System.out.println("文件夹"); String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); if (!readfile.isDirectory()) { System.out.println("路径1=" + readfile.getPath()); System.out.println("绝对路径1=" + readfile.getAbsolutePath()); System.out.println("文件名1=" + readfile.getName()); } else if (readfile.isDirectory()) { readfile(filepath + "\\" + filelist[i]); } } } } catch (FileNotFoundException e) { System.out.println("readfile() Exception:" + e.getMessage()); } return true; } public static void main(String[] args) { try { //linux readfile(File.separator+"home"); //windows readfile("C:"+File.separator+"tmp"+File.separator+"test.txt"); } catch (FileNotFoundException ex) { } catch (IOException ex) { } System.out.println("ok"); }
File.separator方法是jdk提供的自动加路径标示的方法,该方法根据Windows和Unix的不同输出’\’(Windows)或者是’/’(Unix)
阅读全文
0 0
- java读取ini文件
- JAVA读取XML文件
- java读取中文文件
- Java读取文件
- JAVA读取XML文件
- java读取中文文件
- java读取pdf文件
- JAVA读取word文件
- JAVA读取PDF文件
- java读取word文件
- java读取excel文件
- java读取excel 文件
- java 读取文件(中文)
- java读取文件源代码
- java 读取文件
- Java读取Properties文件
- java读取.properties文件
- Java读取文件
- js 空数组直接赋值与push
- zoj3961(区间问题)
- #if、#ifdef、#if defined之间的区别
- JAVASCRIPT
- mysql查询表里的重复数据方法:
- Java读取文件
- FastJson--常用方法总结
- 创建型模式—建造者模式
- 本地存储封装-localStorage,sessionStorage,userData
- iOS中的crash防护(四)NSNotificationCenter指定线程接收通知
- MyBatis教程之一基本使用入门
- Jvm垃圾收集器和垃圾回收算法
- 关于json数据中包含json在低版本中出错的状况
- iOS 加载H5页面的时候添加一个菊花