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. logstash filter plugin

    1. 基本语法%{NUMBER:duration} %{IP:client} 2. 支持的数据类型默认会把所有的匹配都当作字符串,比如0.043, 想要转成浮点数,可以%{NUMBER:num:flo ...

  2. [BZOJ3237][AHOI2013]连通图(分治并查集)

    3237: [Ahoi2013]连通图 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1736  Solved: 655[Submit][Status ...

  3. [BZOJ5289][HNOI2018]排列(拓扑排序+pb_ds)

    首先确定将所有a[i]向i连边之后会形成一张图,图上每条有向边i->j表示i要在j之前选. 图上的每个拓扑序都对应一种方案(如果有环显然无解),经过一系列推导可以发现贪心策略与合并的块的大小和w ...

  4. hdu 1500 Chopsticks DP

    题目链接:HDU - 1500 In China, people use a pair of chopsticks to get food on the table, but Mr. L is a b ...

  5. 线程同步-CountDownLatch

    应用场景: 有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行. 假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await()方法,其他的任务执行 ...

  6. JAVA生成问答式验证码图片,支持加减算法

    原文:http://liuguihua0823.iteye.com/blog/1511355 import java.awt.Color; import java.awt.Font; import j ...

  7. FragmentTransaction的commit的异步操作

    FragmentTransaction是异步的,commit()仅是相当于把操作加入到FragmentManager的队列,然后FragmentManager会在某一个时刻来执行,并不是立即执行.所以 ...

  8. win10 安装java

    https://jingyan.baidu.com/article/fea4511a12b158f7bb9125b9.html 一 下载java SE 官网 二设置环境变量 JAVA_HOME PAT ...

  9. django 分页django-pure-pagination

    虽然django自带了一个paginator,但不是很方便,我们使用django-pure-pagination github地址https://github.com/jamespacileo/dja ...

  10. DTD 和 Schema简介

    什么是DTD? DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块. 它使用一系列的合法元素来定义文档结构. DTD例子 <?xml version="1.0"? ...