package xinhuiji_day07;

import java.io.File;
import java.io.IOException;

public class FileTest {

/**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //1, 创建一个只适用于linux平台的文件
        File file = new File("/home/han/hh.java");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
        //2,打印出当前操作系统的默认名称分隔符    路径分隔符
        System.out.println("separator\t"+File.separator);
        System.out.println("pathSeparator\t"+File.pathSeparator);
        
        //3,可以适应不同操作系统的创建File的方法
        String path = File.separator+"home"+File.separator+"han"+File.separator+"han.java";
        File file1 = new File(path);
        try {
            file1.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        //4,删除文件
        if(file.exists()){  //要先判断文件是否存在
            System.out.println(file.delete());
        }
        
        //5,给定一个文件路径,若该文件存在则删除该文件,如果不存在则创建该文件
        String path2 = File.separator+"home"+File.separator+"han"+File.separator+"test.java";
        File file2 = new File(path2);
        if(file2.exists()){
            file2.delete();
        }else{
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        //6,创建一个文件夹
        String path3 = File.separator+"home"+File.separator+"han"+File.separator+"han";
        File file3 = new File(path3);
        file3.mkdir();
        
        //7,列出指定目录的全部文件
        String path4 = File.separator+"home"+File.separator+"han"+File.separator+"han";
        File file4 = new File(path4);
        String[] files4 = file4.list(); //调用File的list()方法获得当前目录下的所有文件名
        for(String i:files4){
            System.out.println(i);
        }
        System.out.println("-------------------------------");
        File[] filez = file4.listFiles();  //调用File的listFiles()方法获得当前目录下的所有文件
        for(File i:filez){                //该方法获得的是完整的路径名和文件名
            System.out.println(i);
        }
        //8,判定一个给定的路径是否是目录
        String path5 = File.separator+"home"+File.separator+"han"+File.separator+"han";
        File file5 = new File(path5);
        System.out.println(file5.isDirectory());
        
    }

}

File 的基本操作的更多相关文章

  1. file的基本操作;file的修改

    file的基本操作 # Author:nadech # 文件读写/修改/ #data = open("yesterday",encoding="utf-8"). ...

  2. Java File类基本操作

    我们可以利用Java.io.File类对文件进行操作,基本操作如下: 1)创建文件: public boolean createNewFile() throws IOException 2)删除文件: ...

  3. File类基本操作之OutputStream字节输出流

    贴代码了,已经測试,可正常编译 package org.mark.streamRW; import java.io.File; import java.io.FileOutputStream; imp ...

  4. 01.File文件基本操作

    1-创建File对象 /** * 创建我们 java.io.File对象 */ public static void test1() { //第一创建对象方式 File parent=new File ...

  5. Java之IO流概述和File基本操作

    IO流图解 IO(in / out)流的分类 流向: 输入流  读取数据 输出流  写出数据 数据类型: 字节流 一个字节占8位, 以一个字节为单位读数据 八大数据类型所占字节数: byte(1), ...

  6. 文件基本操作 (C语言)

    一切皆文件 ---Linux 头文件 <stdio.h> 中定义了文件的相关操作 #include <stdio.h> 文件操作基本流程: 打开:fopen 相关操作 关闭:f ...

  7. File System Object(FSO对象)A

    FSO对象模型包含在Scripting 类型库 (Scrrun.Dll)中,它同时包含了Drive.Folder.File.FileSystemObject和TextStream五个对象: 1.Dri ...

  8. java学习笔记之IO编程—File文件操作类

    1. File类说明 在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一一个与文件本身操作( ...

  9. Windows Registry Security Check

    catalog . Windows注册表 . Windows注册表包含的攻击向量 . 注册表安全配置基线标定 1. Windows注册表 注册表(Registry,繁体中文版Windows称之为登录档 ...

随机推荐

  1. TCP/IP,http,socket,长连接,短连接 —— 小结

    TCP/IP是什么? TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.    在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议.    在传输层中有TCP协议 ...

  2. [BZOJ5288][HNOI2018]游戏(拓扑排序)

    传送门:https://www.luogu.org/problemnew/show/P4436 20分的暴力加一个Random_shuffle就A了.我还能说什么.. 不过这个也不是毫无道理,复杂度应 ...

  3. 【bzoj3173】【Tjoi2013】【最长上升子序列】treap+dp二分优化

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61560361 向大(hei)佬(e)实力学(di ...

  4. 在mac上运行android的intel模拟器导致的死机问题解决

    最近用Mac开发android的时候遇到了一个烦人的问题,装上了HAXM,intel hardware accelerated execution manager以后,一运行android emula ...

  5. WPF--TextBox的验证

    WPF--TextBox的验证  

  6. Visual Studio 行末回车时运算符两侧自动加空格是怎么设置的

    在工具—>选项->文本编辑器->c#->格式设置->间距后有三个选项 如果已经选中"二元运算符前后插入空格" 而VS又抽风没有这个功能时,可以选中其余 ...

  7. tshop-pbsm-shop-nav-ch结构和样式分析

    html结构 <div id="content" class="eshop head-expand tb-shop"> <div id=&qu ...

  8. JRebel 7.0.10 for intellij IDEA 2017.1

    1什么是JRebel? JRebel是一套JavaEE开发工具.JRebel是一款JAVA虚拟机插件,它使得JAVA程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响.JReb ...

  9. python实现将文件夹内所有txt文件合并成一个文件

    新建一个文件夹命名为yuliao,把所有txt文件放进去就ok啦!注意路径中‘/’,windows下路径不是这样. #coding=utf-8  import os #获取目标文件夹的路径 filed ...

  10. [Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming

    Every dynamic programming algorithm starts with a grid. It entails solving subproblems and builds up ...