您的位置:首页 > 编程语言 > Java开发

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);}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: