输入输出流——字节流部分
2016-02-06 18:19
441 查看
在文件之后 比较合适出现的 就是 关于 流的操作了。
猜想关于数据的写入和写出操作,本质上是01形式的二进制数,而这些数据的排列方式是不能乱套的。他们是一个有序的整体,这队长长的用于表示一些内容的东西就称作流了。在这里面用Stream 来标识。
java.io 包中定义了多个流类,用来实现输入/输出功能,以不同的角度可以分类为:
1、按数据流的方向分为输入和输出。
2、按数据处理的单位分为字节流和字符流。
3、按功能不同分为节点流和处理流。
运行结果:
used to try the first demo of inputStream.
//这个编辑器还真是高级,自动抹掉了我后面茫茫多的空格。事实上因为 我们 创建了一个 长达 1024个 bite位的数组。所以。有多少读多少,没有填空,补足。这样就会有弊端,我们要存储的数据的内容的大小不确定,那么这个 byte给多大合适呢?
方案一:
流作为资源的一种使用之后是要关闭的。
这种抽取形式,还是比较重要的。
方式二:
写出:
字节流拷贝文件:
猜想关于数据的写入和写出操作,本质上是01形式的二进制数,而这些数据的排列方式是不能乱套的。他们是一个有序的整体,这队长长的用于表示一些内容的东西就称作流了。在这里面用Stream 来标识。
java.io 包中定义了多个流类,用来实现输入/输出功能,以不同的角度可以分类为:
1、按数据流的方向分为输入和输出。
2、按数据处理的单位分为字节流和字符流。
3、按功能不同分为节点流和处理流。
package IOPart; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class IOPart { public static void main(String[] args) { /** * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream. */ try { //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹 FileInputStream fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt")); byte[] contents = new byte[1024]; fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下 String result = new String(contents); System.out.println(result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
运行结果:
used to try the first demo of inputStream.
//这个编辑器还真是高级,自动抹掉了我后面茫茫多的空格。事实上因为 我们 创建了一个 长达 1024个 bite位的数组。所以。有多少读多少,没有填空,补足。这样就会有弊端,我们要存储的数据的内容的大小不确定,那么这个 byte给多大合适呢?
方案一:
package IOPart; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class IOPart { public static void main(String[] args) { /** * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream. */ try { //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹 FileInputStream fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt")); byte[] contents = new byte[fileInputStream.available()]; fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下 String result = new String(contents); System.out.println(result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
流作为资源的一种使用之后是要关闭的。
package IOPart; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class IOPart { public static void main(String[] args) { /** * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream. */ FileInputStream fileInputStream=null; try { //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹 fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt")); byte[] contents = new byte[fileInputStream.available()]; fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下 String result = new String(contents); System.out.println(result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(fileInputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
这种抽取形式,还是比较重要的。
方式二:
package IOPart; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class IOPart { public static void main(String[] args) { IOPart ioPart = new IOPart(); ioPart.method2(); } private void method2() { FileInputStream fileInputStream = null; try { fileInputStream= new FileInputStream(new File("f:\\tryFile\\SendPart.java")); byte[] contents = new byte[1024]; int length = 0; while((length=fileInputStream.read(contents))!=-1){ System.out.println(new String(contents,0,length)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(fileInputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private void method1() { /** * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream. */ FileInputStream fileInputStream=null; try { //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹 fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt")); byte[] contents = new byte[fileInputStream.available()]; fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下 String result = new String(contents); System.out.println(result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(fileInputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
写出:
package IOPart; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class OutputDemo1 { public static void main(String[] args) { FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(new File("F:\\tryFile\\output.txt")); String contentString = new String("lifei"); fileOutputStream.write(contentString.getBytes()); System.out.println("写出成功"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fileOutputStream!=null){ try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
字节流拷贝文件:
package IOPart; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFile1 { public static void main(String[] args) { method2(); } private static void method2() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try {//C:\\Users\\Administrator\\Desktop\\震撼世界的演讲《梦想》 标清(270P).qlv //fileInputStream = new FileInputStream(new File("f:/tryFile/SendPart.java")); /** * 这里面还可以复制的有 视频文件,音频文件,其实就是 各种文件了,只要把要拷贝的文件,放在FileInputStream里面就可以了。当然针对字符 还有专门为字符打造的操作流对象。 */ fileInputStream = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\震撼世界的演讲《梦想》 标清(270P).qlv")); fileOutputStream = new FileOutputStream(new File("f:/tryFile/copy.qlv")); byte[] contents = new byte[1024]; int length = 0; while((length=fileInputStream.read(contents))!=-1){ fileOutputStream.write(contents, 0, length); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fileInputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fileOutputStream!=null){ try { fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } System.out.println("复制完成"); } private static void method1() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(new File("f:/tryFile/SendPart.java")); fileOutputStream = new FileOutputStream(new File("f:/tryFile/copy.txt")); byte[] contents = new byte[fileInputStream.available()]; fileInputStream.read(contents); fileOutputStream.write(contents); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fileInputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fileOutputStream!=null){ try { fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } System.out.println("复制完成"); } }
相关文章推荐
- Combination Sum II 题解
- BZOJ 2726: [SDOI2012]任务安排( dp + cdq分治 )
- Selenium之偷懒教程
- 你妹都看得懂的手机网游制作教程(第21篇)游戏黑屏了,别急我们来调试代码
- Oracle 安装报错 [INS-06101] IP address of localhost could not be determined
- HDU-1994-利息计算
- android数据存储的四种方案(一)
- sizeof()的理解
- java之Set源代码浅析
- UVA1220 - Party at Hali-Bula
- C# 基础加强(四) 静态类、静态方法
- 使用Behavior Designer插件简单制作NPC AI
- IOS开发的哪些异常之异常断点
- ThinkPHP - 自动创建 + 自动验证 + 自动完成
- k-d tree算法例子
- HIBERNATE的N+1查询问题
- poj 2528 Mayor's posters(线段树+离散化)
- 搜索(枚举)3
- 搜索(枚举)2
- 二叉树