文件字符流FileReader 与 FileWriter
类FileReader用于读取已过滤的字符流的抽象类。
类Filewriter用来写入字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的。要自己指定这些值,可以先在 FileOutputStream 上构造一个 OutputStreamWriter。
文件是否可用或是否可以被创建取决于底层平台。特别是某些平台一次只允许一个 FileWriter(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。
FileWriter 用于写入字符流。要写入原始字节流,请考虑使用 FileOutputStream。
类 BufferedReader 与 类 BufferedWriter
类 BufferedReader:
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
通常,Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议用 BufferedReader 包装所有其 read() 操作可能开销很高的 Reader(如 FileReader 和 InputStreamReader)。例如,
BufferedReader in
= new BufferedReader(new FileReader(“foo.in”));
将缓冲指定文件的输入。如果没有缓冲,则每次调用 read() 或 readLine() 都会导致从文件中读取字节,并将其转换为字符后返回,而这是极其低效的。
类 BufferedWriter:
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
可以指定缓冲区的大小,或者接受默认的大小。在大多数情况下,默认值就足够大了。
该类提供了 newLine() 方法,它使用平台自己的行分隔符概念,此概念由系统属性 line.separator 定义。并非所有平台都使用新行符 (’\n’) 来终止各行。因此调用此方法来终止每个输出行要优于直接写入新行符。
通常 Writer 将其输出立即发送到底层字符或字节流。除非要求提示输出,否则建议用 BufferedWriter 包装所有其 write() 操作可能开销很高的 Writer(如 FileWriters 和 OutputStreamWriters)。例如,
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter(“foo.out”)));
将缓冲 PrintWriter 对文件的输出。如果没有缓冲,则每次调用 print() 方法会导致将字符转换为字节,然后立即写入到文件,而这是极其低效的。
查看下面的代码:实现文本字符内容的复制
package com.bisxt.io;
import java.io.*;
public class TestFileReaderandFileWriter {
public static void main(String[] args) {
//选择源
File sourceFile = new File("F:\\LJ.txt");
File destPosition = new File("F:\\WODE.txt");
//创建流
try {
FileReader fileReader = new FileReader(sourceFile);
FileWriter fileWriter = new FileWriter(destPosition);
char[] temp = new char[1024];//缓存字节数组变成 字符数组
int len;
while ((len = fileReader.read(temp)) != -1){
fileWriter.write(temp,0,temp.length);
fileWriter.flush();
System.out.println(len);
String str = new String(temp,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
将上面代码使用BufferReader 与 BufferWriter 进行包装,从而提高读写效率;
package com.bisxt.io;
import java.io.*;
public class TestFileReaderandFileWriter02 {
public static void main(String[] args) {
//选择源
File sourceFile = new File("F:\\LJ.txt");
File destPosition = new File("F:\\WODE.txt");
//创建流
try {
BufferedReader fileReader = new BufferedReader(new FileReader(sourceFile));
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(destPosition));
char[] temp = new char[1024];//缓存字节数组变成 字符数组
int len;
while ((len = fileReader.read(temp)) != -1){
fileWriter.write(temp,0,temp.length);
fileWriter.flush();
System.out.println(len);
String str = new String(temp,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
节点流
可以从或向一个特定的地方(节点)读写数据。
如:FileReader、FileWriter、FileInputStream、FileOutputStream等文件进行处理的节点流。
处理流
是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写。
如BufferedReader。
处理流的构造方法总是要带一个其他的流对象做参数。
一个流对象经过其他流的多次包装,称为流的链接。
BufferedImputStrean
BufferedOutputStream
BufferedReader
BufferedWrite
ByteArrayInputStream类 与 ByteArrayOutputStream类
ByteArrayInputStream类
字节数组输入流在内存中创建一个字节数组缓冲区,从输入流读取的数据保存在该字节数组缓冲区中。创建字节数组输入流对象有以下几种方式。
接收字节数组作为参数创建:
ByteArrayInputStream bArray = new ByteArrayInputStream(byte [] a)
另一种创建方式是接收一个字节数组,和两个整形变量 off、len,off表示第一个读取的字节,len表示读取字节的长度
ByteArrayInputStream bArray = new ByteArrayInputStream(byte []a, int off, int len)
ByteArrayOutputStream类
字节数组输出流在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组缓冲区中。创建字节数组输出流对象有以下几种方式。
下面的构造方法创建一个32字节(默认大小)的缓冲区。
OutputStream bOut = new ByteArrayOutputStream();
另一个构造方法创建一个大小为n字节的缓冲区
OutputStream bOut = new ByteArrayOutputStream(int a)
此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。
特别注意不需要目的地
查看下面代码:
package com.bisxt.io;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class TestByteArrayInputStream {
public static void main(String[] args) throws IOException {
//创建源
//getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
byte[] date = "talk is cheap show me the code".getBytes();//源头变成字节数组而不是File对象
//创建流
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(date);
byte temp[] = new byte[1024];//缓存容器
int len;//read(byte[] temp)返回一次性读取的字节数
while((len = byteArrayInputStream.read(temp)) != -1){
String str = new String(temp,0, temp.length);
System.out.println(str);
}
}
}
查看下面的代码,ByteArrayInputStream 与 ByteArrayOutputStream的综合应用 实现任何文件类型的拷贝,且对方法进行了封装
package com.bisxt.io;
import java.io.*;
public class TestByteArrayInputStream02 {
public static void main(String[] args) {
/**
* 文件读取到字节数组里面去
* 字节数组写道文件即还原
/**
* File src = new File("D:\\IO\\src\\abc.txt"); File构造器作用:通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
*/
//图片转换到内存里面的字节数组
byte[] dates = fileToByteArray("E:\\KJ.PNG");
if(dates != null){
System.out.println(dates.length);
}
//字节数组转换到图片
byteToFile(dates,"F:\\jl.PNG");
}
/**
* 图片读取到字节数组
* A)图片到程序 FileInputStream
* B)程序到字节数组(内存里面的字节数组缓冲区)ByteArrayOutputStream
*/
public static byte[] fileToByteArray(String filePath){
//创建源
File src2 = new File(filePath);
byte[] date = null;//目的地
//创建流
InputStream is;
ByteArrayOutputStream BAOS;
try {
is = new FileInputStream(src2); //文件输入流
BAOS = new ByteArrayOutputStream(); //字节数组输出流
try {
//操作
byte[] temp = new byte[1024*10];//缓存容器 10K
//read(byte[] b)从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
//并且方法会返回一次性读取的字节数
int len ;//read(byte[] b)返回一次性读取的字节数
while((len = is.read(temp))!=-1){
BAOS.write(temp,0, len);//写出到字节数组
}
BAOS.flush();
return BAOS.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null; //放在try catch finally语句之外
}
/**
* 字节数组写出到图片
* A)字节数组到程序 ByteArrayInputStream
* B)程序到文件 FileOutputStream
*/
public static void byteToFile(byte[] src, String filePath) {
//创建源
File dest = new File(filePath);//文件不存在的话,系统会自动帮助创建
//创建流
InputStream is;
OutputStream os = null;
try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] temp = new byte[1024 * 10];//10k 缓冲容器
int len;
while ((len = is.read(temp)) != -1) {
os.write(temp, 0, len);
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();//FileOutputStream文件资源需要释放
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
数据输入/输出流 DateInputStream 与 DateOutputStream
数据输入/输出流(DateInputStream类与DateOutputStream类)允许应用程序以与机器无关的方式从底层输入流中读取基本Java数据类型。也就是说,当读取一个数据时,不必在关心这个数值应当是哪种字节
DateInputStream类:
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。
DataInputStream 对于多线程访问不一定是安全的。 线程安全是可选的,它由此类方法的使用者负责。
DateOutputStream类
数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。
注意:数据流操作的对象是基本数据类型与字符串;必须先写出后读取;读取的顺序与写出的顺序保持一致
查看下面的代码:
package com.bisxt.io;
import java.io.*;
public class TestDateStream {
public static void main(String[] args) throws IOException {
//写出
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
//操作数据类型+数据
dos.writeUTF("编");
dos.writeInt(12);
dos.writeBoolean(false);
dos.writeChar('a');
byte[] dates = baos.toByteArray();
//读取
DataInputStream dis = new DataInputStream((new ByteArrayInputStream(dates)));
//顺序与写出一致
String msg = dis.readUTF();
int a = dis.readInt();
boolean so = dis.readBoolean();
char ch = dis.readChar();
System.out.println(a);
}
}