方式一:
File f =new File("a.txt");
将a.txt封装成File对象。可以将已有的和未出现的文件或者文件夹封装成对象。
方式二:
File f2=newFile("c:\\abc","b.txt");
将文件所在目录路径和文件一起传入,指定文件路径。
方式三:
File d=new File("c:\\abc");
File f3=new File(d,"c.txt");
将文件目录路径封装成对象。再创建文件对象。降低了文件于父目录的关联性。
小知识:
File.separator表示目录分隔符,可以跨平台使用。
File类中常用方法:
·创建:
boolean createNewFile();
判断:
boolean exists();//文件是否存在
boolean isFile();//是否是文件
boolean isDirectory();//是否是文件夹
boolean isHidden();//是否是隐藏文件
boolean isAbsolute();//文件是否是绝对路径
获取:
String getName();//获取文件名
String getPath(); //获取文件的相对路径(即创建的对象传入的参数是什么就获取到什么)
String getParent();
//获取文件父目录。返回的是绝对路径中的父目录。如果获取的是相对路径,返回null。如果相对路径中有上一层目录,那么该目录就是返回结果
String getAbsolutePath();//获取文件的绝对路径
long lastModified();//返回文件最后一次被修改的时间
long length();//返回文件长度
过滤
File[] listFiles();//返回一个抽象路径名数组,获取当前文件夹下的所有文件和文件夹
File[] ListFiles(FilenameFilterfilter);//返回抽象路径名数组,获取目录中满足指定过滤器的文件或目录。
FilenameFilter:文件名过滤器,是一个接口,其中包含一个方法,accept(Filedir,String name),返回的是boolean型,对不符合条件的文件过滤掉。
package cn.itheima;
import java.io.File;
import java.io.FilenameFilter;
public class FileFilterDemo {
/**
* @param args
*/
public static void main(String[] args) {
File dir = new File("C:\\MyEclipse 8.5\\day11\\src");
//过滤
GetFileDir(dir);
}
private static void GetFileDir(File dir) {
FilenameFilter myfile = new MyFileFilter(".java");
// myfile = new MyFileExtension(".java");
File[] files = dir.listFiles(myfile);
for(File file : files){
if(file.isDirectory()){
GetFileDir(file);
} else {
System.out.println(file);
}
}
}
}
class MyFileFilter implements FilenameFilter {
private String name ;
public MyFileFilter(String name) {
this.name = name;
}
public boolean accept(File dir, String name) {
return name.endsWith(this.name)||dir.isDirectory();
}
}
package cn.itheima;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class CopeFile {
/**
* @param args
* 复制文件夹
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 1.明确源:
File file = new File ("F:\\456.png");
if (!file.exists()){
throw new RuntimeException("文件不存在");
}
InputStream in = new FileInputStream (file);
// 2.明确目的
OutputStream out = new FileOutputStream("E:\\66.jpg");
// 3.是否使用高效
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(out);
// 4.读取操作
byte[] buf = new byte[1024];
int len ;
while ((len = bis.read(buf)) != -1){
bos.write(buf, 0, len);
// bos.flush();
}
// 5.关闭资源
bis.close();
bos.close();
}
}
复制文件的时候,为了提高效率,使用了缓冲流,但是可以用文件通道(FileChannel)来实现文件复制竟然比用老方法快。
/**
* 使用文件通道的方式复制文件
*
* @param s
* 源文件
* @param t
* 复制到的新文件
*/
public void fileChannelCopy(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();//得到对应的文件通道
out = fo.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
装饰模式:
public class MainDemo {
/**
* @param args
*/
public static void main(String[] args) {
Student stu = new Student();
NewStudents ns = new NewStudents(stu);
ns.chi();
}
}
class Person {
public void chi() {
System.out.println("吃饭");
}
}
class Student extends Person {
public void chi(){
super.chi();
}
}
class NewStudents extends Person{
private Person p;
public NewStudents(Person p){
this.p = p;
}
public void chi(){
System.out.println("晚饭");
p.chi();
System.out.println("睡觉");
}
}