File文件的 基本操作
来源:互联网 发布:三元表达式 java 编辑:程序博客网 时间:2024/06/10 05:26
package com.java.list.ex1;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileTest {
public staticvoid main(String[] args) {
testFile();
}
/**
* 判断输入的绝对路径是代表一个文件或一个目录。若是文件输出此文件的绝对路径,
* 并判断此文件的文件属性(是否可读写或隐藏);若是目录则输出该目录下所有文件(不包括隐藏文件)。
*
* @throws IOException
*/
public staticvoid testFile() {
String path = inputPathFromKeyBoard();
File file = new File(path);
if (file.isFile()) {
System.out.println("是文件");
System.out.println("绝对路劲:" +file.getPath());
System.out.println("文件属性:" + (file.canRead() ?"可读文件" : "不可读文件"));
System.out.println("文件属性:" + (file.canWrite() ?"可写文件" : "不可写文件"));
System.out.println("文件属性" + (file.isHidden() ?"隐藏文件" : "非隐藏文件"));
} else if (file.isDirectory()) {
System.out.println("是目录");
File[] lists =file.listFiles();
for (Filef : lists) {
if (f.isDirectory() ==false) {
System.out.println(f.getPath());
}
}
} else {
if (createFile("/Users/yzk/Documents/Demo/Yzk.txt")) {
System.out.println("创建文件成功");
} else {
System.out.println("创建文件失败");
}
}
}
public staticboolean createFile(String path) {
File file = new File(path);
String fileDir = file.getPath();
int lastIndex = fileDir.lastIndexOf(File.separator);
String dir = fileDir.substring(0, lastIndex);
File dirFile = new File(dir);
dirFile.mkdirs();
try {
if (file.createNewFile()) {
returntrue;
} else {
returnfalse;
}
} catch (IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnfalse;
}
public static String inputPathFromKeyBoard() {
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
System.out.println("请输入路劲:");
String path = "";
try {
path =br.readLine();
} catch (IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnpath;
}
}
更多方法
详见API
- 文件File的基本操作
- File文件的 基本操作
- File的基本操作
- File类的基本操作之创建文件
- File类的基本操作之删除文件
- File类的基本操作之读出文件路径
- 操作File文件的一些基本打包函数
- File文件的一些基本方法和操作
- File的基本实用操作
- Java File的基本操作
- File类的基本操作
- File类的基本操作
- File文件的基本属性
- IO容器与文件操作(二)File类的基本操作
- 文件的基本操作
- 文件的基本操作
- 文件的基本操作
- 文件的基本操作
- Gradle build finished with 200 error(s)
- Hessian源码分析--HessianSkeleton
- HTML5学习_day11(1)--HTML新标签
- ☆首发☆非常适合学习理解laravel的项目--图书管理系统
- leetcode No138. Copy List with Random Pointer
- File文件的 基本操作
- 基于N源码的ContentService数据通知机制
- Maven中Pom文件打包配置build
- Java基础复习---面向对象程序设计
- 130. Surrounded Regions 将包围的符号变换 BFS & DFS & UNION find
- Median of Two Sorted Arrays
- 51单片机串口通信(字符串接收和发送)
- linux c++ epoll的简单实现
- JavaScript常用的事件及简单应用