Java中关于File的操作

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();
		}
	}

}


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Henry_Jing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值