Day20_IO第二天
1、IO体系总图

2、IO体系——字节流

3、表达式解释
4、标准代码JDK1.6之前(掌握)
/*** 将source拷贝到dest中 source:源文件 dest:目标文件*/public static void copy1(String source, String dest) {// 输入流FileInputStream fis = null;// 输出流FileOutputStream fos = null;try {fis = new FileInputStream(source);fos = new FileOutputStream(dest);// 读取到的数据int data;while ((data = fis.read()) != -1) {fos.write(data);}} catch (Exception e) {e.printStackTrace();} finally{if(fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}if(fos != null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
public static void copy2(String source, String dest) {// 输入流FileInputStream fis = null;// 输出流FileOutputStream fos = null;try {fis = new FileInputStream(source);fos = new FileOutputStream(dest);//bys里存的是读取到的数据byte[] bys= new byte[1024];//读取了几个数据int len;while((len=fis.read(bys)) != -1){fos.write(bys, 0, len);}} catch (Exception e) {e.printStackTrace();}finally{if(fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}if(fos != null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
public void copy3(String source, String dest) throws Exception {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(new FileInputStream(source));bos = new BufferedOutputStream(new FileOutputStream(dest));byte[] bys = new byte[1024*20];int len;while((len=bis.read(bys)) != -1){bos.write(bys, 0, len);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}}}
5、标准代码JDK1.7(掌握)
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;public class CopyDemo {public static void main(String[] args) {/*格式:try(//声明输入输出流){//拷贝文件}catch(Exception e){//异常处理}*/try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(".project"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));){//存储读取到的数据byte[] data = new byte[1024*8];//存储读取到的数据的长度int len = -1;//通过read方法将数据读取到data数组中,读取了几个数据呢?读取了len个数据while((len=bis.read(data)) !=-1){bos.write(data,0,len);}}catch(Exception e){e.printStackTrace();}}}
5、案例(掌握)
6、案例代码(掌握)
package com.heima.test;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test1 {/*** @param args* @throws IOException* 将写出的字节异或上一个数,这个数就是密钥,解密的时候再次异或就可以了*/public static void main(String[] args) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));int b;while((b = bis.read()) != -1) {bos.write(b ^ 123);}bis.close();bos.close();}}
package com.heima.test;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Scanner;public class Test2 {/*** 在控制台录入文件的路径,将文件拷贝到当前项目下** 分析:** 1,定义方法对键盘录入的路径进行判断,如果是文件就返回* 2,在主方法中接收该文件* 3,读和写该文件* @throws IOException*/public static void main(String[] args) throws IOException {File file = getFile(); //获取文件BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));int b;while((b = bis.read()) != -1) {bos.write(b);}bis.close();bos.close();}/** 定义一个方法获取键盘录入的文件路径,并封装成File对象返回* 1,返回值类型File* 2,参数列表无*/public static File getFile() {Scanner sc = new Scanner(System.in); //创建键盘录入对象System.out.println("请输入一个文件的路径:");while(true) {String line = sc.nextLine(); //接收键盘录入的路径File file = new File(line); //封装成File对象,并对其进行判断if(!file.exists()) {System.out.println("您录入的文件路径不存在,请重新录入:");}else if(file.isDirectory()) {System.out.println("您录入的是文件夹路径,请重新录入:");}else {return file;}}}}
package com.heima.test;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Scanner;public class Test3 {/*** 将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出** 分析:* 1,创建键盘录入对象* 2,创建输出流对象,关联text.txt文件* 3,定义无限循环* 4,遇到quit退出循环* 5,如果不quit,就将内容写出* 6,关闭流* @throws IOException*/public static void main(String[] args) throws IOException {//1,创建键盘录入对象Scanner sc = new Scanner(System.in);//2,创建输出流对象,关联text.txt文件FileOutputStream fos = new FileOutputStream("text.txt");System.out.println("请输入数据:");//3,定义无限循环while(true) {String line = sc.nextLine(); //将键盘录入的数据存储在line中//4,遇到quit退出循环if("quit".equals(line)) {break;}//5,如果不quit,就将内容写出fos.write(line.getBytes()); //字符串写出必须转换成字节数组fos.write("\r\n".getBytes());}//6,关闭流fos.close();}}
public class Demo2 {public static void main(String[] args)throws Exception {Scanner sc = new Scanner(System.in);String line;FileOutputStream fos = new FileOutputStream("record.txt");//如果输入的不是quite就将用户输入的信息写入到文本文件里面while(!(line=sc.nextLine()) .equals("quite")){fos.write(line.getBytes());//System.getProperty("line.separator")获取换行符fos.write(System.getProperty("line.separator").getBytes());}fos.close();}}
7、今天必须掌握的内容,面试题,笔试题。
int data;while ((data = fis.read()) != -1) {fos.write(data);}
byte[] data = new byte[1024*8];int len = -1;while((len=bis.read(data)) !=-1){bos.write(data,0,len);}
flush用来刷新缓冲区的,刷新后可以再次写出,即flush后流仍然可以使用close用来关闭流释放资源的的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出,即不能再使用该流
Day20_IO第二天的更多相关文章
- 读书笔记:JavaScript DOM 编程艺术(第二版)
读完还是能学到很多的基础知识,这里记录下,方便回顾与及时查阅. 内容也有自己的一些补充. JavaScript DOM 编程艺术(第二版) 1.JavaScript简史 JavaScript由Nets ...
- [ 高并发]Java高并发编程系列第二篇--线程同步
高并发,听起来高大上的一个词汇,在身处于互联网潮的社会大趋势下,高并发赋予了更多的传奇色彩.首先,我们可以看到很多招聘中,会提到有高并发项目者优先.高并发,意味着,你的前雇主,有很大的业务层面的需求, ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
- 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- Entity Framework教程(第二版)
源起 很多年前刚毕业那阵写过一篇关于Entity Framework的文章,没发首页却得到100+的推荐.可能是当时Entity Framework刚刚发布介绍EF的文章比较少.一晃这么多年过去了,E ...
- [C#] 软硬结合第二篇——酷我音乐盒的逆天玩法
1.灵感来源: LZ是纯宅男,一天从早上8:00起一直要呆在电脑旁到晚上12:00左右吧~平时也没人来闲聊几句,刷空间暑假也没啥动态,听音乐吧...~有些确实不好听,于是就不得不打断手头的工作去点击下 ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
- 菜鸟Python学习笔记第二天:关于Python黑客。
2016年1月5日 星期四 天气:还好 一直不知道自己为什么要去学Python,其实Python能做到的Java都可以做到,Python有的有点Java也有,而且Java还是必修课,可是就是不愿意去学 ...
- (转)从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
原文地址: http://www.cnblogs.com/lyhabc/p/4682028.html 这一篇是从0开始搭建SQL Server AlwaysOn 的第二篇,主要讲述如何搭建故障转移集 ...
随机推荐
- php多维数组去除空元素
在php中去除数组中的空值可以使用array_filter() 这个函数 但是这个函数只能对一维数组起作用,一旦需要对多维数组去空就不行了,而且去除的空也包括(int)0,(string)0,使用起来 ...
- Http 状态码对照表
1xx 消息 1. 100 Continue 2. 101 Switching Protocol 3. 102 Processing 2xx 成功 1. 200 OK ...
- touch穿透
出现穿透的前提:上层绑定touch事件,下层绑定click事件. 解决方案:touchend绑定: e.preventDefault();$(下层).css("pointer-events& ...
- package
1.设计package原因 理解基目录的概念,思考jre加载class的顺序,如果没有package会怎么样?有了之后又是怎么样..? 主要:确保类名的唯一性. 次要:方便组织代码 2.怎样访问\导入 ...
- Pycharm使用问题# Interpreter设置
Pycharm可以迅速更换interpreter版本. 在菜单栏选择File-Settings打开Settings设置对话框,选择展开Interpreter选项: 使用列表右侧的+和—即可增加和删除I ...
- 【matlab】查看程序运行时间
程序开头 profile on 结尾 profile viewer 然后就会很贴心滴出现下面的界面,可以从中展开,查看每段运行的时间
- zabbix3.0.4 部署之八 (zabbix3.0.4 微信报警)
[root@sv-zabbix ~]# cat /usr/local/zabbix/share/zabbix/alertscripts/weixin.py #!/usr/bin/env python# ...
- Win7精简成功后的总结
vsax 发表于 2014-7-20 20:59:43 https://www.itsk.com/forum.php?mod=viewthread&tid=333816&highli ...
- Gson手动序列化POJO(工具类)
gson2.7版本 只是简单的工具类(练习所用): package pojo; import javax.xml.bind.annotation.XmlSeeAlso; import com.goog ...
- 如何让popWindow显示在view上方
看了bilibili的客户端搜索按钮,很喜欢大爱!自己也想做个类似的(相似度 10% 哈哈) popWin的出现退出动画也可以自己设定,用过其方法setAnimationStyle(R.style.x ...