Home > Java > javaTutorial > Detailed explanation of the basic usage of File class in Java file programming

Detailed explanation of the basic usage of File class in Java file programming

黄舟
Release: 2017-08-11 09:48:18
Original
1645 people have browsed it

下面小编就为大家带来一篇Java文件(io)编程_基于File类的基本用法(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1、首先了解文件流的相关概念:

2、文件File类的基本用法


public class Demo_1 {

  public static void main(String[] args) {
  
    //创建一个文件对象
    File f=new File("e:\\aa.txt");

    //得到文件的路径
    System.out.println("文件路径:"+f.getAbsolutePath());
    
    //得到文件的大小,字节数
    System.out.println("文件大小:"+f.length());
    
    System.out.println("文件可读:"+f.canRead());

    //创建文件夹
    File f3=new File("e:\\ff");
    if(!f3.isDirectory()){
      //创建
      f3.mkdir();
    }else{
      System.out.println("文件夹已存在");
    }
    
    //创建文件
    File f2=new File("e:\\ff\\hsp.txt");
    if(!f2.exists()){
      //可以创建
      try {
        f2.createNewFile();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }else{
      System.out.print("有文件,不能创建");
    }
    
    //列出一个文件夹下面的所有文件
    File f4=new File("e://安装包下载");   //引号下也可写成e:/ff
    
    if(f4.isDirectory()){
      File lists[]=f4.listFiles();
      for(int i=0;i<lists.length;i++){
        System.out.println("文件名:"+lists[i].getName());
      }
    }    
  }
}
Copy after login

The above is the detailed content of Detailed explanation of the basic usage of File class in Java file programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template