day08(File类 ,字节流)

File类

  构造方法

    File(String path);

    FIle(String parent, String child);

       File(File parent, String child)

  功能:

    创建功能    boolean createNewFile();

              删除功能   boolean delect();

        判断功能   返回值为boolean      

         判断是否为绝对路径  isAbsolute();

         判断是否为文件夹     isdirectory();

         判断是否为文件        isFile();

    获取功能

        //获取文件绝对路径

       File getAbsoluteFile();

       String  getAbsolutePath();

        //获取传递路径

          String getPath();

        //获取父路径

        String  getParent();

        //获取文件大小

          long  length();

public class FileTest {
	public static void main(String[] args) throws IOException {
		File file1=new File("b.txt");
		long l = file1.length();
		long m = file1.lastModified();
		Date d=new Date(m);
		System.out.println(d.toLocaleString());
		System.out.println(l);
	}
}

  

输出结果:
2017-9-2 13:39:34
3

  

        //获取文件最后一次的修改时间

          long lastModified();  返回值为毫秒值

        

public class FileTest {
	public static void main(String[] args) throws IOException {
		File file1=new File("b.txt");
		File file2=new File("a\b.txt");
		File file3=new File("C:\a.txt");
		//获取绝对路径
		System.out.println("获取绝对路径");
		File absoluteFile1= file1.getAbsoluteFile();
		File absoluteFile2 = file2.getAbsoluteFile();
		File absoluteFile3 = file3.getAbsoluteFile();
		String absolutePath1 = file1.getAbsolutePath();
		String absolutePath2 = file2.getAbsolutePath();
		String absolutePath3 = file3.getAbsolutePath();
		System.out.println(absoluteFile1);
		System.out.println(absoluteFile2);
		System.out.println(absoluteFile3);
		System.out.println(absolutePath1);
		System.out.println(absolutePath2);
		System.out.println(absolutePath3);
		System.out.println("------------");
		//获取传递路径
		System.out.println("获取传递路径");
		String path1 = file1.getPath();
		String path2 = file2.getPath();
		String path3 = file3.getPath();
		System.out.println(path1);
		System.out.println(path2);
		System.out.println(path3);
		System.out.println("------------");
		//获取父路径(按照传递进去的文件路径获取的)
		System.out.println("获取父路径(按照传递进去的文件路径获取的)");
		String parent1 = file1.getParent();
		String parent2 = file2.getParent();
		String parent3 = file3.getParent();
		System.out.println(parent1);
		System.out.println(parent2);
		System.out.println(parent3);
	}
}

  

输出结果:
获取绝对路径
D:javamyeclipseITEMJYBDemo.txt
D:javamyeclipseITEMJYBDemoa.txt
C:a.txt
D:javamyeclipseITEMJYBDemo.txt
D:javamyeclipseITEMJYBDemoa.txt
C:a.txt
------------
获取传递路径
b.txt
a.txt
C:a.txt
------------
获取父路径(按照传递进去的文件路径获取的)
null
a
C:

  

字节流   (和字符流的用法一样,字符流只能读文本文件。)

    输出字节流(写数据) OutputStream(抽象类)----FileOutputStream(实现类)

    输入字节流(读数据) InputStream(抽象类)-----FileInputStream(实现类)

      eg:  复制文件

public class FileTest {
	public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("b.txt"); //读文件
		FileOutputStream fos=new FileOutputStream("a\b.txt");//写文件
		int len;
		byte[] bt=new byte[1024*1024*5];//5M
		while ((len=fis.read(bt))!=-1) {
			fos.write(bt,0,len);
		}
		fis.close();
		fos.close();
	}
}

      eg02  复制文件夹 

   

public class FileCopyNew {
	public static void main(String[] args) throws IOException {
		String src="D:\java\myeclipse\ITEM\JYBDemo\a";//d:\str  要复制的文件夹路径
		String dsc="D:\java\myeclipse\ITEM\JYBDemo\aa";//c:\g   要复制到的文件夹路径
		copy(dsc,src);
	}

	private static void copy(String dsc, String src) throws IOException {
		File df=new File(dsc);//目的
		File sf=new File(src);//源文件夹
		String s=df.getAbsolutePath()+"\"+sf.getAbsolutePath().substring(sf.getParent().length());
		File file=new File(s);
		if (sf.isDirectory()) {
			file.mkdir();//给目的文件夹创建文件夹
			File[] files = sf.listFiles();
			for (File file2 : files) {
				copy(s, file2.toString());
			}
		}else{
			//file.createNewFile();
			chuanShu(sf.getAbsolutePath(),file.toString());
		}
		
	}
	private static void chuanShu(String des,String src) throws  IOException {
		FileInputStream fis=new FileInputStream(des);
		FileOutputStream fos=new FileOutputStream(src);
		int len;
		byte[] b=new byte[1024*1024*50];
		while ((len=fis.read(b))!=-1) {
			fos.write(b,0,len);//一次读一个字节数组
		}	
		fis.close();
		fos.close();
	}

}

  

 过滤器

public class MyFileFilter implements FileFilter{//自定义过滤器

	@Override
	public boolean accept(File f) {
		if (f.getName().endsWith(".java")) {
			return true;
		}
		if (f.isDirectory()) {
			return true;
		}
		return false;
	}
}

  

public class FileTest {
	public static void main(String[] args) throws IOException {
		File f=new File("a");
		method(f);
	}
	private static void method(File f) {
		File[] Files = f.listFiles(new MyFileFilter());
		
		for (File file : Files) {
			if (file.isDirectory()) {
				method(file);
			}else{
			System.out.println(file);}//输出文件类型为java文件
		}
	}
}

  

  

原文地址:https://www.cnblogs.com/fjkgrbk/p/fileOperator.html