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 的第二篇,主要讲述如何搭建故障转移集 ...
随机推荐
- dojox.grid.DataGrid
创建表格 <table data-dojo-type="dojox.grid.DataGrid" data-dojo-id="grid" style=&q ...
- 通过Mac远程调试iPhone/iPad上的网页(转)
我们知道在 Mac/PC 上的浏览器都有 Web 检查器这类的工具(如最著名的 Firebug)对前端开发进行调试,而在 iPhone/iPad 由于限于屏幕的大小和触摸屏的使用习惯,直接对网页调试非 ...
- WINDOWS Composer & PHPUnit 安装记录
Windows: 安装Composer: 下载了composer_setup.exe 运行之后提示和Xdebug冲突,在php.ini中注释掉php_xdebug.dll,再次运行.通过 compos ...
- python collections defaultdict
class_counts = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...
- js中对radio和checkbox是否选中的判断
一.js判断checkbox 例如:<div class="checkbox" style="width: 150px;"> <label&g ...
- services 文件
Services 文件列出了服务使用的标准端口号.可以向表中添加自己定义的项,来给自己的服务选择.(安装在Windows目录下的一个子目录中,取决于Windows版本) # Copyright (c) ...
- JQuery设置和去除disabled属性
//两种方法设置disabled属性 $('#areaSelect').attr("disabled",true); $('#areaSelect').attr("dis ...
- C学习笔记(八)字符输入输出和输入确认
缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区 ...
- chrome浏览器遇到的异常
昨天写了一个二进制输出图片的方法,发现在chrome浏览器里面出了异常: (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING 代码是这样写的: //直接输出 ...
- css\html布局及部分知识小分享~~~
近期发现和总结的知识跟大侠们分享,请大侠们多多评论指教一二? HTML 1.(1)body需设置页面默认字体大小 body{font-size:12px;} (2)IE6下png图片划过切换失效,建 ...