Day 60

news/2025/2/12 16:08:26/

字符流读数据的两种方式

  1. 方法名说明
    int read()一次读一个字符数据
    int read(char[] cbuf)一次读一个字符数组数据
  2. package demo14;import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;public class _IO_Stream_Demo_03 {public static void main(String[] args) {/*int read()                一次读一个字符数据int read(char[] cbuf)     一次读一个字符数组数据*/InputStreamReader isr = null;try {isr = new InputStreamReader(new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo14\\book_01.txt"));if (isr != null) {int ch;/*// int read()     一次读一个字符数据while ((ch = isr.read()) != -1) {System.out.print((char) ch);}*/// int read(char[] cbuf)     一次读一个字符数组数据char[] arr = new char[1024];while ((ch = isr.read(arr)) != -1) {/*public String(char[] value, int offset,  int count)分配一个新的String ,其中包含字符数组参数的子阵列中的字符。offset参数是子阵列的第一个字符的索引, count参数指定子阵列的长度。副本的内容被复制; 字符数组的后续修改不会影响新创建的字符串*/System.out.print(new String(arr,0,ch));}}} catch (IOException e) {e.printStackTrace();}finally {try {isr.close();} catch (IOException e) {e.printStackTrace();}}}
    }
    ==============================================
    声表面波(SAW,Surface Acoustic Wave)是沿物体表面传播的一种弹性波。
    声表面波是英国物理学家瑞利(Rayleigh)在19世纪80 年代研究地震波的过程中偶尔发现的一种能量集中于地表面传播的声波。
    1965年,美国的怀特(R.M.White)和沃尔特默(F.W.Voltmer)发表题为“一种新型声表面波声——电转化器”的论文,
    取得了声表面波技术的关键性突破,能在压电材料表面激励声表面波的金属叉指换能器 IDT的发明,大大加速了声表面波技术的发展,
    使这门年轻的学科逐步发展成为一门新兴的、声学和电子学相结合的边缘学科。
    Process finished with exit code 0
    

_字符流复制Java文件改进版

  1. 分析:
    • 转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,所以,为了简化书写,转换流提供了对应的子类
    • FileReader:用于读取字符文件的便捷类:FileReader(String fileNme)
    • FileWriter:用于写入字符文件的便捷类:FileWriter(String fileNme)

_字符缓冲流

  1. BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区的大小,或者是可以接受默认大小。默认值足够大,可用于大多数用途
  2. BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的搞笑读取,可以指定缓冲区的大小,或者是可以使用默认大小。默认值足够大,可用于大多数用途

_字符缓冲流复制Java文件

  1. 思路:

    • 根据数据源创建字符缓冲流输入对象
    • 根据目的地创建字符缓冲输出流对象
    • 读写数据,复制文件
    • 释放资源
  2. package demo14;import java.io.*;public class _IO_Stream_Demo_04 {public static void main(String[] args) {/*需求: 字符缓冲流复制Java文件思路:- 根据数据源创建字符缓冲流输入对象- 根据目的地创建字符缓冲输出流对象- 读写数据,复制文件- 释放资源*/BufferedReader br = null;BufferedWriter bw = null;try {br = new BufferedReader(new FileReader("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\_IO_Buffered_02.java"));bw = new BufferedWriter(new FileWriter("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo14\\_IO_Buffered_02.java"));if (br != null) {char[] ch = new char[1024];int len;while ((len = br.read(ch)) != -1) {bw.write(ch,0,len);}}} catch (IOException e) {e.printStackTrace();}finally {try {br.close();} catch (IOException e) {e.printStackTrace();}try {bw.close();} catch (IOException e) {e.printStackTrace();}}}
    }
    ==========================================Process finished with exit code 0
    ******************************************package demo13;import java.io.*;public class _IO_Buffered_02 {public static void main(String[] args) {/*需求:用4种方法把一个视频文件复制到当前的模块下,比较程序运行的时间思路 :- 根据数据源创建字节输入流对象- 根据目的地创建字节流入流对象- 读写数据,复制视频- 释放资源基本字节流一次读写一个字节:       程序运行耗时:59099毫秒基本字节流一次读写一个字节数组:    程序运行耗时:101毫秒字节缓冲流一次读写一个字节:       程序运行耗时:258毫秒字节缓冲流一次读写一个字节数组:    程序运行耗时:37毫秒*/// 记录开始时间long startTime = System.currentTimeMillis();
    //        run_01();     //  程序运行耗时:59099毫秒
    //        run_02();     //  程序运行耗时:101毫秒
    //        run_03();     //  程序运行耗时:258毫秒run_04();     //  程序运行耗时:37毫秒// 记录程序结束时间long endTime = System.currentTimeMillis();System.out.println("程序运行耗时:"+(endTime-startTime)+"毫秒");}// 单独使用FileOut/InStream,一次读取一个字节public static void run_01() {FileOutputStream fos = null;FileInputStream fis = null;try {fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");if (fis != null) {int read;while ((read = fis.read()) != -1) {fos.write(read);}}} catch (IOException e) {e.printStackTrace();}finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}try {fis.close();} catch (IOException e) {e.printStackTrace();}}}// 单独使用FileOut/InStream,一次读取byte[1024]public static void run_02() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");if (fis != null) {int len;byte[] bytes = new byte[1024];while ((len = fis.read(bytes)) != -1) {fos.write(bytes,0,len);}}} catch (IOException e) {e.printStackTrace();}finally {try {fis.close();} catch (IOException e) {e.printStackTrace();}try {fos.close();} catch (IOException e) {e.printStackTrace();}}}// 使用字节缓冲流复制视频,但一次只读取一个字节public static void run_03() {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {FileInputStream fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");FileOutputStream fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);if (bis != null) {int read;while ((read = bis.read()) != -1) {bos.write(read);}}} catch (IOException e) {e.printStackTrace();}finally {try {bis.close();} catch (IOException e) {e.printStackTrace();}try {bos.close();} catch (IOException e) {e.printStackTrace();}}}// 使用字节缓冲流复制视频,一次读取byte[1024]public static void run_04() {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {FileInputStream fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");FileOutputStream fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);if (bis != null) {int len;byte[] bytes = new byte[1024];while ((len = bis.read(bytes)) != -1) {bos.write(bytes,0,len);}}} catch (IOException e) {e.printStackTrace();}finally {try {bis.close();} catch (IOException e) {e.printStackTrace();}try {bos.close();} catch (IOException e) {e.printStackTrace();}}}
    }
    *****************************************
    package demo13;import java.io.*;public class _IO_Buffered_02 {public static void main(String[] args) {/*需求:用4种方法把一个视频文件复制到当前的模块下,比较程序运行的时间思路 :- 根据数据源创建字节输入流对象- 根据目的地创建字节流入流对象- 读写数据,复制视频- 释放资源基本字节流一次读写一个字节:       程序运行耗时:59099毫秒基本字节流一次读写一个字节数组:    程序运行耗时:101毫秒字节缓冲流一次读写一个字节:       程序运行耗时:258毫秒字节缓冲流一次读写一个字节数组:    程序运行耗时:37毫秒*/// 记录开始时间long startTime = System.currentTimeMillis();
    //        run_01();     //  程序运行耗时:59099毫秒
    //        run_02();     //  程序运行耗时:101毫秒
    //        run_03();     //  程序运行耗时:258毫秒run_04();     //  程序运行耗时:37毫秒// 记录程序结束时间long endTime = System.currentTimeMillis();System.out.println("程序运行耗时:"+(endTime-startTime)+"毫秒");}// 单独使用FileOut/InStream,一次读取一个字节public static void run_01() {FileOutputStream fos = null;FileInputStream fis = null;try {fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");if (fis != null) {int read;while ((read = fis.read()) != -1) {fos.write(read);}}} catch (IOException e) {e.printStackTrace();}finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}try {fis.close();} catch (IOException e) {e.printStackTrace();}}}// 单独使用FileOut/InStream,一次读取byte[1024]public static void run_02() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");if (fis != null) {int len;byte[] bytes = new byte[1024];while ((len = fis.read(bytes)) != -1) {fos.write(bytes,0,len);}}} catch (IOException e) {e.printStackTrace();}finally {try {fis.close();} catch (IOException e) {e.printStackTrace();}try {fos.close();} catch (IOException e) {e.printStackTrace();}}}// 使用字节缓冲流复制视频,但一次只读取一个字节public static void run_03() {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {FileInputStream fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");FileOutputStream fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);if (bis != null) {int read;while ((read = bis.read()) != -1) {bos.write(read);}}} catch (IOException e) {e.printStackTrace();}finally {try {bis.close();} catch (IOException e) {e.printStackTrace();}try {bos.close();} catch (IOException e) {e.printStackTrace();}}}// 使用字节缓冲流复制视频,一次读取byte[1024]public static void run_04() {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {FileInputStream fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");FileOutputStream fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);if (bis != null) {int len;byte[] bytes = new byte[1024];while ((len = bis.read(bytes)) != -1) {bos.write(bytes,0,len);}}} catch (IOException e) {e.printStackTrace();}finally {try {bis.close();} catch (IOException e) {e.printStackTrace();}try {bos.close();} catch (IOException e) {e.printStackTrace();}}}
    }
    bos = null;try {FileInputStream fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");FileOutputStream fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);if (bis != null) {int len;byte[] bytes = new byte[1024];while ((len = bis.read(bytes)) != -1) {bos.write(bytes,0,len);}}} catch (IOException e) {e.printStackTrace();}finally {try {bis.close();} catch (IOException e) {e.printStackTrace();}try {bos.close();} catch (IOException e) {e.printStackTrace();}

_字符缓冲流的特有功能

  1. BufferedWriter:

    • void newLine() :写一行行分隔符,行分隔符字符由系统属性定义
  2. BufferedReader:

    • public String readLine() :读一行文字。结果包含的内容的字符串,不包括任何终止字符,如果流的结尾已经到达,则为null
  3. package demo14;import java.io.*;public class _IO_Stream_Demo_05 {public static void main(String[] args) {/*需求:用字符流的特有功能复制JAVA文件思路:1. BufferedWriter:- void newLine() :写一行行分隔符,行分隔符字符由系统属性定义2. BufferedReader:- public String readLine() :读一行文字。结果包含的内容的字符串,不包括任何终止字符,如果流的结尾已经到达,则为null*/BufferedWriter bw = null;BufferedReader br = null;try {bw = new BufferedWriter(new FileWriter("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo14\\_Copy_IO_Buffered_02.java"));br = new BufferedReader(new FileReader("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\_IO_Buffered_02.java"));if (br != null) {String len;while ((len = br.readLine()) != null) {bw.write(len);bw.newLine();bw.flush();}}} catch (IOException e) {e.printStackTrace();}finally {try {bw.close();} catch (IOException e) {e.printStackTrace();}try {br.close();} catch (IOException e) {e.printStackTrace();}}}
    }
    =================================Process finished with exit code 0

http://www.ppmy.cn/news/500446.html

相关文章

讨论| 电视行业已是落日夕阳?

如今,随着移动设备、互联网技术的发展,电视似乎逐渐淡出人们的视野。近期,与电视相关的热门讨论似乎都是对于各大视频平台的会员投屏制度,这似乎更加减少了大众对于电视的使用欲望。那么电视领域是否真的趋于淘汰了呢?…

60

1 --12 SELECT STUNO,STUNAME3 FROM Student4 WHERE STUNO=000000015 --26 SELECT *7

pyfaidx:序列处理工具

pyfaidx是一个处理序列文件的软件,类似于samtools中的faidx。 参考地址:https://github.com/mdshw5/pyfaidx 安装: pip install pyfaidx 基本方法: 读取文件: 参数简介: split_char:分隔…

2023年6月24日(星期六):骑行明郎

2023年6月24日(星期六):骑行明郎,早8:30到9:00, 大观公园门囗集合,9:30点准时出发 【因迟到者,骑行速度快者,可自行追赶偶遇。】 偶遇地点: 大观公园门囗集合,家住南,东&#xff0c…

宿舍智能计量电表的功能介绍

宿舍智能计量电表石家庄光大远通电气有限公司宿舍智能模块参数功能:后用电,无费自动断电,当用户剩余电量为设定值时,自动断供电,直至重新付费后自动恢复供电,断电启动紧急信号:当用户的剩余电量…

SIFT算法简介

参考资料 SIFT文献-David-UBC: 《Distinctive Image Features from Scale-Invariant Keypoints》SIFT算法介绍: SIFT特征详解 - Brook_icv - 博客园 (cnblogs.com) 简介 Scale-invariant feature transform 尺度不变特征变换 SIFT算法不仅只有尺度不…

资源分享:17 张程序员专属高清壁纸

1、三思后再写代码!!! 2、从世界上搜索喜欢你的人!!! 3、代码没写完,哪里有脸睡觉!!! 4、程序员的 Home 键!!! 5、编程是一…

OpenSSL生成SSL证书,受浏览器信任吗?

OpenSSL是用于传输层安全(TLS)协议的开源工具包,OpenSSL生成SSL证书能受到浏览器信任吗?OpenSSL生成SSL证书能不能用于网站HTTPS加密呢? OpenSSL是什么? OpenSSL是基于密码学的用于传输层安全(TLS)协议的开源工具包,可…