File类既可以表示文件,也可以表示为文件夹
文件的创建、删除、重命名
1、文件的创建
File file=new File("new Hello.txt");//当前工程目录下
file.createNewFile();
File file=new File("bin/hello.txt");//当前工程目录下的bin文件夹下
file.createNewFile();
File file=new File("../hello.txt");//当前工程目录的上一级目录
file.createNewFile();
File file=new File("../../hello.txt");//上一级的上一级目录
file.createNewFile();
2、文件的删除
file.delete();
3、文件的重命名
(1)重命名文件
File file=new File("Hello.txt");
File nameto=new File("new Hello.txt");
file.renameTo(nameto);
(2)复制文件到指定目录(文件夹结构必须处于同一个分区,文件处于不同的分区,需要使用文件的拷贝,而不是文件的重命名)
File nameto=new File("new Hello.txt");
File nameto=new File("src/new Hello.txt");
file.renameTo(nameto);//把文件从根目录拷贝到根目录下src目录下
文件夹的创建、删除、重命名
文件夹的创建
创建单一文件夹
File folder=new File("my new folder");
folder.mkdir();//返回的是一个bool值
创建整个文件夹目录
File folder=new File("my new folder/one/two/three/main");
folder.mkdirs();
文件夹的重命名
File folder=new File("my new folder");
File newfolder=new File("my new folder-new");
folder.renameTo(newfolder);
文件夹的删除
File newfolder=new File("my new folder-new");
newfolder.delete();//删除时,当前文件夹必须是空的,否则不能被删除,删除失败,和文件的删除不同
文件属性的读取
package com.jingchenyong.io;
import java.io.File;
public class FileTest1 {
public static void main(String[] args) {
File file=new File("text.txt");
//判断文件是否存在
System.out.println("判断文件是否存在"+file.exists());
//读取文件名称
System.out.println("读取文件名称"+file.getName());
//读取文件路径(相对路径)
System.out.println("读取文件路径"+file.getPath());
//读取文件绝对路径
System.out.println("读取文件绝对路径"+file.getAbsolutePath());
//读取文件的父级路径
System.out.println("读取文件的父级路径"+new File(file.getAbsolutePath()).getParent());
//读取文件的大小
System.out.println("读取文件的大小"+file.length()+"byte");//字节
//判断文件是否被隐藏
System.out.println("判断文件是否被隐藏"+file.isHidden());
//判断文件是否可读
System.out.println("判断文件是否可读"+file.canRead());
//判断文件是否可写
System.out.println("判断文件是否可写"+file.canWrite());
//判断文件是否为文件夹
System.out.println("判断文件是否为文件夹"+file.isDirectory());
}
}
文件属性的设置
file.setWritable(true);
//将文件设定为可读
file.setReadable(true);
//将文件设定为只读
file.setReadOnly();
遍历文件夹
package com.jingchenyong.io;
import java.io.File;
public class FileTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
printFiles(new File("C:/Users/jingchenyong/Desktop/desktop"),1);
//绝对路径
//printFiles(new File("H:/Users/jingchenyong/Workspaces/MyEclipse 8.6/Test"));
//相对路径
//printFiles(new File("../Test"),1);
}
public static void printFiles(File dir,int tab){
if(dir.isDirectory()){
File next[]=dir.listFiles();//返回的file对象的数组的数组
for(int i=0;i<next.length;i++){
for(int j=0;j<tab;j++){
System.out.print("|--");
}
System.out.println(next[i].getName());
if(next[i].isDirectory()){
printFiles(next[i],tab+1);
}
}
}
}
}
文件的简单读写
package com.jingchenyong.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class FileTest3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//读
File file=new File("text.txt");//位于工程目录下
if(file.exists()){
System.err.println("exist");
try {
FileInputStream fis=new FileInputStream(file);//获取文件的输入流(字节流)
InputStreamReader isr=new InputStreamReader(fis,"utf-8");//(字符流)
BufferedReader br=new BufferedReader(isr);//带有缓冲的
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//写
try {
File newfile=new File("next.txt");
FileOutputStream fos=new FileOutputStream(newfile);
OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
BufferedWriter bw=new BufferedWriter(osw);
//覆盖写入
bw.write("jingchenyong1\r\n");
bw.write("jingchenyong2\r\n");
bw.write("jingchenyong3\r\n");
bw.write("jingchenyong4\r\n");
bw.write("jingchenyong5\r\n");
bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}