赞
踩
输入流 | 输出流 | |
---|---|---|
字节流 | InputStream | OutputStream |
字符流 | Reader | Writer |
写入数据的原理:Java程序IO流 – JVM – OS(Operate System) – OS调方法–写入成功.
1.总结构造方法
1.abstract void write(int b)
将指定的字节写入此输出流
2.void write(byte[] b)
将 b.length字节从指定的字节数组写入此输出流。
3.void write(byte[] b, int off, int len)
从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。
package Java学习.Java高级.IO.IO.OutputStream字节输出流; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * 写入数据 内存---------硬盘 * 一、步骤 * 1.创建FileOutputStream对象,抽象类,用多态 * * 2.写入数据 fileOutputStream.write(): 3 种方法。 * 1.abstract void write(int b) * 将指定的字节写入此输出流 * 2.void write(byte[] b) * 将 b.length字节从指定的字节数组写入此输出流。 * 3.void write(byte[] b, int off, int len) * 从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。 * * 3.刷新缓存:fileOutputStream.flush(); * * 4.关闭流,节省内存: fileOutputStream.close() */ public class Main { public static void main(String[] args) { String url = "D:\\Program Files\\JetBrains\\test1\\Lab\\src\\Java学习\\Java高级\\" + "IO\\IO\\OutputStream字节输出流\\"; FileOutputStream fileOutputStream = null; try { System.out.println("-----------单个字符测试----------"); fileOutputStream = new FileOutputStream(url+"单个字符测试.txt"); fileOutputStream.write('a'); fileOutputStream.flush(); System.out.println("-------字符数组---------"); System.out.println("如果字节是正数(0-127)显示的查询ASCII表"); System.out.println("如果是负数,就会第一个字节和第二个字节组成中文显示,查询默认码表(GBK)表"); FileOutputStream fileOutputStream1 = new FileOutputStream(url + "字符数组.txt"); byte[] bytes1 = {65,66,67,68,69,'\n'}; byte[] bytes2 = {-65,-66,-67,-68,-69}; fileOutputStream1.write(bytes1);//ABCDE fileOutputStream1.flush(); fileOutputStream1.write(bytes2); fileOutputStream1.flush(); fileOutputStream1.close(); System.out.println("------3.写入指定部分的字节----"); FileOutputStream fileOutputStream2 = new FileOutputStream(url + "写入指定部分的字节.txt"); fileOutputStream2.write(bytes1,1,2);//索引为1,长度为2 BC fileOutputStream2.flush(); fileOutputStream2.close(); System.out.println("-----4.字符串getByte()转字节实现写入字符串---------"); FileOutputStream fileOutputStream3 = new FileOutputStream(url + "字符串getByte()转字节实现写入字符串.txt"); String string = "半亩方糖"; byte[] bytes4 = string.getBytes(); fileOutputStream3.write(bytes4); fileOutputStream3.flush(); fileOutputStream3.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
输出:
InputStream inputStream = new FileInputStream(url);
url为文件的路径
new String(Byte[]) ``` #### 5. 代码 ```java package Java学习.Java高级.IO.IO.InputStream字节读取流; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; /** * 一、构造方法 * InputStream inputStream = new FileInputStream(url+"test1.txt"); * 二、常用方法 * 1.read();读取一个字节 * 2.int read(Byte[]);一次缓存多个字节,依次读取多个,建议1024*n * 3.close() 关闭读流。 * 返回读取的字节数量.int * 三、拓展把 字节数组变为字符串-- * 1. new String(byte[]) * 2.new String(byte[],index,length )重索引index 开始,转换长度 Length * */ public class Main { public static void main(String[] args) throws IOException { String url = "D:\\Program Files\\JetBrains\\test1\\Lab\\src\\Java学习\\Jav" + "a高级\\IO\\IO\\InputStream字节读取流\\"; System.out.println("-------1.read()单个字节读取.----------"); InputStream inputStream = new FileInputStream(url+"test1.txt"); int indexByte;//文件接受 while ((indexByte = inputStream.read())!=-1){ System.out.println("单个字节为: "+indexByte); } System.out.println("---------2.read(byte[])多个字节读取--------"); inputStream = new FileInputStream(url+"test1.txt"); byte[] bytes = new byte[2]; int length; length = inputStream.read(bytes);//记录读取的字节数量 System.out.println("读取的字节数: "+length); System.out.println("读取的字节数组:"+ Arrays.toString(bytes)); System.out.println("字节数组转化为字符串: "+new String(bytes)); System.out.println("=====2次读取,指针后移===="); length = inputStream.read(bytes);//记录读取的字节数量 System.out.println("读取的字节数: "+length); System.out.println("读取的字节数组:"+ Arrays.toString(bytes)); System.out.println("字节数组转化为字符串: "+new String(bytes)); System.out.println("-----3.循环连续读取字符数组------"); inputStream = new FileInputStream(url+"test1.txt"); bytes = new byte[1024]; length = 0; while ((length = inputStream.read(bytes))!=-1){ System.out.println("读取的字节数组转化为拥有长度的字符串: "+new String(bytes,0,length)); } } } ``` Run输出: -------1.read()单个字节读取.---------- 单个字节为: 97 单个字节为: 98 单个字节为: 99 单个字节为: 100 单个字节为: 101 ---------2.read(byte[])多个字节读取-------- 读取的字节数: 2 读取的字节数组:[97, 98] 字节数组转化为字符串: ab =====2次读取,指针后移==== 读取的字节数: 2 读取的字节数组:[99, 100] 字节数组转化为字符串: cd -----3.循环连续读取字符数组------ 读取的字节数组转化为拥有长度的字符串: abcde Process finished with exit code 0 ### 四、练习,字节读写完成图片的复制. ```java package Java学习.Java高级.IO.IO.InputStream字节读取流.练习读写复制文件; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 注意先关闭写的流,因为写完了,就一定读取完了,不然会数据丢失 */ public class Demo { public static void main(String[] args) throws IOException { String url = "D:\\Program Files\\JetBrains\\test1\\Lab\\src\\Java学习\\Java" + "高级\\IO\\IO\\InputStream字节读取流\\练习读写复制文件\\"; System.out.println("---------1.单个字节读写效率慢-----"); FileInputStream fileInputStream1 = new FileInputStream(url + "img.png"); FileOutputStream fileOutputStream1 = new FileOutputStream(url + "单字节慢复制.png"); byte[] bytes1 = new byte[1]; long startTime = System.currentTimeMillis(); while (fileInputStream1.read(bytes1) != -1){//开始单个字节的读取 fileOutputStream1.write(bytes1);//单个文件的写入 } System.out.println("复制单个字节图片文件用时:"+(System.currentTimeMillis()-startTime)+" 毫秒"); fileOutputStream1.close();//先关闭写 fileInputStream1.close();//再关闭读 System.out.println("----------1024*n 的字节读取效率快-----"); FileInputStream fileInputStream2 = new FileInputStream(url + "img.png"); FileOutputStream fileOutputStream2 = new FileOutputStream(url + "多字节快复制.png"); byte[] bytes2 = new byte[1024*100]; long startTime2 = System.currentTimeMillis(); while (fileInputStream2.read(bytes2) != -1){//开始1024*100字节的读取 fileOutputStream2.write(bytes2);//1024*100字节的的写入 } System.out.println("复制800KB,80_0000字节图片文件用时:"+(System.currentTimeMillis()-startTime2)+" 毫秒"); fileOutputStream2.close();//先关闭写 fileInputStream1.close();//再关闭读 } }
Run:
---------1.单个字节读写效率慢-----
复制单个字节图片文件用时:4954 毫秒
----------1024*n 的字节读取效率快-----
复制800KB,80_0000字节图片文件用时:0 毫秒
Process finished with exit code 0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。