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

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

一、File类

1、定义:表示文件和目录路径的抽象表示形式。
2、作用:实现文件的创建、删除、重命名、得到路径、创建时间,是唯一与文件本身有关的操作类
3、常用方法:
public static final String separator----- "\"
public static final String pathSeparator-----";"
public File(String pathname)----
构造File类实例,要传入路径
public boolean createNewFile()throws IOException----创建新文件
public boolean delete()----删除文件
public String getParent()----得到文件的上一级路径
public boolean isDirectory----判断给定路径是否是文件夹
public String[] list()----列出文件夹中的文件
public String[] listFile()----列出文件夹中的所有文件

public boolean mkdir()----创建新的文件夹

4、经典案例---查找文件

public class FileDemo {

	public static void main(String[] args) {
		String extname=".jpg";
		String oldfile="F:\\img";
		//"F:"+File.separator+"img"+File.separator+"1.jpg"
		File oldFile=new File(oldfile);
		findFile(oldFile,extname);
	}
	public static void findFile(File f,String e){
		if(f.isDirectory()){
			File[] fs=f.listFiles();
			for (File file : fs) {
				findFile(file,e);
			}
		}else{
			String path=f.getPath().toLowerCase();
			if(path.endsWith(e)){
				System.out.println(path);
			}
		}
	}

}
二、IO流
IO流的分类:根据处理数据类型分为:字符流和字节流
                根据数据流向不同:输入流和输出流。
1、字节流
1)、FileOutputStream(File file/File file boolean append)(字节输出流)
2)、FileInputStream(File file)(字节输入流)
注意关闭流。.close()
2、字符流
1)、FileWriter(File file/File file boolean append)(字符输出流)
2)、FileReader(File file)(字符输入流)
注意关闭流。.close()

3、经典案例--拷贝文件

public class IoDemo {

	public static void main(String[] args) {
		String oldfile="F:\\img\\1.jpg";
		String newfile="D:\\";
		String extname=oldfile.substring(oldfile.lastIndexOf("."));
		File oldFile=new File(oldfile);
		File newFile=new File(newfile+"\\1"+extname);
		System.out.println("运行。。。。");
		CopyFile(oldFile,newFile);
	}
	public static void CopyFile(File o,File n){
		FileInputStream fis=null;
		FileOutputStream fos=null;
		try {
			 fis=new FileInputStream(o);
			 fos=new FileOutputStream(n);
			int len=-1;
			byte[] b=new byte[512];
			while((len=fis.read(b))!=-1){
				fos.write(b, 0, len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				fis.close();
				fos.close();
				System.out.println("success");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
3、转换流
1)定义:可以将一根字节流转换为字符流,也可以反过来
2)OutputStreamWriter
  InputStreamReader
4、缓冲流
1)、好处:能够更高效的读写信息。
2)、 BufferedInputSream
        BufferedOutputStream
        BufferedWriter
        BufferedReader

3)、写法如:OutputStreamWriter osw=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File(String s))));
注意关闭和刷新.close()
    .flush()
5、打印流
1)、PrintStream PrintWriter
6、对象流
1)、ObjectOutputStream
        ObjectInputStream
2)、对象序列化:将一个对象转化成二进制的byte流
将对象保存在文件上的操作称为对象的序列化操作
将对象从文件中恢复称为反序列化操作
序列化一组对象可采用:对象数组的形式。
transient关键字,声明的属性属性不会被序列化
7、数据流
1)、DataInputStream
        DataOutputStream
8、字符串流
1)、StringReader
       StringWriter
2)、StringWriter关闭无效
他们的关系:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值