zoukankan      html  css  js  c++  java
  • java IO之File基本操作

    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//"G:\JAVA\test\test.txt"
    		createNewFile();
    		showSeparator();
    		useSepartor();
    		mkNewDir();
    		showAllNoSub();
    		showAllWithSub(new File("G:"+File.separator+"JAVA"+File.separator+"test"+File.separator));
    	
    	}
    
    	/*
    	 * 1.创建一个新文件
    	 */
    	public static void createNewFile() {
    		File file = new File("G:\JAVA\test\test.txt");
    		try {
    			file.createNewFile();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    	}
    
    	/*
    	 * 2.file类的常量separator,patSeparator。文件分割符 //对于不同的系统分隔符孚不一样
    	 */
    	public static void showSeparator() {
    		System.out.println(File.separator); // 
    		System.out.println(File.pathSeparator); // ;
    	}
    
    	/*
    	 * 3.删除文件,用separator让程序更健壮
    	 */
    	public static void useSepartor() {
    		String fileName = "G:" + File.separator + "JAVA" + File.separator
    				+ "test" + File.separator + "test.txt";
    		File file = new File(fileName);
    		if (file.exists())
    			file.delete();
    		else
    			System.out.println("文件不存在");
    
    	}
    
       /*
        * 4.创建一个文件夹
        */
    	public static void mkNewDir()
    	{
    		String filePath= "G:" + File.separator + "JAVA" + File.separator
    				+ "test" + File.separator + "newDir";
    		File file=new File(filePath);
    		if(file.isDirectory())
    			System.out.println("dir exit");
    		else
    		  System.out.println(file.mkdir());
    	}
    
    	/*
    	 *5. 列出所在目录的所有文件包括隐藏文件(但是不显示子文件)
    	 */
    	public static void showAllNoSub()
    	{
    		String filePath="G:"+File.separator+"JAVA"+File.separator+"test";
    		File file=new File(filePath);
    	    String [] all=file.list();
    		for(String s:all)
    		{
    			System.out.println(s);
    		}
    	}
    	
    	/*
    	 *6. 用递归算法,列出所在目录的所有文件包括隐藏文件,包括子文件
    	 */
    	public static void showAllWithSub(File file)
    	{
    		if(file.isFile())    //判断是文件
    			System.out.println("  File:"+file.getPath());
    		else  if(file.isDirectory())  //判断是文件夹
    		{
    			System.out.println("Dir:"+file.getPath());
    			File [] allfile=file.listFiles();
    			for(File f : allfile)
    			{
    				showAllWithSub(f);
    			}
    			
    		}
    			 
    		
    	}
    	
    
  • 相关阅读:
    使用HtmlAgilityPack将HtmlTable填入DataTable
    用EXCEL的VBA将PHPCMS的备份文件转换成HTML的一次尝试
    从微观角度看到宏观世界
    洛克菲特:如何管好你的钱包
    论永生_基因编辑
    如何隐藏自己的应用程序在服务器上不被发现?
    检视阅读
    改变了我对英语理解的语法课
    Rick And Morty使命必达与毁灭--------英语笔记
    文件太大,网速太慢,如何高效的传递到服务器上运行
  • 原文地址:https://www.cnblogs.com/fjsnail/p/3479165.html
Copyright © 2011-2022 走看看