• 学习总结 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("创建目录失败");
                }
                
            }
        }
    
    }

  • 相关阅读:
    meta标签设置(移动端)
    清除浮动
    响应式设计
    堆和堆排序
    O(n^2)以及O(nlogn)时间复杂度的排序算法
    求数组的最大连续子数组和
    HTTP缓存原理
    将两个有序数组合并为一个有序数组
    如何实现居中对齐
    查找字符串中出现最多的字符
  • 原文地址:https://www.cnblogs.com/zhoudi/p/5544815.html
Copyright © 2020-2023  润新知