节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter
节点流(文件流)
FileInputStream(字节流)处理视频类的
FileOutputStream(字节流)
FileReader(字符流)处理文本文件
FileWriter(字符流)
TestFileInputOutStream
package com.aff.file; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import javax.print.DocFlavor.STRING; import org.junit.Test; //节点流(文件流) // FileInputStream(字节流)处理视频类的
// FileOutputStream(字节流)
//
// FileReader(字符流)处理文本文件
// FileWriter(字符流)
public class TestFileInputOutStream { //FileInputStream
//从硬盘存在的文件中,读取其内容到程序中
//要读取的文件一定要存在,否则抛出FileNotFoundException
@Test
public void testFileInputStream() throws IOException {
// 1.创建一个File类的对象
File file = new File("heel.txt");//指明要读入文件的路径
// 2.创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取
/*
* read(): 读取文件的一个字节每执行到文件结尾时, 返回-1
*/
// int b = fis.read();
// while (b != -1) {
// System.out.println((char)b);
// b = fis.read();
// }
int b ;
while((b = fis.read())!=-1){
System.out.println((char)b);
}
// 4.关闭相应的流
fis.close();
} // 改进, 使用try-catch处理,保证流的关闭操作一定可以执行
@Test
public void testFileInputStream1() { FileInputStream fis = null;
try {
// 1.创建一个File类的对象
File file = new File("heel.txt");//指明要读入文件的路径
// 2.创建一个FileInputStream类的对象
fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取
int b ;
while((b = fis.read())!=-1){
System.out.println((char)b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
// 4.关闭相应的流
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} //再改进, 使用使用数组读取,更加快
@Test
public void testFileInputStream2() {
FileInputStream fis = null;
try {
File file = new File("heel.txt");
fis = new FileInputStream(file);
//使用数组
byte [] b = new byte[5];//读取到的数据写入数组
int len ;//每次读入到byte中的字节的长度
while((len = fis.read(b))!=-1){
// for(int i = 0;i<len;i++){
// System.out.println((char)b[i]);
// }
String str = new String(b,0,len);//读入数组b中,从0开始,每次读入的长度
System.out.print(str);
} } catch (Exception e) {
e.printStackTrace();
}finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} //FileOutputStream
@Test
public void testFileOutputStream(){
FileOutputStream fos = null;
try {
//创建一个File类的对象。表名要写入的文件位置
//输出的物理文件可以不存在,当执行过程中,若不存在,会自动创建。若存在会将原来的文件覆盖。
File file = new File("aff");
//创建一个FileOutputStream类的对象,将file类的对象作为形参传递给FileOutputStream的构造器中
fos = new FileOutputStream(file);
//写入操作
fos.write(new String("i love fangfang").getBytes());//将字符串转为字节数组
}catch (IOException e) {
e.printStackTrace();
}finally {
//关闭输出流
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} //testFileInputOutStream, 实现文件复制
@Test
public void testFileInputOutStream(){
//从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)
File file1 = new File("aff.txt");
File file2 = new File("aff2.txt");
FileInputStream fis = null;
FileOutputStream fos = null;
//提供相应的流
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//实现文件的复制
byte[] b = new byte[20];
int len;
while((len = fis.read(b)) != -1){
fos.write(b, 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 src,String dest){
File file1 = new File(src);
File file2 = new File(dest);
FileInputStream fis = null;
FileOutputStream fos = null;
//提供相应的流
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//实现文件的复制
byte[] b = new byte[20];
int len;
while((len = fis.read(b)) != -1){
fos.write(b, 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 src = "C:\\Users\\lz\\Desktop\\1.avi";
String dest = "C:\\Users\\lz\\Desktop\\2.avi";
copyFile(src,dest);
long end = System.currentTimeMillis();
System.out.println("花费的时间:"+(end-start));//21MB 4968
} }
TestFileReaderWriter:
package com.aff.file; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import org.junit.Test; public class TestFileReaderWriter { // FileReader
@Test
public void testFileReader() {
FileReader fr = null;
try {
File file = new File("license.txt");
fr = new FileReader(file);
char[] c = new char[30];
int len;
while ((len = fr.read(c)) != -1) {
String str = new String(c, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} // 使用FileReader FileWriter ,实现文本文件的复制
// 对于非文本文件(视频,图片,音乐等)只能使用字节流
@Test
public void testFileReaderWriter() {
// 1.输入流对应的文件src一定要存在,输出流对应的dest可以不存在,执行过程中会自动创建
FileReader fr = null;
FileWriter fw = null; try {
File src = new File("license.txt");
File dest = new File("license1.txt");
fr = new FileReader(src);
fw = new FileWriter(dest);
char[] c = new char[30];
int len;
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }
复制结果:

节点流(文件流) FileInputStream & FileOutputStream & FileReader & FileWriter的更多相关文章
- java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例
FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...
- 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝
两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...
- io流-文件流\节点流
FileOutputStream类(jdk1.0) 描述 java.io.FileOutputStream 类是文件字节输出流,用于将数据写入到文件中. 构造方法 //构造方法 FileOutputS ...
- 用内存流 文件流 资源生成客户端(Delphi开源)
正文:很多木马生成器就是用的内存流和文件流生成客户端的,废话不多说了,代码如下: unit Main; interface usesWindows, Messages, SysUtils, Varia ...
- csv内存流文件流
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)
1.操作基本数据类型的流 1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...
- java 文件字节和字符流 缓冲流
流的原理 1) 在 Java 程序中,对于数据的输入/输出操作以“流”(stream) 方式进行:2) J2SDK 提供了各种各样的“流”类,用以获取不同种类的数据:程序中通过标准的方法输入或输出数据 ...
- JAVA的节点流和处理流以及流的关闭顺序
今天在编写hadoop程序的时候,用到了流的处理.关闭流的时候出现了问题: 代码: FSDataInputStream fsin = fs.open(new Path(filein)); FSData ...
- 文件流FileStram类
本节课主要学习三个内容: 创建FileStram流 读取流 写入流 文件流FileStram类,是用来实现对文件的读取和写入.FileStram是操作字节的字节数组,当提供向文件读取和写入字节的方法时 ...
随机推荐
- 经过踩坑,搭建成功的Appium自动化测试环境
因为最近本人准备搞app自动化,所以就搭建环境过程记录下来(主要踩过好几个坑) 期间有点烦躁,后面调整了下心态还是成功弄好了. 一.Appium环境搭建准备软件 所需要到的软件如下: 1.安装JDK1 ...
- 面试被问为什么使用Spring Boot?答案好像没那么简单
面试官:项目中有使用Spring Boot吗? 小小白:用过. 面试官:说一下为什么要使用Spring Boot? 小小白:在使用Spring框架进行开发的过程中,需要配置很多Spring框架包的依赖 ...
- libevent(四)event_base 2
接上文libevent(三)event_base event_io_map event_list是双向链表,min_heap是小根堆,那event_io_map是什么呢? #ifdef WIN32 # ...
- MAC使用vagrant搭建开发环境
公司的开发环境是这样的: Windows主机通过虚拟机安装CentOS.平时在Windows下编辑代码,然后跑到虚拟机里编译. 我自己有台MAC,本来准备直接在MAC上装开发环境的.基于以下两个原因放 ...
- P2380狗哥采矿(状态不易设计)
描述:https://www.luogu.com.cn/problem/P2380 首先分析一下,易知传送带一定是要么向上,要么向右.且一定摆满了整个矩阵. 所以我们设 f [ i ] [ j ]表示 ...
- 2020 wannafly camp 补题 day1
题目可以从牛客上找到. 最简单的一个题应该是B B. 密码学 这个应该就是倒着推,题目给了你加密的顺序,所以我们逆推这个就可以得到每一次加密前的字符串. 1H. 最大公约数 题目大意就是给你一个范围1 ...
- C. Jury Marks 思维
C. Jury Marks 这个题目虽然是只有1600,但是还是挺思维的. 有点难想. 应该可以比较快的推出的是这个肯定和前缀和有关, x x+a1 x+a1+a2 x+a1+a2+a3... x+s ...
- 聚合类型与POD类型
Lippman在<深度探索C++对象模型>的前言中写道: I have heard a number of people over the years voice opinions sim ...
- 认识mysql3个基本库
一.3个基本库 数据库初始化安装完毕会有三个基本库mysql .information_schema.performace_schema.作为应用程序开发者,平时较少关注这些数据库尤其是后两者.但是通 ...
- node常用插件使用
1.nodemon 用于热更新,随时监控文件的变化 安装npm i -g nodemon 使用nodemon index.js 2.nvm nvm用于nodejs版本管理,我们在开发过程中,不同的项目 ...