FileInputStream/FileOutputStream的应用
这是一对继承于InputStream和OutputStream的类,用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象);
本地文件读写编程的基本过程为:
① 生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类);
② 调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容;
③ 关闭文件(close())。
实例:流文件读写
流文件的单元是字节,所以它不但可以读写文本文件,也可以读写图片、声音、影像文件,这种特点非常有用,因为我们可以把这种文件变成流,然后在网络上传输。
问题是有了通用的流文件以后,为什么还要专门的字符流呢?这是因为文本可以用不同的方式存储,可以是普通的文本(UTF-8编码方式),ASCII文本和Unicode文本,字符流对象可以进行必要的转换,从而读出正确的文本。
有人认为流文件不能读写文本文件,这其实是个误会,因为文本文件本质上也是由字节组成的,当然是流文件的一种。作为读写文件的全体,这是没问题的,但是,如果要处理每次读入的内容,就最好使用字符流。
所以在文本文件处理时,使用字符流是个最常用的方法。
样例:
import java.io.*;
public class FileStreamDemo {
public static void main(String[] args) throws IOException {
//创建两个文件,face.gif是已经存在文件,newFace.gif是新创建的文件
File inFile = new File("face.gif");
File outFile = new File("newFace.gif");
//创建流文件读入与写出类
FileInputStream inStream = new FileInputStream(inFile);
FileOutputStream outStream = new FileOutputStream(outFile);
//通过available方法取得流的最大字符数
byte[] inOutb = new byte[inStream.available()];
inStream.read(inOutb); //读入流,保存在byte数组
outStream.write(inOutb); //写出流,保存在文件newFace.gif中
inStream.close();
outStream.close();
}
}
实例:读写任意大文件应用
因为byte数组最大存储值不超过64M,所以当一个文件大于60M 的时候,需要分开几个流操作。我们把上面的程序作一个修改,就可以写入任意大小的文件。这个程序应用了FileInputStream类的方法如下:
read(byte[] b,int off,int len)
把特定位置的流内容读入数组,已经读入byte[]数组的内容,会在流文件中删除。
程序运行的结果会产生一个新文件。
样例:
import java.io.*;
public class FileStreamDemo2 {
public static void main(String[] args) throws IOException {
//创建两个文件
File inFile = new File("tcty36.rm");
File outFile = new File("newtcty36.rm");
//最大的流为60Mb,当文件的容量大于60Mb的时候便分开流
final int MAX_BYTE = 60000000;
long streamTotal = 0; //接受流的容量
int streamNum = 0; //流需要分开的数量
int leave = 0; //文件剩下的字符数
byte[] inOutb; //byte数组接受文件的数据
//创建流文件读入与写出类
FileInputStream inStream = new FileInputStream(inFile);
FileOutputStream outStream = new FileOutputStream(outFile);
//通过available方法取得流的最大字符数
streamTotal = inStream.available();
//取得流文件需要分开的数量
streamNum = (int)Math.floor(streamTotal/MAX_BYTE);
//分开文件之后,剩余的数量
leave = (int)streamTotal % MAX_BYTE;
//文件的容量大于60Mb时进入循环
if (streamNum > 0) {
for(int i = 0; i < streamNum; ++i){
inOutb = new byte[MAX_BYTE];
//读入流,保存在byte数组
inStream.read(inOutb, 0, MAX_BYTE);
outStream.write(inOutb); //写出流
outStream.flush(); //更新写出的结果
}
}
//写出剩下的流数据
inOutb = new byte[leave];
inStream.read(inOutb, 0, leave);
outStream.write(inOutb);
outStream.flush();
inStream.close();
outStream.close();
}
}
六、管道PipedInputStream/PipedOutputStream类:
当需要在两个线程中读写数据的时候,由于线程的并发执行,读写的同步问题可能会发生困难,这时候可以使用管道,管道事实上是一个队列。
管 道是由系统维护的一个缓冲区,当然程序员也可以自己直接指定该缓冲区的大小(只需要设置管道流类中的PIPE_SIZE属性的值)。当生产者生产出数据 后,只需要将数据写入管道中,消费者只需要从管道中读取所需要的数据。利用管道的这种机制,可以将一个线程的输出结果直接连接到另一个线程的输入端口,实 现两者之间的数据直接传送。
线程1
线程2
临时文件
管道
1.管道的连接:
方法之一是通过构造函数直接将某一个程序的输出作为另一个程序的输入,在定义对象时指明目标管道对象
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream(pInput);
方法之二是利用双方类中的任一个成员函数 connect()相连接
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream();
pinput.connect(pOutput);
2.管道的输入与输出:
输出管道对象调用write()成员函数输出数据(即向管道的输入端发送数据);而输入管道对象调用read()成员函数可以读起数据(即从输出管道中获得数据)。这主要是借助系统所提供的缓冲机制来实现的。
实例:Java的管道的输入与输出
import java.io.*;
public class PipedIO //程序运行后将sendFile文件的内容拷贝到receiverFile文件中
{
public static void main(String args[])
{
try
{
//构造读写的管道流对象
PipedInputStream pis=new PipedInputStream();
PipedOutputStream pos=new PipedOutputStream();
//实现关联
pos.connect(pis);
//构造两个线程,并且启动。
new Sender(pos,"c:\\text2.txt").start();
new Receiver(pis,"c:\\text3.txt").start();
}
catch(IOException e)
{
System.out.println("Pipe Error"+ e);
}
}
}
//线程发送
class Sender extends Thread
{
PipedOutputStream pos;
File file;
//构造方法
Sender(PipedOutputStream pos, String fileName)
{
this.pos=pos;
file=new File(fileName);
}
//线程运行方法
public void run()
{
try
{
//读文件内容
FileInputStream fs=new FileInputStream(file);
int data;
while((data=fs.read())!=-1)
{
//写入管道始端
pos.write(data);
}
pos.close();
}
catch(IOException e)
{
System.out.println("Sender Error" +e);
}
}
}
//线程读
class Receiver extends Thread
{
PipedInputStream pis;
File file;
//构造方法
Receiver(PipedInputStream pis, String fileName)
{
this.pis=pis;
file=new File(fileName);
}
//线程运行
public void run()
{
try
{
//写文件流对象
FileOutputStream fs=new FileOutputStream(file);
int data;
//从管道末端读
while((data=pis.read())!=-1)
{
//写入本地文件
fs.write(data);
}
pis.close();
}
catch(IOException e)
{
System.out.println("Receiver Error" +e);
}
}
}
七、随机文件读写:RandomAccessFile类
它直接继承于Object类而非InputStream/OutputStream类,从而可以实现读写文件中任何位置中的数据(只需要改变文件的读写位置的指针)。
编程步骤:
① 生成流对象并且指明读写类型;
② 移动读写位置;
③ 读写文件内容;
④ 关闭文件。
另外由于RandomAccessFile类实现了DataOutput与DataInput接口,因而利用它可以读写Java中的不同类型的基本类型数据(比如采用readLong()方法读取长整数,而利用readInt()方法可以读出整数值等)。
程序实例:
利用随机数据流RandomAccessFile类来实现记录用户在键盘的输入,每执行一次,将用户的键盘输入存储在指定的UserInput.txt文件中。
import java.io.*;
public class RandomFileRW
{
public static void main(String args[])
{
StringBuffer buf=new StringBuffer();
char ch;
try
{
while( (ch=(char)System.in.read()) !='\n')
{
buf.append(ch);
}
//读写方式可以为"r" or "rw"
RandomAccessFile myFileStream=new RandomAccessFile("c:\\UserInput.txt","rw");
myFileStream.seek(myFileStream.length()) ;
myFileStream.writeBytes(buf.toString());
//将用户从键盘输入的内容添加到文件的尾部
myFileStream.close();
}
catch(IOException e)
{
}
}
}
八、DataInput/DataOutput接口:
实
现与机器无关的各种数据格式读写(如readChar()
、readInt()、readLong()、readFloat(),而readLine()将返回一个String)。其中
RandomAccessFile类实现了该接口,具有比FileInputStream或FileOutputStream类更灵活的数据读写方式。
FileInputStream/FileOutputStream的应用的更多相关文章
- Java IO 之 FileInputStream & FileOutputStream源码分析
Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter ...
- Java基础知识强化之IO流笔记22:FileInputStream / FileOutputStream 复制文本文件案例1
1. 使用字节流FileInputStream / FileOutputStream 复制文本文件案例: 分析: (1)数据源:从哪里来 a.txt -- 读取数据 -- FileInpu ...
- FileInputStream FileOutputStream
FileInputStream is a stream to grab the information from files.Combined with FileOutputStream, we ca ...
- Java基础 FileInputStream/ FileOutputStream / 字节输入流 字节输出流实现文件的复制
FileInputStream/FileOutputStream的笔记: /**(FileInputStream/FileOutputStream四个步骤: ①声明②加载地址③read/write④c ...
- Java字节流:FileInputStream FileOutputStream
----------------------------------------------------------------------------------- FileInputStream ...
- java io读书笔记(8)FileInputStream/FileOutputStream的应用
转自:http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html 这是一对继承于InputStream和OutputStream的类,用 ...
- Java API —— IO流( FileInputStream & FileOutputStream & BufferedInputStream & BufferedOutputStream )
1.IO流概述 · IO流用来处理设备之间的数据传输 · 上传文件和下载文件 · Java对数据的操作是通过流的方式 · Java用于操作流的对象都在IO包中 2.IO ...
- Java基础知识强化之IO流笔记26:FileInputStream / FileOutputStream 复制mp4视频的案例
1. 需求:把D:\\English.mp4 复制到当前项目目录下copy.mp4 代码示例: package com.himi.filecopy; import java.io.FileInput ...
- Java基础知识强化之IO流笔记25:FileInputStream / FileOutputStream 复制图片案例
1. 需求:把D:\\美女.jpg 复制到当前项目目录下mn.jpg 代码示例: package com.himi.filecopy; import java.io.FileInputStream; ...
- Java基础知识强化之IO流笔记24:FileInputStream / FileOutputStream 复制文本文件案例2
1. 需求:把d盘下的a.txt的内容复制到f盘下的b.txt中: 代码示例: package com.himi.filecopy; import java.io.FileInputStream; i ...
随机推荐
- Linux系统kernel参数优化
目录 iptables相关 单进程最大打开文件数限制 内核TCP参数方面 内核其他TCP参数说明 众所周知在默认参数情况下Linux对高并发支持并不好,主要受限于单进程最大打开文件数限制.内核TCP参 ...
- python爬虫的基本思路
爬虫:请求网站并提取数据的自动化程序. 流程: 发送请求 -> 获取数据 -> 解析数据 -> 存储数据
- 2018年江西理工大学C语言程序设计竞赛(高级组) 三角平方数
题目描述 三角数:形如图a,圆点摆放成等边三角形的数字,则为三角数. (图a) 平方数:形如图b,小方块摆放成正方形的数字,则为平方数. (图b) 那么如果一个数字既是三角形数又是平方数,则称为三角平 ...
- 回顾Scrum学习:《Scrum实战》第4次课【全职的Scrum Master】作业
回顾Scrum学习 1.回顾目标 1.1 期望结果 了解和学习Scrum,为将来换方向打好理论基础 如果能在目前公司引入和推行也很好,但是根据目前公司的文化氛围来看,推行希望不大 把敏捷思想应用到 ...
- imageX
imageX 编辑 ImageX 是一个命令行工具,原始设备制造商 (OEM) 和公司可以使用它来捕获.修改和应用基于文件的磁盘映像以进行快速部署.ImageX 可以使用 Windows 映像 (.w ...
- MFC定时关机程序的实现1
写个定时关机程序自用,界面简单实用,最终界面如下 第一步,新建一个MFC对话框应用程序,拖几个控件过来, 界面如下: 然后给下拉列表框,复选按钮绑定变量,以方便进行操作. CComboBox m_co ...
- Python基础-week03 集合 , 文件操作 和 函数详解
一.集合及其运算 1.集合的概念 集合是一个无序的,不重复的数据组合,它的主要作用如下 *去重,把一个列表变成集合,就自动去重了 *关系测试,测试两组数据之前的交集.并集.差集.子集.父级.对称差集, ...
- Python中的Json模块dumps、loads、dump、load函数介绍
Json模块dumps.loads.dump.load函数介绍 1.json.dumps() json.dumps() 用于将dict类型的数据转成str,因为如果直接将dict类型的数据写入json ...
- Kafka SSL 配置
#!/bin/bash # 生成服务器keystore(密钥和证书)keytool -keystore server.keystore.jks -alias machine03.zheng.com - ...
- Python面向对象之私有方法(4)
类里面有很多成员修饰符,用来修饰各种属性 (1)私有属性,只有内部的方法可以访问 class Foo: xo = 'xo'#表明是公共的,内部外部都可以访问 __ox = '私有属性'#私有属性,只有 ...