JAVA IO流 <一>文件基本操作
2015-04-21 11:09
555 查看
文件的创建,显示等
public class File_1 implements createFile {public static void main(String[] args) throws IOException {makeDir("d:\\","abc");createFile("d:\\","abc","1.txt");// showFile(path+File.separator+"1.txt");}public static void makeDir(String path, String dirName) {//创建文件夹String str = path+File.separator+dirName;File file = new File(str);boolean b = file.mkdir();if(b)System.out.println("创建文件夹成功!!");elseSystem.out.println("创建文件夹失败!!");System.out.println();}// public static void showFile(String str) throws IOException {// String str = path+File.separator+name;// File file = new File(str);// //获取文件的名称// System.out.println("文件名称"+file.getName());// //获取文件大小// System.out.println("文件大小: "+file.length());// //获取文件的路径// System.out.println("文件的路径: "+file.getParent());// //获取文件的最后修改时间// /*// * 创建日期的方法:// * 1-- 创建Date对象 ,方法为:Date date = new Date(参数:可以为年月日,long型的数字等);// * 2-- 设置时间的格式,方法为:DateFormat dateFormat = DateFormat.getDateTimeInstance(格式)// * 3-- 用设置好的格式来来设置上面创建好的日期对象,方法为:dateFormat.format(date);// * OK结束// * */// long time = file.lastModified();// Date date = new Date(time);// DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);// String str_date = dateFormat.format(date);// System.out.println("该文件最后修改的时间为: "+str_date);// }public static void createFile(String path, String dirName, String wName) throws IOException {String str = path +File.separator+ dirName +File.separator+ wName;File file = new File(str);//在D盘上创建一个1.txtboolean b = file.createNewFile();if(b)System.out.println("创建文件成功!!");elseSystem.out.println("创建文件失败!!");System.out.println();}}使用递归实现文件目录的显示:
import java.io.File;public class ShowAllFiles {/*** 本程序演示使用递归遍历所有文件*/public static void main(String[] args) {File dir = new File("D:\\eclipse\\java_code");showAllFiles(dir);}static void showAllFiles(File dir) {File [] file = dir.listFiles();for(File s : file){if(s.isDirectory()){System.out.println("文件夹"+ s);showAllFiles(s) ; //使用递归}elseSystem.out.println("文件"+s);}}}
相关文章推荐
- <<<I、O概述及字符流基本操作>>>
- Nosql :MongoDB一些基本操作<一>
- Java中的 File类对文件 路径分割符不敏感! <发现>
- Hadoop中文件读写(Java) &lt;转&gt;
- 黑马程序员_<<IO流基本操作(Writer,Reader)>>
- Java基础加强<一>_MyEclipse、JDK1.5新特性、枚举、反射
- LDAP学习笔记<四>jldap实现Java对LDAP的基本操作
- <java——IO流>
- java对redis的基本操作<转>
- Java中的<< >>运算符操作细节
- <Head First Java>学习笔记--第一章:基本概念
- < 基本功>short与Unsigned short以及Java基本类型
- [Perl]对*.zip文件操作的记录<一>
- <Java>RandomAccessFile在文件中定位写入
- <一>java数据结构与算法 数组
- <Java>使用String读取文件名称获取得定位置的字符串
- <Win32 API> 文件操作CreateFile/ReadFile/WriteFile
- Java多态之重写<一>
- java对redis的基本操作<转>
- Jdbc基本操作<一>