黑马程序员-------------IO流

------ Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
一、File
用来将文件或文件夹封装成对象。
创建方式:

方式一:      

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



二、什么是流?
用与设备之间数据的传输,数据以流的形式存在。
IO字节构架图

IO字符构架图:


一个流必有源和目的:主要有字节流和字符流
设备上的数据都是以字节的形式存储。所以操作数据的时候都可以考虑使用字节流,操作文本的时候使用字符流,占两个字节,操作起来比较的方便,转换流是字节和字符的桥梁,可以设定制定字符进行编码。还有就是一些功能流。
1、源---------读取
|------InputStream
|------Reader
目的--------写入
|--------OutputStream
|--------Writer
2、明确是否是文本
                 是
|------Reader、Writer
|------InputStream、OutputStream
3、是否高效使用缓冲(使用装饰模式)
BufferedInputStream、BufferedOutputStream
BufferedReader、 BufferedWriter
4.明确设备:
源设备:|------内存:ByteArrayInputStream
|------硬盘:FileReader
|------键盘:System.in (标准: BufferedReader br=new BufferedReader(new InputStreamReader(System.in));)
             目的设备:|------内存:ByteArrayOutputStream
|------硬盘:FileWriter
|------控制台:System.out  (Print)


应用实例:复制媒体文件

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值