javaIO流之File类

来源:互联网 发布:知网阅读器mac 编辑:程序博客网 时间:2024/05/01 04:13

在java中File类主要是表示文件和目录,只能创建文件,而不能访问文件;

File的构造函数如下:

File(File file,String str)File(String dircetory)File(String directory,String filename)
file是一个File对象,directory是文件所在目录的路径名,
用下列的例子来说明几个常用的方法

package com.test;import java.io.File;import java.io.IOException;public class FILE {public static void main(String[] args)throws IOException {File file = new File("D:\\learning\\FILE");//Create File objectif(!file.exists()){//To determine whether the directory existsfile.mkdir();//Create directory}System.out.println(file.isDirectory());//To see if it is a directorySystem.out.println(file.isFile());//To see if it is a fileFile file1 = new File("D:\\learning\\FILE\\file.txt");//Create File objectif(!file1.exists()){file1.createNewFile();//Create file}System.out.println(file.getAbsolutePath());//Absolute path string for the print directorySystem.out.println(file1.getAbsolutePath());//Absolute path string for the print fileSystem.out.println(file.getName());//View directory nameSystem.out.println(file1.getName());//View file nameSystem.out.println(file.getParent());//Get the parent directory nameSystem.out.println(file1.getParent());//Get the parent file name}}



0 0
原创粉丝点击