1概念 IO流用来处理设备之间的数据传输 java对数据的操作是通过流的方式 java用于操作流的类都在IO包中 流按流向分为俩种 字节流:字符流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的 字符流:字符只能操作纯字符数据。比较方便。
public class demo_FileOutputStream { /** *FileOutputStrean在创建对象的时候是如果没有这个文件会帮我创建出来,如果有这个文件就会先将文件清空 */ public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream(“yyy.txt”); //创建字节输出流对象,如果没有就自己写 //fos.write(97); //虽然写出的是一个int书。但是到文件上是一个字节,会自动去除前三个8位数 //fos.write(98); //fos.write(99); fos.write(100); fos.close(); } }
2IO流常用父类 字节流的抽象父类: inputsream outputstruts 字符流的抽象父类 reader writer 3IO程序书写 使用前,导入IO包中的类 使用时、进行IO异常处理 使用后释放资源 IO流(FileInputStream) read()
package com.heima.stream; import java.io.FileInpuStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; package com.heima.stream; public class Demo3_Cpy { /** * @param args */ public static void main(String[] args) { FileInputStream fis = new FileInputStream("双元。jpg"); //创建输入流对象,关联双元。jpg FileInputStream fos = new FileOutputStream("copy.jpg"); //创建输出流对象。关联copy.jpg int b; while((b = fis.read()) != -1) { //在不断的读取每一个字节 fos.write(b); //将每一个字节写出 } fis.close(); //释放资源 fos.close(); }字节数组拷贝之available()方法 A:案例演示 int read(byte[] b):一次读取一个字节数组 write(byte[]b):一次写出一个字节数组 available()获取读的文件所有的字节个数 弊端:有可能会内存溢出
public class Demo3_Cpy { public static void main(String[] args) throws IOException { //拷贝。不推荐使用因为有可能导致内存溢出 FileInputStream fis = new FileInputStream(“致青春.mp3”); //创建输入流对象,关联双元。jpg FileInputStream fos = new FileOutputStream(“copy.mp3”); //创建输出流对象。关联copy.jpg //int len = fis.available(); //System.out.println(len);
byte[] arr = new byte[fis.available()]; //创建与文件一样大小的字节数组 fis.read(arr); // 将文字上的字节读取到内存中 fos.write(arr); //将字节数组中的数据写文件上到 fis.close(); fos.close(); }} 4定义小数组 write(byte[]b) write(byte[]b,int off,int len)写出有效的字节数
package com.heima.stream; import java.io.FileInputStream; public class Demo_ArrayCopy { /** *@psrsm args *第三种拷贝 *定义小数组 */ public static void main(String[] args) throws IOException { FileInputstream fis = new FileInputStream("xxx.txt"); FileOutputstream fos = new FileOutputStream("yyy.txt"); byte[] arr = new byte[1024 * 8]; int len; while((len = fis.read(arr)) != -1){ //如果忘记加arr,返回的就不是读取的字节个数 fos.write(arr,0,len); } fis.close(); fos.close(); } public static void main(String[] args) { FileInputstream fis = new FileInputStream("xxx.txt"); byte[] arr = mew byte[2]; int a = fis.read(arr); // 将文件上的字节读取到字节数组中 System.out.println(a); //读取到有效字节个数 fore(byte b : arr) { //第一次读取到文件上的a和b System.out.println(b); } System.out.println("--------------"); int b = fis.read(arr); fore(byte c : arr); System.out.println(c); fore(byte b : arr) { System.out.println(b); } fis.close(); } }5字节流读取中文
读
package com.heima.stream; import java.io.FileInputStream; public class Demo_Chinese { public static void main(String[] args) { FileInputstream fis = new FileInputStream("yyy.txt"); byte[] arr = new byte[3]; int len; while((len = fis.read(arr)) 1= -1) { System.out.println(new String(arr,0,len)); //字节流读取中文可能会读到半个中文;造成乱码 } fis.close(); } }写
package com.heima.stream; import java.io.FileInputStream; public class Demo_Chinese { public static void main(String[] args) throws IOException { FileOutputstream fos = new FileOutputStream("zzz.txt"); fos.write("我读书少。你不要骗我".getBytes()); fos.write("\r\n".getBytes()); fos.close(): } }1.7处理异常
public class Demo_TryFinally { public static void main(String[] args) throws IOException { try( FileInputstream fis = new FileInputStream("xxx.txt"); FileOutputstream fos = new FileOutputStream("yy.txt"); ){ int b; while((b = fis.read()) != -1) { fos.write(b); } } }