字节流: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 ...
随机推荐
- 6、oracle网络(监听)
oracle包含 1.软件 2.数据库 3.实例 4.监听(listener) 监听的特点 可以独立启动,就是说,数据库没有启动,监听可以启动:数据库启动,监听也可以不启动:数据库启动,监听也启动 监 ...
- 2.TP6的入门-分页类的改写
看了看推荐的分页类的使用,还是很简单的,可是自己去尝试改写生成的分页类结构就会很麻烦,总是不成功,后来发现手册里面还有这个 就说你想重写分页类,就需要这样做 赶紧实践了一下,先改这里的provider ...
- 敏捷开发:Sprint Planning 冲刺计划会议详细介绍和用户故事拆分、开发任务细分
Sprint Planning 冲刺计划会议简介 Sprint Planning (冲刺计划会议),又叫规划会议.此会议通过 Scrum 团队的集体沟通讨论,确定接下来的 Sprint 中要完成的待开 ...
- spring IOC 实现原理模拟实现
原文系装载: http://blog.sina.com.cn/s/blog_6a7f00ed01011dyv.html 最近对spring IOC AOP 机制实现原理了解了下,在此做下整理,希望能给 ...
- Win10多用户同时登陆
1. 说明: (1)Win服务器版默认是支持多用户登陆的,甚至可以在主机上用不同用户自己远程登陆自己,如window server 2016. (2)Win10正常情况下是不允许用户同时远程的,即一个 ...
- 【CSS样式】好看的样式千篇一律,有趣的灵魂万里挑一
input 输入框美化: https://www.jianshu.com/p/9b4b60b8d475 button 按钮美化: 1.登录框按钮 https://blog.csdn.net/wangj ...
- VUE3 使用资源路径加载
1.使用场景 有些情况下,我需要使用组件路径动态的方式加载组件. 2.实现方法 import { defineAsyncComponent } from 'vue'; /** * 根据view组件路径 ...
- 华为云云日志服务 HarmonyOS NEXT采集最佳实践
鸿蒙背景介绍 华为鸿蒙HarmonyOS系统是面向万物互联的全场景分布式操作系统,支持手机.平板.智能穿戴.智慧屏等多种终端设备运行,提供应用开发.设备开发的一站式服务的平台.2024 年 1 月 1 ...
- docker启动所有容器命令
启动所有容器 docker start $(docker ps -a | awk '{ print $1}' | tail -n +2) 关闭所有容器 docker stop $(docker ps ...
- powershell禁止系统运行脚本
错误信息: set-executionpolicy remotesigned set-executionpolicy : Windows PowerShell 已成功更新你的执行策略,但在更具体的作业 ...

