JAVA_IO_file类基础API使用

26 篇文章 0 订阅
1.两个常量
  1. 路径分隔符
  2. 名称分隔符
public class Demo01 {
	public static void main(String[] args) {
		System.out.println(File.pathSeparator);  // ;
		System.out.println(File.separator); // \
		String path = "E:\\test\\2.jpg";// 路径表示形式
		path = "E:" + File.separator + "xp" + File.separator + "test" + File.separator + "2.jpg";
		// 推荐方式
		path = "E:/xp/test/2.jpg";
	}
}
2.使用相对路径与绝对路径构造 File对象
import java.io.File;

/**
 * 相对路径与绝对路径构造 File对象
 * 1、相对路径
 * 	File(String parent, String child)  ==>File("E:/xp/test","2.jpg")
 *  File(File parent, String child)     ==> File(new File("E:/xp/test"),"2.jpg")
 * 2、绝对路径
 *  File(String name);
 * @author WL20180732
 *
 */
public class Demo02 {
	public static void main(String[] args) {
		String parentPath = "E:/xp/test";
		String name = "2.jpg";
		// 相对路径
		File src = new File(parentPath, name);
		src = new File(new File(parentPath), name);
		// 输出
		System.out.println(src.getName());
		System.out.println(src.getPath());
		// 绝对路径
		src = new File("E:/xp/test/2.jpg");
		System.out.println(src.getName());
		System.out.println(src.getPath());
		// 没有盘符: 以 user.dir构建
		src = new File("test.txt");
		src = new File(".");
		System.out.println(src.getName());
		System.out.println(src.getPath());
		System.out.println(src.getAbsolutePath());
	}
}
3.常用方法
package test.file;

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

/**
 * 常用方法:
 * 1.文件名
 * 	getName() 文件名、路径名
 *  getPath()路径名
 *  getAbsoluteFile() 绝对路径所对应的File对象
 *  getAbsolutePath() 绝对路径名
 *  getParent() 父目录 ,相对路径的父目录,可能为null 如. 删除本身后的结果
 * 2.判断信息
 *  exists()
 *  canWrite()
 *  canRead()
 *  isFile()
 *  isDirectory()
 *  isAbsolute():消除平台差异,ie以盘符开头,其他以/开头
 * 3.长度 字节数  不能读取文件夹的长度
 *  length()
 * 4.创建、删除
 *  createNewFile() 不存在则创建新文件,存在返回false
 *  delete() 删除文件
 *  static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
 *  staticcreateTempFile(前缀3个字节长,后缀默认.temp,目录)
 *  deleteOnExit() 退出虚拟机删除,常用于删除临时文件
 *  
 * @author WL20180732
 *
 */
public class Demo03 {
	public static void main(String[] args) throws IOException, InterruptedException {
//		test1();
//		test2();
//		test3();
	}

	// 创建删除文件
	public static void test3() throws IOException, InterruptedException {
		// createNewFile() 不存在创建新文件
		// String path="E:/xp/test/con"; //con系统关键字
		String path = "E:/xp/test/200.jpg";
		// String path="E:/xp/test/1.jpg";
		File src = new File(path);
		if (!src.exists()) {
			boolean flag = src.createNewFile();
			System.out.println(flag ? "成功" : "失败");
		}

		// 删除文件
		boolean flag = src.delete();
		System.out.println(flag ? "成功" : "失败");

		// static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
		// static createTempFile(前缀3个字节长,后缀默认.temp,目录)
		File temp = File.createTempFile("tes", ".temp", new File("e:/xp/test"));
		Thread.sleep(10000);
		temp.deleteOnExit(); // 退出即删除
	}

	// 2、判断信息
	// 3、长度 length()
	public static void test2() {
		// String path ="2.txt";
		String path = "E:/xp/test/200.jpg";
		// String path="E:/xp/test/200.jpg";
		File src = new File(path);
		// 是否存在
		System.out.println("文件是否存在:" + src.exists());
		// 是否可读 写 canWrite() canRead()
		System.out.println("文件是否可写" + src.canWrite());
		System.out.println("============");
		// isFile()
		// isDirectory()
		if (src.isFile()) {
			System.out.println("文件");
		} else if (src.isDirectory()) {
			System.out.println("文件夹");
		} else {
			System.out.println("文件不存在");
		}

		System.out.println("是否为绝对路径" + src.isAbsolute());
		System.out.println("长度为:" + src.length());

	}
	
	// 1、名称
	public static void test1() {
		// File src =new File("E:/xp/test/2.jpg");
		// 建立联系
		File src = new File("2.txt");
		System.out.println(src.getName()); // 返回名称
		System.out.println(src.getPath()); // 如果是绝对路径,返回完整路径,否则相对路径
		System.out.println(src.getAbsolutePath());// 返回绝对路径
		System.out.println(src.getParent());// 返回上一级目录,如果是相对,返回null
	}
}

4.操作目录
package test.file;

import java.io.File;
import java.io.FilenameFilter;

/**
 * 5.操作目录
 * mkdir() 创建目录,必须确保 父目录存在,如果不存在,创建失败
 * mkdirs() 创建目录,如果父目录链不存在一同创建
 * list() 文件|目录名   字符串形式
 * listFiles()
 * static listRoots() 根路径
 * @author WL20180732
 *
 */
public class Demo04 {

	public static void main(String[] args) {
//		test1();
		test2();
	}

	private static void test2() {
		String path = "E:/xp/test/";
		File src = new File(path); // 文件夹
		if (src.isDirectory()) { // 存在并且为目录
			System.out.println("======子目录|文件名===");
			String[] subNames = src.list();

			for (String temp : subNames) {
				System.out.println(temp);
			}
			System.out.println("=====子目录|文件File对象====");
			File[] subFiles = src.listFiles();
			for (File temp : subFiles) {
				System.out.println(temp.getAbsolutePath());
			}
			System.out.println("=====子文件.java对象====");
			// 命令设计模式
			subFiles = src.listFiles(new FilenameFilter() {

				@Override
				/**
				 * dir代表src
				 */
				public boolean accept(File dir, String name) {
					// System.out.println(dir.getAbsolutePath());
					return new File(dir, name).isFile() && name.endsWith(".java");
				}

			});
			for (File temp : subFiles) {
				System.out.println(temp.getAbsolutePath());
			}
		}
	}
	
	public static void test1(){
		String path ="E:/xp/test/parent/p/test";
		File src =new File(path);
		//src.mkdir();
		src.mkdirs();
	}
}

5.递归输出子孙级目录|文件的名称(绝对路径)
package test.file;

import java.io.File;
import java.util.Arrays;

/**
 * 输出子孙级目录|文件的名称(绝对路径)
 * 1.listFiles()
 * 2.递归
 * static listRoots() 根路径
 * @author WL20180732
 *
 */
public class Demo05 {
	public static void main(String[] args) {
		String path = "E:/xp/test";
		File parent = new File(path);
		// printName(parent);
		File[] roots = File.listRoots();
		System.out.println(Arrays.toString(roots));
		for (File temp : roots) {
			// printName(temp);

		}
	}

	/**
	 * 输出路径
	 */
	public static void printName(File src) {
		if (null == src || !src.exists()) {
			return;
		}
		System.out.println(src.getAbsolutePath());
		if (src.isDirectory()) { // 文件夹
			for (File sub : src.listFiles()) {
				printName(sub);
			}

		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值