I/O流之文件流

1.文件操作类 File

1.public File(String pathname)//给定一个要操作文件的完整路径
2.public File(File parent,String child)//给定要操作文件的父路径和子路径名称

 取得了文件之后需要对文件进行操作,会用到以下方法

1.public boolean creatNewFile()throws IOException//创建文件
2.public boolean delete()//删除文件
3.public boolean exists()//判断给定的路径是否存在

 文件的基本操作

public class Test {
     public static void main(String[] args) throws IOException {
	 File f=new File("D:"+File.separator+"demo.txt");//文件的路径
	 if(f.exists()) {
		 f.delete();
	 }else {
			 f.createNewFile();//需要完全控制数据盘
		 }
}
}

如果想要创建带目录的文件,需要使用以下方法

1.public File getParentFile()//找到一个指定路径的父路径
2.public boolean mkdirs()//创建指定目录

 举例

public class Test {
     public static void main(String[] args) throws IOException {
     //文件路径
     //D:/hello/my/test/demo.txt
	 File f=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator+"demo.txt");//文件的路径
	 if(!f.getParentFile().exists()) {//如果父路径不存在
		 f.getParentFile().mkdirs();//创建目录
	 }
	 if(f.exists()){   //如果文件存在
		 f.delete();//删除文件
	 }else {
		 f.createNewFile();//创建新文件
	 }
}
}

 File类的其它方法

1.public String getName()//取得文件名称
2.public boolean isDirectory()//判断给定的路径是否是文件夹
3.public boolean isFile()//判断给定的路径是否是文件
4.public booleaan isHidden()//判断是否隐藏
5.public long lastModified()//文件最后一次修改日期
6.public long length()//取得文件大小,以字节为单位返回
7.public boolean renameTo(File dest)//为文件重命名
8.public File[] listFile()//将目录中所有文件以File对象数组的方式输出

 取得文件信息

package 数据流;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
     public static void main(String[] args) throws IOException {
     //文件路径
     //D:/hello/my/test/demo.txt
	 File f=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator+"demo.txt");//文件的路径
	 if(!f.getParentFile().exists()) {//如果父路径不存在
		 f.getParentFile().mkdirs();//创建目录
	 }
	 if(f.exists()) {
		 System.out.println("文件名称:"+f.getName());//获得文件名称
		 System.out.println(f.getName()+(f.isDirectory()?"是一个目录":"不是一个目录"));
		 System.out.println(f.getName()+(f.isFile()?"是一个文件":"不是一个文件"));
		 System.out.println(f.getName()+(f.isHidden()?"是一个隐藏文件":"不是一个隐藏文件"));
		 System.out.println("最后一次更改的时间"+new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date(f.lastModified())));
		 
	 }else {
		 f.createNewFile();
	 }
}
}

 为文件重命名

 File newf=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator+"caizhen.txt");//指定一个文件路径
f.renameTo(newf);//使用renameTo()方法将原本的文件名称更改为新的文件名称

 列出目录内容

package 数据流;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
     public static void main(String[] args) throws IOException {
     //文件路径
     //D:/hello/my/test/demo.txt
	 File f=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator);//目录的路径
	 if(!f.isDirectory()) {//如果父路径不存在
		 System.out.println("不是目录");
	 }
	 if(f.exists()) {
         File result[]=f.listFiles();
         for(int x=0;x<result.length;x++) {
        	 System.out.println(result[x]);
         }
	 
	 } 
}
}

 这个方法只适用列出给定目录下的文件(目录内容)

列出指定目录下的全部内容

猜你喜欢

转载自www.cnblogs.com/cainame/p/10417640.html