字节流:FileInputStream和FileOutputStream的使用

/**
* 测试FileInputStream和FileOutputStream的使用
*
* 结论:
* 1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
* 2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理
* @author CH
* @create 2021 下午 2:13
*/
//使用字节流FileInputStream处理文本文件,可能出现乱码。
@Test
public void testFileInputStream() {
FileInputStream fis = null;
try {
//1. 造文件
File file = new File("hello.txt"); //2.造流
fis = new FileInputStream(file); //3.读数据
byte[] buffer = new byte[5];
int len;//记录每次读取的字节的个数
while((len = fis.read(buffer)) != -1){ String str = new String(buffer,0,len);
System.out.print(str); }
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
//4.关闭资源
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }


分析:输出流buffer五个字节,国字正好夹在中间,所以没有乱码
/*
实现对图片的复制操作
*/
@Test
public void testFileInputOutputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//
File srcFile = new File("爱情与友情.jpg");
File destFile = new File("爱情与友情2.jpg"); //
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile); //复制的过程
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
} } catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
//
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }

//指定路径下文件的复制
public void copyFile(String srcPath,String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//
File srcFile = new File(srcPath);
File destFile = new File(destPath); //
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile); //复制的过程
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
} } catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
//
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }
@Test
public void testCopyFile(){ long start = System.currentTimeMillis(); String srcPath = "C:\\Users\\Administrator\\Desktop\\01.jvm结构_1.avi";
String destPath = "C:\\Users\\Administrator\\Desktop\\0111.jvm结构_1.avi"; // String srcPath = "hello.txt";
// String destPath = "hello3.txt"; copyFile(srcPath,destPath); long end = System.currentTimeMillis(); System.out.println("复制操作花费的时间为:" + (end - start));//618 }

字节流:FileInputStream和FileOutputStream的使用的更多相关文章
- Java IO(五)字节流 FileInputStream 和 FileOutputStream
Java IO(五)字节流 FileInputStream 和 FileOutputStream 一.介绍 字节流 InputStream 和 OutputStream 是字节输入流和字节输出流的超类 ...
- java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr
BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...
- java的文件流:字节流(FileInputStream、FileOutputStream)和字符流(FileReader、FileWriter)。
java的输入输出建立在4个抽象类的基础上:InputStream.OutputStream.Reader.Writer.InputSream和OutputStream被设计成字节流类,而Reader ...
- java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片
package com.lp.ecjtu; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...
- java-IO流-字节流-概述及分类、FileInputStream、FileOutputStream、available()方法、定义小数组、BufferedInputStream、BufferedOutputStream、flush和close方法的区别、流的标准处理异常代码
1.IO流概述及其分类 * 1.概念 * IO流用来处理设备之间的数据传输 * Java对数据的操作是通过流的方式 * Java用于操作流的类都在IO包中 * ...
- (16)IO流之输入字节流FileInputStream和输出字节流FielOutputStream
IO流技术解决的问题:设备与设备之间的传输问题,内存-->硬盘,硬盘-->内存,等等 IO流技术 如果按照数据的流向划分可以划分为:输入流和输出流 输入输出的标准是以程序为参考物的,如果流 ...
- FileInputStream和FileOutputStream详解
一.引子 文件,作为常见的数据源.关于操作文件的字节流就是 FileInputStream & FileOutputStream.它们是Basic IO字节流中重要的实现类.二.FileInp ...
- [八]JavaIO之FileInputStream 与 FileOutputStream
接下来介绍 FileInputStream 和 FileOutputStream 现在看名字应该可以看得出来: 他就是从一个文件中读取数据 或者将数据写入到一个文件中 FileInputStream ...
- J03-Java IO流总结三 《 FileInputStream和FileOutputStream 》
1. FileInputStream FileInputStream是一个文件输入节点流,它是一个字节流,它的作用是将磁盘文件的内容读取到内存中. FileInputStream的父类是Inpu ...
- Java修炼——FileInputStream和FileOutputStream
文件字节流FileInputStream是读文件内容 有一下五个方法 1) abstract int read( ); 2) int read( byte b[ ] ); 3) int read( b ...
随机推荐
- 干货分享:Air780E软件指南:字符串处理
一.Lua字符串介绍 关于字符串,Lua提供了一些灵活且强大的功能,一些入门知识如下: 1.1 字符串定义 在Lua中,字符串可以用单引号'或双引号"来定义.例如: localstr1='H ...
- 超实用的SpringAOP实战之日志记录
本文主要以日志记录作为切入点,来讲解Spring AOP在实际项目开发中怎样更好的使项目业务代码更加简洁.开发更加高效. 日志处理只是AOP其中一种应用场景,当你掌握了这一种场景,对于其它应用场景也可 ...
- Tornado框架之异步与WebSocket(五)
知识点 理解同步与异步执行过程 理解异步代码的回调写法与yield写法 Tornado异步 异步Web客户端AsyncHTTPClient tornado.web.asynchronous torna ...
- Django+SimpleUI
1.安装 pip install django-simpleui -i https://pypi.tuna.tsinghua.edu.cn/simple 2.修改配置文件 # 修改project的se ...
- 2024-11-27:字符串的分数。用go语言,给定一个字符串 s,我们可以定义其“分数”为相邻字符的 ASCII 码差值绝对值的总和。 请计算并返回字符串 s 的分数。 输入:s = “hello“
2024-11-27:字符串的分数.用go语言,给定一个字符串 s,我们可以定义其"分数"为相邻字符的 ASCII 码差值绝对值的总和. 请计算并返回字符串 s 的分数. 输入:s ...
- 150页的剑指Offer解答PDF,它来了!!!
它来了!!! 终于整理出了第一版剑指Offer的PDF,主要以Java语言为主,一共67道题,100多页. 领取方式如下(无套路直接获取百度网盘的 链接,如果链接失效可以直接找我): [秦怀杂货店]公 ...
- web移动端基础
1.像素密度 PPI 说到屏幕就离不开2个因素,屏幕大小和屏幕分辨率. PPI是Pixels Per Inch缩写,pixels per inch所表示的是每英寸所拥有的像素(pixel)数目. PP ...
- MySql 9 in Docker 主从切换
继上一篇<MySql 9 in Docker 利用克隆插件搭建主从>我们说了主从复制后, 那么我们接下来说说如何手动的进行主从切换. 动手~ 1. 原主库设置 切断应用对主库的访问 主库设 ...
- Postman 汉化教程
Postman 汉化教程(Postman中文版) 迷恋自留地 postman官网下载地址 https://www.postman.com/downloads/ postman汉化包 https://g ...
- 【Amadeus原创】IPAD忘记密码重置恢复出厂设置
打开iTunes,确保您的 iPad 没有连接到电脑. 按住顶部按钮,直到出现关机滑块.拖移这个滑块以将 iPad 关机. 在按住主屏幕按钮的同时,将 iPad 连接到电脑.继续按住主屏幕按钮,直到看 ...

