有没有做生物科技相关的网站,弄一个app大概多少钱,引流推广平台有哪些,wordpress 文件上传功能菜鸟之路Day15一一IO流#xff08;一#xff09;
作者#xff1a;blue
时间#xff1a;2025.2.8 文章目录 菜鸟之路Day15一一IO流#xff08;一#xff09;0.概述1.初识IO流1.1.什么是IO流#xff1f;1.2.IO流的作用1.3.IO流的分类 2.IO流的体系结构3.字节输出流的基本…菜鸟之路Day15一一IO流一
作者blue
时间2025.2.8 文章目录 菜鸟之路Day15一一IO流一0.概述1.初识IO流1.1.什么是IO流1.2.IO流的作用1.3.IO流的分类 2.IO流的体系结构3.字节输出流的基本用法4.字节输入流的基本用法5.异常处理了解即可6.字符集7.字符输入流7.1FileReader7.2FileWriter 8.IO流综合练习8.1练习一8.2练习二8.3练习三 0.概述
文章内容学习自黑马程序员BV1yW4y1Y7Ms
1.初识IO流
1.1.什么是IO流
答存储和读取数据的解决方案
1.2.IO流的作用
答用于读写数据本地文件网络
1.3.IO流的分类
按流的方向分 按操作文件的类型分 2.IO流的体系结构 3.字节输出流的基本用法
FileOutputStream 操作本地文件的字节输出流可以把程序中的数据写到本地文件中
书写步骤:
①创建字节输出流对象
细节1参数是字符串表示的路径或者File对象都是可以的
细节2如果文件不存在会创建一个新的文件但是要保证父级路径是存在的
细节3如果文件已经存在则会清空文件
②写数据
细节write方法的参数是整数但是实际上写到本地文件中的是整数在ASCII上对应的字符
③释放资源
细节每次使用完流之后都要释放资源
package IOStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class FileOutputStreamDemo1 {public static void main(String[] args) throws IOException {FileOutputStream fos new FileOutputStream(src\\IOStream\\a.txt);//①创建字节输出流对象fos.write(97);//②写数据fos.close();//③释放资源}
}写出数据的三种方式
public class FileOutputStreamDemo2 {public static void main(String[] args) throws IOException {/** void write(int b) 一次写一个字节数据* void write(byte[] b) 一次写一个字节数组数据** void write(byte[] b,int off,int len) 一次写一个字节数组的部分数据* 参数一数组* 参数二起始索引 0* 参数三个数 3* */FileOutputStream fos new FileOutputStream(src\\IOStream\\a.txt);byte[] b {97,98,99,100,101};fos.write(b,1,2);}
}换行和续写
public class FileOutputStreamDemo3 {public static void main(String[] args) throws IOException {/** 换行符* windows:\r\n* Linux:\n* Mac:\r* 续写* 打开续写开关即可即创建对象的第二个参数* 默认是false表示关闭创建对象会清空文件* 传递true表示打开续写此时创建对象不会清空文件* */FileOutputStream fos new FileOutputStream(src\\IOStream\\a.txt,true);String str1 kcbdkdvkdkjxkjdkj;String str2 \r\n;String str3 666;byte[] b1 str1.getBytes();byte[] b2 str2.getBytes();byte[] b3 str3.getBytes();fos.write(b1);fos.write(b2);fos.write(b3);}
}4.字节输入流的基本用法
FileInputStream操作本地文件的字节输入流可以把本地文件中的数据读取到程序中来
书写步骤
①创建字节输入流对象
细节1如果文件不存在就直接报错
②读数据
细节1一次读一个字节读出来的数据在ASCII上对应的数字
细节2读到文件末尾了read方法返回-1
③释放资源
细节每次使用完流之后都要释放资源
public class FileInputStreamDemo1 {public static void main(String[] args) throws IOException {FileInputStream fis new FileInputStream(src\\IOStream\\a.txt);int ans fis.read();while(ans!-1){System.out.print((char)ans);ans fis.read();}fis.close();//释放资源}
}文件拷贝的基本代码
思想边读边写
public class FileInputStreamDemo2 {public static void main(String[] args) throws IOException {//1.创建对象FileInputStream fis new FileInputStream(C:\\Users\\zhangtenlan\\Desktop\\abc.txt);FileOutputStream fos new FileOutputStream(src\\IOStream\\ans.txt);//2.拷贝//核心思想边读边写int b;while((bfis.read())!-1){fos.write(b);}//3.释放资源//规则先开的最后关闭fos.close();fis.close();}
}以上方法在拷贝大文件的时候速度非常的慢原因就在于每次只能读取一个字节的数据我们可以用read方法的一个重载来一次性读取多个数据。
public int read(byte[] buffer) //一次读一个字节数组数据
//注意一次读一个字节数组的数据每次读取会尽可能把数组装满
//长度尽量设置为1024的整数倍不要太大像1024*1024*5例子
public class FileInputStreamDemo3 {public static void main(String[] args) throws IOException {FileInputStream fis new FileInputStream(src\\IOStream\\ans.txt);byte[] b new byte[2];int len;while((lenfis.read(b))!-1){ //当读到的长度为-1时代表读完了System.out.print(new String(b,0,len));}fis.close();}
}拷贝大文件的写法
public class FileInputStreamDemocratic {public static void main(String[] args) throws IOException {long start System.currentTimeMillis();//记录开始时间//1.创建对象FileInputStream fis new FileInputStream(大文件);FileOutputStream fos new FileOutputStream(目标位置);//2.拷贝int len;byte[] bytes new byte[1024*1024*5];while((lenfis.read(bytes))!-1){fos.write(bytes,0,len);}//3.释放资源fos.close();fis.close();long end System.currentTimeMillis();//记录结束时间System.out.println(end-start);//看看程序运行花了多少时间}
}5.异常处理了解即可
try{}catch(IOException e){}finally{//finally中代码一定执行除非JVM退出//所以释放资源的代码写到这里再合适不过
}6.字符集
1.在计算机中任意数据都是以二进制的形式来存储的
2.计算机中最小的存储单元是一个字节
3.ASCII字符集中一个英文占一个字节
4.简体中文版Windows默认使用GBK字符集
5.GBK字符集完全兼容ASCII字符集
注意一个英文占一个字节二进制第一位是0
一个中文占两个字节二进制高位字节的第一位是1
6.Unicode字符集的UTF-8编码格式
注意一个英文占一个字节二进制第一位是0转成十进制是正数
一个中文占三个字节二进制第一位是1第一个字节转成十进制是负数
为什么会有乱码
原因1读取数据时未读完整个汉字
原因2编码和解码时的方式不统一
Java中的解码和编码
public class FileInputStreamDemo5 {public static void main(String[] args) throws UnsupportedEncodingException {/** java中的编码方法* public byte[] getBytes() 使用默认方式进行编码UTF-8* public byte[] getBytes(String charsetName) 使用指定方式进行编码** java中的解码方法* String(byte[] bytes) 使用默认方式进行解码UTF-8* String(byte[] bytes,String charsetName) 使用指定方式进行解码* */String str 你好;byte[] res1 str.getBytes();System.out.println(Arrays.toString(res1));System.out.println();byte[] res2 str.getBytes(GBK);System.out.println(Arrays.toString(res2));System.out.println();String ans1 new String(res1);System.out.println(ans1);System.out.println();String ans2 new String(res2,GBK);System.out.println(ans2);}
}7.字符输入流 字符流
字符流 字节流字符集
特点
输入流一次读一个字节遇到中文时一次读多个字节所以适合处理中文
输出流底层会把数据按照指定的编码方式进行编码变成字节再写到文件中
使用场景
对于纯文本文件进行读写操作
书写步骤
①创建字符输入流对象
②读取数据
注意细节1按字节进行读取遇到中文一次读多个字节读取后解码返回一个整数
细节2读到文件末尾了read方法返回-1
③释放资源
7.1FileReader
public class IOTestOne {public static void main(String[] args) throws IOException {/** 第一步创建对象* public FileReader(File file) 创建字符输入流关联本地文件* public FileReader(String pathname)** 第二步读取数据* public int read() 读取数据读到末尾返回-1* public int read(char[] buffer) 读取多个数据读到末尾返回-1** 第三步释放资源* public void close 释放资源* */FileReader fr new FileReader(src\\IOTest\\a.txt);int ch;while((chfr.read())!-1){System.out.print((char)ch);}fr.close();}
}/*有参的readFileReader fr new FileReader(src\\IOTest\\a.txt);char[] chars new char[2];int len;while((lenfr.read(chars))!-1){//有参read返回值是System.out.print(new String(chars,0,len));}r.close();
*/7.2FileWriter
①创建字符输出流对象
细节1参数是字符串表示的路径或者是File对象都可以
细节2如果文件不存在会创建一个新的文件但是要保证父级路径是存在的
细节3如果文件已经存在则会清空文件如果不想清空可以打开续写开关
②写数据
细节如果write方法的参数是整数但是实际上写到本地文件中的是整数在字符集上对应的字符
③释放资源
细节每次使用完流之后都要释放资源
public class IOTestTwo {public static void main(String[] args) {/** 第一步创建对象* public FileWriter(File file)* public FileWriter(String pathname)* public FileWriter(File file,boolean append) 续写开关* public FileWriter(String pathname,boolean append)** 第二步读取数据* void write(int c)* void write(String str)* void write(String str,int off,int len)* void write(char[] cbuf)* void write(char[] cbuf,int off,int len)** 第三步释放资源* public void close() * */}
}8.IO流综合练习
字节流和字符流的使用场景
字节流拷贝任意类型的文件
字符流读取纯文本文件中的数据往纯文本文件中写出数据
8.1练习一
拷贝文件夹考虑子文件夹
public class Test1 {public static void main(String[] args) throws IOException {File src new File(C:\\Users\\zhangtenlan\\Desktop\\src);//数据源File dest new File(C:\\Users\\zhangtenlan\\Desktop\\dest);//目的地copydir(src,dest);}private static void copydir(File src, File dest) throws IOException {dest.mkdirs();//1.进入数据源File[] arr src.listFiles();//2.遍历数据源for(File file:arr){//3.如果是文件则拷贝if(file.isFile()) {FileInputStream fis new FileInputStream(file);FileOutputStream fos new FileOutputStream(new File(dest,file.getName()));byte[] bytes new byte[1024];int len;//边读边写while((len fis.read(bytes))!-1){fos.write(bytes,0,len);}fos.close();fis.close();}//4.如果是文件夹则递归else {copydir(file,new File(dest,file.getName()));}}}
}8.2练习二
文件加密
为了保证文件的安全性就需要对原始文件进行加密存储再使用的时候再对其进行解密处理。
加密原理
对原始文件中的每一个字节数据进行更改然后将更改以后的数据存储到新文件中
解密原理
读取加密之后的文件按照加密规则反向操作变成原始文件
//完成这个功能我们可以利用异或的原理
//加密我们可以令每个字节都异或上一个数字
public class Test2 {public static void main(String[] args) throws IOException {FileInputStream fis new FileInputStream(src\\yuan.png);FileOutputStream fos new FileOutputStream(src\\jiami);int b;while((bfis.read())!-1){fos.write(b^5);}fos.close();fis.close();}
}//解密我们取原来加密过的图片再次异或上这个数字
public class Test2 {public static void main(String[] args) throws IOException {FileInputStream fis new FileInputStream(src\\jiami);FileOutputStream fos new FileOutputStream(src\\jiemi.png);int b;while((bfis.read())!-1){fos.write(b^5);}fos.close();fis.close();}
}8.3练习三
文本文件中有以下数据2-1-9-4-7-8
将文本中的数据进行排序变成以下数据1-2-4-7-8-9
public class Test3 {public static void main(String[] args) throws IOException {//1.读取数据//纯文本文件可以使用字符流来操作FileReader fr new FileReader(src\\a.txt);StringBuilder sb new StringBuilder();int b;while((bfr.read())!-1){sb.append((char)b);}fr.close();//2.排序ArrayListInteger list new ArrayList();String[] arr sb.toString().split(-);//分离-for (String s : arr) {//将数据转为整数类型为排序做准备list.add(Integer.parseInt(s));}Arrays.sort(arr);//排序//3.写回FileWriter fw new FileWriter(src\\b.txt);for (int i 0; i arr.length; i) {if(iarr.length-1) {fw.write(arr[i]);}else {fw.write(arr[i]-);}}fw.close();}
}