zoukankan      html  css  js  c++  java
  • 学习总结 java 输入输出流

    思维导图

    代码实际演示

     1 package com.hanqi.io;
     2 
     3 import java.io.*;
     4 
     5 public class Test1 {
     6 
     7     public static void main(String[] args) {
     8         // File 文件
     9         
    10         //构造方法(文件全路径+文件名)
    11         File  file = new File("d:\test1.txt");
    12 
    13         //判断文件是否存在
    14         if(file.exists())
    15         {
    16             System.out.println("文件存在");
    17             
    18             System.out.println("getAbsolutePath = " + file.getAbsolutePath());
    19             
    20             System.out.println("getParent = " + file.getParent());
    21             
    22             System.out.println("getName = " + file.getName());
    23             
    24             //删除文件
    25             //file.delete();
    26             
    27             //给文件改名
    28             file.renameTo(new File("d:/test2.txt"));
    29             
    30             
    31         }
    32         else
    33         {
    34             
    35             System.out.println("文件不存在");
    36             
    37             //创建新文件
    38             //可控式异常
    39             try
    40             {
    41                 
    42             file.createNewFile();
    43             
    44             System.out.println("文件创建成功");
    45             
    46             }
    47             catch(IOException e)
    48             {
    49                 
    50                 System.out.println("文件创建失败");
    51                 
    52                 e.printStackTrace();
    53             }
    54         }
    55         
    56     
    57         
    58     }
    59 
    60 }

    创建文件

    package com.hanqi.io;
    
    import java.io.*;
    
    public class Text2 {
    
        public static void main(String[] args) {
            
            
            //操作目录
            File dir = new File("d:/test/test");
            
            if(!dir.exists())
            {
                //创建目录
                if(dir.mkdirs())
                {
                    System.out.println("创建目录成功");
                }
                else
                {
                    System.out.println("创建目录失败");
                }    
                
            }
            
            
            
            
            File file = new File("d:/test/test.txt");  //指定一个具体文件
            if(file.exists())
            {
                
            }
    
            else
            {
                try
                {
                    file.createNewFile();
                    System.out.println("创建文件成功");
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                    System.out.println("创建目录失败");
                }
                
            }
        }
    
    }

  • 相关阅读:
    cookie和session的区别和用法
    JavaScript深浅拷贝
    前端知识
    typescript -- ts
    vue笔记精华部分
    宜人贷项目里-----正则匹配input输入月份规则
    PHP-Socket-阻塞与非阻塞,同步与异步概念的理解
    PHP socket客户端长连接
    PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令
    查看局域网内所有ip
  • 原文地址:https://www.cnblogs.com/zhoudi/p/5544815.html
Copyright © 2011-2022 走看看