java io流
1.流
一连串有顺序的数据系列可以看成一个流,它还代表任何有能力产出数据的数据源对象或者是有能力接收数据的接收端对象。
简单的理解:流是数据和数据处理过程的统称
流操作关心三部分内容:数据源、目标以及过程。
数据源包括:
- 字节数组
- String对象
- 文件
- 网络数据流
- 其他等等
流分类:
- 按流的方向:输入流和输出流
输入流:数据流入程序
输出流:数据流出程序
- 按数据单位:字节流和字符流
字节流:数据流中最小的数据单元是字节
字符流:数据流中最小的数据单元是字符,Java中的字符是Unicode编码,一个字符占用两个字节
- 按功能:节点流和过滤流
节点流直接连接到数据源,处理流是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现增强的数 据读写功能,它并不直接连到数据源。
java io流类结构图:
具体每个类简介查看:https://docs.oracle.com/javase/7/docs/api/java/io/package-frame.html
2.实例
下面主要通过程序来学习:
import java.io.*;
public class TestMain {
public static void main(String[] args) {
fileInputStreamTest();
System.out.println("\n--------------");
fileReaderTest();
System.out.println("\n--------------");
fileOutputStreamTest();
System.out.println("\n--------------");
fileWriterTest();
}
public static void fileInputStreamTest(){
try{
//创建字节输入流
FileInputStream fis = new FileInputStream("C:\\Users\\15391\\Desktop\\aaa.txt");
byte[] bbuf = new byte[128];
int hasRead = 0;
while ((hasRead = fis.read(bbuf)) > 0) {
System.out.print(new String(bbuf, 0, hasRead));
}
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void fileOutputStreamTest(){
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream("C:\\Users\\15391\\Desktop\\aaa.txt");
fos=new FileOutputStream("C:\\Users\\15391\\Desktop\\bbb.txt");
byte[] buf=new byte[128];
int len=0;
while((len=fis.read(buf))>0){
fos.write(buf,0,len);
}
fis.close();
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void fileReaderTest(){
try{
FileReader fr=new FileReader("C:\\Users\\15391\\Desktop\\aaa.txt");
char[] cbuf=new char[32];
int hasRead=0;
while((hasRead=fr.read(cbuf))>0){
System.out.print(new String(cbuf , 0 , hasRead));
}
fr.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void fileWriterTest(){
FileWriter fw = null;
try {
//创建字符输出流
fw = new FileWriter("C:\\Users\\15391\\Desktop\\poem.txt");
fw.write("锦瑟 - 李商隐\r\n");
fw.write("锦瑟无端五十弦,一弦一柱思华年。\r\n");
fw.write("庄生晓梦迷蝴蝶,望帝春心托杜鹃。\r\n");
fw.write("沧海月明珠有泪,蓝田日暖玉生烟。\r\n");
fw.write("此情可待成追忆,只是当时已惘然。\r\n");
fw.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
以上是文件字节输入输出流和文件字符输入输出流使用的实例
JDK7中,InputStream/OutputStream实现了 AutoCloseable接口,所以可以在try…catch中 使用而不需要显式关闭。
try (InputStream inputStream1 = Files.newInputStream(……) {
//....
} catch (IOException e) {
//....
}
流的流:
FileInputStream fin=new FileInputStream(文件名);
DataInputStream din=new DataInputSteam(fin);
Double s=din.readDouble();
- DataInputStream提供了向流中写入常用数据类型 (如int,char,double等)的能力。
- 上述这种代码创建的流对象“首尾相接”,称为“流对象链”,链尾的流对象是程序中真正用到的流对象
- DataInputStream就是前面说的“处理流”,而FileInputStream就是前面说的“节点流“
PrintStream:
- 常用的System.out被称为“标准输出流”, 它是PrintStream类型的实例。
- PrintStream构造于OutputStream对象之上
public PrintStream(OutputStream out)
- FileOutputStream转换为PrintStream,从而可以很方便地向文件中输出各种类型的数据
import java.io.*;
public class TestMain {
public static void main(String[] args) throws IOException{
PrintStream ps=null;
try{
FileOutputStream fos = new FileOutputStream("test.txt");
ps=new PrintStream(fos);
ps.println("输出字节流:");
ps.println(new TestMain());
}catch (IOException e){
e.printStackTrace();
}finally{
ps.close();
}
}
}
缓存技术:
Java使用了文件缓冲技术,优点有:提高了I/O的性能,在内存中开辟一块区域,称为缓冲区,当缓冲区满时一次写入到磁盘中。
使用事例:
BufferedReader in=new BufferedReader(new FileReader(“input.txt”));
String line=in.readLine();
BufferedReader in=Files.newBufferedReader(Paths.get(“input.txt”), Charset.forName(“utf-8”) );//可以使用其他编码方式
相关类:BufferedInputStream/BufferedOutputStream: 适合于包容二进制数据的文件,BufferReader/BufferWriter:适合于读写文本文件。
BufferedInputStream/BufferedOutputStream使用:
import java.io.*;
public class TestMain {
public static void main(String[] args) throws IOException{
try {
byte[] data = new byte[1];
File srcFile = new File("C:\\Users\\15391\\Desktop\\aaa.txt");
File desFile = new File("C:\\Users\\15391\\Desktop\\aaa.txt.bak");
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(desFile));
System.out.println("复制文件:" + srcFile.length() + "字节");
while(bufferedInputStream.read(data) != -1) {
bufferedOutputStream.write(data);
}
// 将缓冲区中的数据全部写出
bufferedOutputStream.flush();
bufferedInputStream.close();
bufferedOutputStream.close();
System.out.println("复制完成");
}
catch(IOException e) {
e.printStackTrace();
}
}
}
BufferReader/BufferWriter使用:
import java.io.*;
public class TestMain {
public static void main(String[] args) throws IOException{
try {
System.out.println("输入文本,键入quit结束");
// 缓冲System.in输入流
BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));
// 缓冲FileWriter字符输出流
BufferedWriter bufWriter = new BufferedWriter(new FileWriter("C:\\Users\\15391\\Desktop\\bbb.txt"));
String input = null;
// 每读一行进行一次写入动作
while(!(input = bufReader.readLine()).equals("quit")) {
bufWriter.write(input);
// newLine()方法写入与操作系统相依的换行字符
bufWriter.newLine();
}
bufReader.close();
bufWriter.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
流转换:
System.in(InputStream类型实例)转为字符输入流BufferedReader:
import java.io.*;
public class TestMain {
public static void main(String[] args) throws IOException{
BufferedReader br = null;
try {
//将Sytem.in对象转换成Reader对象
InputStreamReader reader = new InputStreamReader(System.in);
//将普通Reader包装成BufferedReader
br = new BufferedReader(reader);
String buffer = null;
//采用循环方式来一行一行的读取
while ((buffer = br.readLine()) != null) {
//如果读取的字符串为"exit",程序退出
if (buffer.equals("exit")) {
System.out.println("程序退出……");
System.exit(1);
}
//打印读取的内容
System.out.println("输入内容为:" + buffer);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} //关闭输入流
finally {
try {
br.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
内存流:使用ByteArrayInputStream和ByteArrayOutputStream两个类将Byte数组作为读写源头和目的地
随机存取文件:使用RandomAccessFile类
附File类使用:
//创建一个文件路径
File file = new File("D:\\testData.txt");
if(file.exists()){
//得到文件路径
System.out.println(file.getAbsolutePath());
//得到文件大小
System.out.println("文件大小:"+file.length());
}
//创建文件和创建文件夹
File file1 = new File("d:\\iotest.txt");
if(!file1.exists())
{
try {
file1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("文件已存在");
}
//创建文件夹
File file2 = new File("d:\\testIO");
if(file2.isDirectory())
{
System.out.println("文件夹存在");
}else{
file2.mkdir();
}
//列出一个文件夹下的所有文件
File f = new File("d:\\testIO");
if(f.isDirectory())
{
File lists[] = f.listFiles();
for(int i=0;i<lists.length;i++)
{
System.out.println(lists[i].getName());
}
}
参考:
金旭亮Java编程系列(大部分内容)