zoukankan      html  css  js  c++  java
  • JAVA之File类创建对象构造函数传参数需要注意的几点

    java中File类用于创建一个文件对象。

    首先看一段代码:

    1.

    package MyText1;
    
    import java.io.File;
    
    public class MyText1 {
    	public static void main (String []args)
    	{
    		File file1 = new File("G:\");
    		File file = new File(file1, "helloworld.txt");
    		if(file.exists())
    		{
    			String str = file.getName();
    			System.out.println(str);
    		}
    		else
    		{
    			try{
    				file.createNewFile();
    				System.out.println("文件创建成功");
    			}catch(Exception e)
    			{
    				System.out.println("异常");
    			}
    		}
    	}
    }
    

    在G盘中创建helloworld.txt文件。



    2.

    package MyText1;
    
    import java.io.File;
    
    public class MyText1 {
    	public static void main (String []args)
    	{
    		File file1 = new File("G:\text1\src");
    		File file = new File(file1, "helloworld.txt");
    		if(file.exists())
    		{
    			String str = file.getName();
    			System.out.println(str);
    		}
    		else
    		{
    			try{
    				file.createNewFile();
    				System.out.println("文件创建成功");
    			}catch(Exception e)
    			{
    				System.out.println("异常");
    			}
    		}
    	}
    }
    

    这里要说下在windows中File file1 = new File("G:\text1\src");与File file1 = new File("G:/text1/src");效果一样

    该代码运行会输出异常,原因是helloworld.txt要在G:\txt\src文件夹下创建,而G盘中没有txt\src文件夹,故提示异常

    所以需要在G盘下建立一个txt文件夹,并在创建txt的子文件夹src。

    package MyText1;
    
    import java.io.File;
    
    public class MyText1 {
    	public static void main (String []args)
    	{
    		File file1 = new File("G:\text1\src");
    		if(file1.exists())
    		{
    			String str = file1.getName();                                                                                              
    			System.out.println(str);
    			
    		}
    		else
    		{
    			try{
    				file1.mkdirs();
    				System.out.println("创建文件夹成功");
    			}catch(Exception e)
    			{
    				System.out.println("异常");
    			}
    		}
    		File file = new File(file1, "helloworld.txt");
    		if(file.exists())
    		{
    			String str = file.getName();
    			System.out.println(str);
    		}
    		else
    		{
    			try{
    				file.createNewFile();
    				System.out.println("文件创建成功");
    			}catch(Exception e)
    			{
    				System.out.println("异常");
    			}
    		}
    	}
    }
    



    第一次运行结果:

    创建文件夹成功
    文件创建成功

    ====================================================

    创建一个文件夹可以用mkdir方法,而创建父子文件夹需要用mkdirs方法。

    ====================================================

    第二次运行结果:

    src
    helloworld.txt

    ====================================================

    getName方法获取文件名或者最内层文件夹名

    ====================================================

    package MyText1;
    
    import java.io.File;
    
    public class MyText1 {
    	public static void main (String []args)
    	{
    		File file1 = new File("G:\text1\src");
    		if(file1.exists())
    		{
    			String str = file1.getName();                                                                                              
    			System.out.println("文件夹名称:" + str);
    			System.out.println("是否是目录:" + file1.isDirectory());
    		}
    		else
    		{
    			try{
    				file1.mkdirs();
    				System.out.println("创建文件夹成功");
    			}catch(Exception e)
    			{
    				System.out.println("异常");
    			}
    		}
    		File file = new File(file1, "helloworld.txt");
    		if(file.exists())
    		{
    			System.out.println("==============================");
    			String str = file.getName();
    			long l = file.length();
    			String str1 = file.getAbsolutePath();
    			String str2 = file.getParent();
    			boolean b1 = file.isFile();
    			boolean b2 = file.isDirectory();
    			System.out.println("长度:" + l);
    			System.out.println("文件名称:" + str);
    			System.out.println("绝对路径:" + str1);
    			System.out.println("父路径:" + str2);
    			System.out.println("是否是文件:" + b1);
    			System.out.println("是否是目录:" + b2);
    		}
    		else
    		{
    			try{
    				file.createNewFile();
    				System.out.println("文件创建成功");
    			}catch(Exception e)
    			{
    				System.out.println("异常");
    			}
    		}
    	}
    }
    


    输出结果:

    文件夹名称:src
    是否是目录:true
    ==============================
    长度:0
    文件名称:helloworld.txt
    绝对路径:G: ext1srchelloworld.txt
    父路径:G: ext1src
    是否是文件:true
    是否是目录:false

  • 相关阅读:
    基础总结深入:数据类型的分类和判断(数据、内存、变量) 对象 函数 回调函数 IIFE 函数中的this 分号
    BOM 定时器 通过修改元素的类来改变css JSON
    事件 事件的冒泡 事件的委派 事件的绑定 事件的传播
    DOM修改 使用DOM操作CSS
    包装类 Date Math 字符串的相关的方法 正则表达式 DOM DOM查询
    数组 call()、apply()、bind()的使用 this arguments
    autocad 二次开发 最小包围圆算法
    win10 objectarx向导在 vs2015中不起作用的解决办法
    AutoCad 二次开发 jig操作之标注跟随线移动
    AutoCad 二次开发 文字镜像
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3301600.html
Copyright © 2011-2022 走看看