节点流(文件流)

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的更多相关文章

  1. java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例

    FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...

  2. 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝

    两个类的简述 专门用来对文件进行读写的类. 父类是InputStream.OutputStream 文件读入细节 FileOutputStream流的构造方法:new FileOutputStream ...

  3. io流-文件流\节点流

    FileOutputStream类(jdk1.0) 描述 java.io.FileOutputStream 类是文件字节输出流,用于将数据写入到文件中. 构造方法 //构造方法 FileOutputS ...

  4. 用内存流 文件流 资源生成客户端(Delphi开源)

    正文:很多木马生成器就是用的内存流和文件流生成客户端的,废话不多说了,代码如下: unit Main; interface usesWindows, Messages, SysUtils, Varia ...

  5. csv内存流文件流

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  6. Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)

    1.操作基本数据类型的流     1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...

  7. java 文件字节和字符流 缓冲流

    流的原理 1) 在 Java 程序中,对于数据的输入/输出操作以“流”(stream) 方式进行:2) J2SDK 提供了各种各样的“流”类,用以获取不同种类的数据:程序中通过标准的方法输入或输出数据 ...

  8. JAVA的节点流和处理流以及流的关闭顺序

    今天在编写hadoop程序的时候,用到了流的处理.关闭流的时候出现了问题: 代码: FSDataInputStream fsin = fs.open(new Path(filein)); FSData ...

  9. 文件流FileStram类

    本节课主要学习三个内容: 创建FileStram流 读取流 写入流 文件流FileStram类,是用来实现对文件的读取和写入.FileStram是操作字节的字节数组,当提供向文件读取和写入字节的方法时 ...

随机推荐

  1. C# 基础知识系列- 14 IO篇 流的使用

    0. 前言 继续之前的C# IO流,在前几篇小短片中我们大概看了下C# 的基础IO也对文件.目录和路径的操作有了一定的了解.这一篇开始,给大家演示一下流的各种操作.以文件流为例,一起来看看如何操作吧. ...

  2. 多线程高并发编程(7) -- Future源码分析

    一.概念 A Future计算的结果. 提供方法来检查计算是否完成,等待其完成,并检索计算结果. 结果只能在计算完成后使用方法get进行检索,如有必要,阻塞,直到准备就绪. 取消由cancel方法执行 ...

  3. <学习笔记: Django之初见>

    Django 1. web框架介绍 具体介绍Django之前,必须先介绍WEB框架等概念. web框架: 别人已经设定好的一个web网站模板,你学习它的规则,然后“填空”或“修改”成你自己需要的样子. ...

  4. Flutter 首页必用组件NestedScrollView

    老孟导读:昨天Flutter 1.17版本重磅发布,新的版本主要是优化性能.修复bug,有人觉得此版本毫无亮点,但也从另一方面体现了Flutter目前针对移动端已经较为完善,想了解具体内容,文末有链接 ...

  5. LeetCode--LinkedList--141.Linked List Cycle(Easy)

    141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a l ...

  6. Coursera课程笔记----C程序设计进阶----Week 5

    指针(二) (Week 5) 字符串与指针 指向数组的指针 int a[10]; int *p; p = a; 指向字符串的指针 指向字符串的指针变量 char a[10]; char *p; p = ...

  7. Centos7 使用 Ansible 批量安装中文字体

    需求背景 Centos7 下 Java 生成图片水印时中文乱码,原因是没有安装中文字体. 安装中文字体 以下是基于 Centos7 手动安装中文字体的详细步骤.当测试或者生产环境服务器比较多的时候,建 ...

  8. docker redis shell

    docker中安装好redis后,运行 docker ps 指令,查看所有运行中的镜像信息 然后运行 docker inspect --format "{{ .State.Pid}}&quo ...

  9. HBase Filter 过滤器之QualifierFilter详解

    前言:本文详细介绍了 HBase QualifierFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.QualifierFilter 基于列名进行过滤, ...

  10. 花店橱窗布置问题(FLOWER)

    目录 问题描述 问题分析 Java代码实现 运行结果 今天老师上完课说所有花都要被放,这个算法还是考虑多了,包含了这个选择,代码就不给了,用dp思想就可以解决了. 问题描述   假设你想以最美观的方式 ...