io流之字节流【基础流】
①字节输入流:读取文件中的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.Lucky.io.byteStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /* 字节输入流: FileInputStream【读取文件中的数据】 细节: 创建阶段: 文件不存在,就直接报错【文件都不存在,我读取啥???】 读取阶段: 一次读取一个字节,到了最后return 返回-1 { 空格也是数据:空格在ASCII编码中的数字是32 如果数据中有-1,那么会将-先进行读取,再对1进行读取操作 } 关闭阶段:每次都要关闭资源 步骤:1.创建2.读取3.关闭 */ public class fileinputstream { public static void main(String[] args) throws IOException { System.out.println( "----------初级写法:太麻烦---------" ); FileInputStream inputStream= new FileInputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\testNext.txt" ); inputStream.read(); //Y 每次读取的只有一个【默认返回的是根据ASCII编码转换大的数字】 System.out.print(( char ) inputStream.read()); //Y 可以强行转换成char类型数据 System.out.print(( char ) inputStream.read()); //D 可以强行转换成char类型数据 System.out.print(( char ) inputStream.read()); //S 可以强行转换成char类型数据 System.out.print(( char ) inputStream.read()); //N 可以强行转换成char类型数据 System.out.print(( char ) inputStream.read()); //B 可以强行转换成char类型数据 System.out.print(( char ) inputStream.read()); //空格 可以强行转换成char类型数据 System.out.print(( char ) inputStream.read()); //- 可以强行转换成char类型数据 System.out.println(( char ) inputStream.read()); //1 可以强行转换成char类型数据 System.out.print( "结尾了" +inputStream.read()); //-1 结尾了,返回-1 inputStream.close(); System.out.println(); System.out.println( "----------中级写法:循环遍历读取数据---------" ); FileInputStream inputStream1= new FileInputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\testNext.txt" ); //定义一个遍历储存read的每一个值 int val; //遍历条件【不是结尾】 while ((val=inputStream1.read())!=- 1 ){ System.out.println(( char ) val); //转换成char类型数据并且输出到控制台 } //关闭资源 inputStream1.close(); } } |
②字节输出流:将数据写出到文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package com.Lucky.io.byteStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* io: 1.按照流的方式分类 --》 【输入流and输出流】 2.按照操作文件分类 --》 【字节流and字符流】 注意点:{ 字节流可以操作所有类型的文件 字符流只能操作纯文本文件【用window自带的记事本打开能正常阅读的就是,例如:md/txt】 } */ /** * 字节输出流的小细节: * 创建阶段: * 1.FileOutputStream的参数可以是文件路径,也可以是File对象 * 2.如果不存在指定的文件,会自动创建该文件并且写出数据【前提是:父级路径要真实存在】 * 3.如果指定文件已经存在,进行写出操作时,会将原本文件的数据删除 * 写数据阶段: * 1.write方法的参数是整数,该整数会根据ASCII上对应的字符进行输出并写入到file文件 * 释放资源阶段: * 1.每次使用完都要关闭资源。 */ public class fileoutputstream { public static void main(String[] args) throws IOException { System.out.println( "---------基础部分--------------" ); //创建文件输出流 java.io.FileOutputStream fileOutput= new java.io.FileOutputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\test.txt" ); //写出操作 fileOutput.write( 97 ); //一个参数:可以整数 byte [] bytes={ 97 , 98 , 99 , 100 , 101 , 102 , 103 }; fileOutput.write(bytes); //一个参数:也可以是数组 fileOutput.write(bytes, 1 , 3 ); //三个参数:①数组②开始输出索引③输出字节的数量 //释放资源 fileOutput.close(); System.out.println( "---------进阶部分【换行写】--------------" ); java.io.FileOutputStream fileOutput1= new java.io.FileOutputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\testDemo.txt" ); String str= "WYRSYYDS" ; byte [] bytes1 = str.getBytes(); //写出操作 fileOutput1.write(bytes1); //一个参数:数组 //换行写【就是将在中间加上\r\n】 String rn= "\r\n" ; byte [] bytes2 = rn.getBytes(); fileOutput1.write(bytes2); String str1= "NB" ; byte [] bytes3 = str1.getBytes(); //写出操作 fileOutput1.write(bytes3); //一个参数:数组 fileOutput1.close(); System.out.println( "---------进阶部分【续写】--------------" ); /* 续写: 在创建阶段时,添加的第二个参数就是开关【默认是false,可以手动开启】 */ java.io.FileOutputStream fileOutput2= new java.io.FileOutputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\testNext.txt" , true ); // String xu="YYDS"; // byte[] bytes4 = xu.getBytes(); // //写出操作 // fileOutput2.write(bytes4); //一个参数:数组 String next= "NB" ; byte [] bytes5 = next.getBytes(); //写出操作 fileOutput2.write(bytes5); //一个参数:数组 fileOutput2.close(); } } |
综合小练习:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.Lucky.io.byteStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; /* 拷贝小文件 :边读边写 要求:要求计算拷贝所消耗的时间【单位是毫秒】 */ public class copyMinData { public static void main(String[] args) throws IOException { //创建 FileInputStream input= new FileInputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\test.txt" ); //读取对象 FileOutputStream outout= new FileOutputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\copyminData.txt" ); //输出对象 long l1 = System.currentTimeMillis(); //遍历 int val; while ((val=input.read())!=- 1 ){ //读取 outout.write(val); //输出到copy文件中 } //关闭资源{先开后关} --》可以保证数据读取完整 outout.close(); input.close(); long l2 = System.currentTimeMillis(); System.out.println(l2-l1); //消耗的毫秒数 } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | package com.Lucky.io.byteStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; /* 拷贝大文件 :边读边写【一次读取多个字节】 */ public class copyMaxData { public static void main(String[] args) throws IOException { //创建 FileInputStream input= new FileInputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\test.txt" ); //读取对象 FileOutputStream outout= new FileOutputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\copymaxData.txt" ); //输出对象 //创建一次读取的字节数组 byte [] arr= new byte [ 2 ]; //数组长度为2 //byte[] arr=new byte[1024*1024*5]; //数组长度为5M //遍历 int val; //每一次遍历几个字节 while ((val=input.read(arr))!=- 1 ){ //读取 //输出到copy文件中【参数1:要写的数据源;参数2:开始索引;参数3:个数】 outout.write(arr, 0 ,val); } //关闭资源{先开后关} --》可以保证数据读取完整 outout.close(); input.close(); //////////////////////////////////////////////////////////////////////////// //使用try catch /* FileInputStream inputTry=null; //必须先赋值为null FileOutputStream outTry=null; try { //创建 inputTry=new FileInputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\test.txt"); //读取对象 outTry=new FileOutputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\copymaxData.txt"); //输出对象 //创建一次读取的字节数组 byte[] arrTry=new byte[5]; //数组长度为5 //遍历 int valTry; //每一次遍历几个字节 while ((valTry=inputTry.read(arrTry))!=-1){ //读取 //输出到copy文件中【参数1:要写的数据源;参数2:开始索引;参数3:个数】 outTry.write(arrTry,0,valTry); } } catch (IOException e) { e.printStackTrace(); } finally { //关闭资源{先开后关} --》可以保证数据读取完整 //判断outTry对象是否是null if(outTry!=null){ try {//因为close可能也存在异常 outTry.close(); } catch (IOException e) { e.printStackTrace(); } } //判断inputTry对象是否是null if(outTry!=null){ try {//因为close可能也存在异常 inputTry.close(); } catch (IOException e) { e.printStackTrace(); } } } */ } } |
★★★拓展:加密/解密
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.Lucky.io.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * 加密与解密: 关键点: ^ * * 在对要加密的文件数据进行【^】异或操作就可以使得储存的数据发生改变 * 当对要加密的文件数据进行解密时,只要再次使用【^】异或操作就可以 */ public class mecryOrDecode { public static void main(String[] args) throws IOException { //加密 FileInputStream inStr = new FileInputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\characterStream\\girl.jpg" ); FileOutputStream OutStr = new FileOutputStream( "H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\characterStream\\encry.jpg" ); //解密 // FileInputStream inStr = new FileInputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\characterStream\\encry.jpg"); // FileOutputStream OutStr = new FileOutputStream("H:\\项目\\javaSE\\IO\\src\\com\\Lucky\\io\\characterStream\\redu.jpg"); int val; while ((val=inStr.read())!=- 1 ){ OutStr.write(val ^ 20 ); } inStr.close(); OutStr.close(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律