[IO]——创建文件
2016-04-04 02:25
337 查看
public class demo03 { public static void main(String[] args) { //test1(); //test2(); try { test3(); } catch (Exception e) { e.printStackTrace(); System.out.println("文件操作失败"); } } public static void test3() throws IOException, InterruptedException{//创建为文件,删除文件 String path="E:/others/www.txt";//con为系统关键字 File src=new File(path); if (!src.exists()) { Boolean flag = src.createNewFile(); System.out.println(flag?"成功":"失败"); } //删除文件 Boolean flag=src.delete(); System.out.println(flag?"成功":"失败"); //static createTempFile(前缀3个字节,后缀默认.temp,默认临时空间) //static createTempFile(前缀3个字节,后缀默认.temp,目录) File temp=File.createTempFile("aaa", ".temp",new File("E:/others/")); Thread.sleep(10000); temp.deleteOnExit();//程序退出,则自动删除 } public static void test2(){//判断信息 File src=new File("E:/others/w.png"); System.out.println("文件是否存在 "+src.exists());//文件是否存在 System.out.println("文件是否可写"+src.canWrite());//是否可读写,canwrite(),canread() //isFile(),isDirectory if (src.isFile()) { System.out.println("文件"); }else if(src.isDirectory()){ System.out.println("文件夹"); }else { System.out.println("文件不存在"); } System.out.println("是否为绝对路径 "+src.isAbsolute()); System.out.print(src.length());//长度,字节长度,只有文件才能读出长度 } public static void test1(){//获取文件名的常用方法,建立联系 File src=new File("E:/others/w.png"); System.out.println(src.getName());//返回名称 System.out.println(src.getPath());//如果是绝对路径,返回完整路径,否则返回相对路径 System.out.println(src.getAbsolutePath());//返回绝对路径 System.out.println(src.getParent());//返回上一级目录,如果没有上一级,返回空 } }
相关文章推荐
- Mysql IO 内存方面的优化
- Node.js 的异步 IO 性能探讨
- SQL Server误区30日谈 第22天 资源调控器可以调控IO
- mysql 数据同步 出现Slave_IO_Running:No问题的解决方法小结
- java中的Io(input与output)操作总结(一)
- GO语言的IO方法实例小结
- java中的Io(input与output)操作总结(四)
- C#路径,文件,目录及IO常见操作汇总
- SQL语句实现查询当前数据库IO等待状况
- Java进阶教程之IO基础
- java中的Io(input与output)操作总结(三)
- Java 1.0和Java 1.1 的IO类的比较
- JAVA IO API使用详解
- Java NIO和IO的区别
- Java编程中最基础的文件和目录操作方法详解
- Python中使用select模块实现非阻塞的IO
- Python通过poll实现异步IO的方法
- Python通过select实现异步IO的方法
- Java编程中字节流与字符流IO操作示例
- STM32 IO口双向问题