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 的第二篇,主要讲述如何搭建故障转移集 ...
随机推荐
- SerialChat与Arduino的配合使用
最近在开发过程中,用到了Arduino开发板以及其IDE:Arduino,这个IDE使用起来很方便,编码也很简单,但有一点美中不足的是Arduino只能输出数值,不能绘图,所以就用到了另外一款串口调试 ...
- 计算城市间的球面距离(C++实现)
#include<iostream> #include<string> #include<cmath> #include<iomanip> using ...
- 在网上看到的一篇文章关于js和php编码的
解决办法: 采用js对URL中的汉字进行escape编码. <a href="" onclick="window.open('product_list.php?p_ ...
- qq2440启动linux后出现错误提示request_module: runaway loop modprobe binfmt-464c
1.情景: 编译busybox时加了make CROSS_COMPILE=arm-linux-,但是还是出现了此情况! 2.解决方案如下: 配置busybox时,在配置中发现busybox setti ...
- cocos2dx 动画显示异常
最近遇到一个问题 好多cocostudio导出的动画 显示都会有异常 很明显的融合方式 把混合方式里面的 src 改成one dst 改成 one-src alpha 解决 后面附上同行的文章 浅显易 ...
- 【PL/SQL系列】Oracle存储过程使用动态SQL
Oracle存储过程相信大家都比较了解,下面就为您介绍Oracle存储过程使用动态SQL的方法,希望对您能够有所帮助. CREATE OR REPLACE PROCEDURE P_STAT_SCORE ...
- C语言细节——献给初学者(二)
C语言细节——献给初学者(二) 主题 循环运用+选择判断 C语言循环有for和while/do...while: 选择判断有:if...else和switch...case 在循环中需要注意搭配br ...
- oracle的存储过程和函数(PL/SQL)
czmmiao 存储过程概述 存储过程是子程序的一种类型,能够完成一些任务,作为schema对象存储于数据库.是一个有名字的PL/SQL代码块,支持接收或不接受参数,同时也支持参数输出.一个存储过程通 ...
- Excel 2013中单元格添加下拉列表的方法
使用Excel录入数据的时候我们通常使用下拉列表来限定输入的数据,这样录入数据就很少发生错误了.Excel 2013较以前的版本发生了很大的变化,那么在Excel 2013是如何添加下拉列表的呢? 下 ...
- Java 概述
一 Java 程序的种类 1)Java 小应用程序(Java Applet) — 在Web浏览器中运行(内嵌Java虚拟机) —特定标记 <APPLET CODE="HelloWorl ...