文件操作与序列化
1、读取文件
1.1、字符流读取文件
public static void readInFile() throws IOException {
String fileName = "test03.txt";
FileReader fileReader = null;
try {
System.out.println("一次读取单个字符:");
fileReader = new FileReader(fileName);
int tmp=-1; //实际是去存储四个字节
Charset charset = Charset.defaultCharset();
System.out.println("当前系统默认编码:"+charset);
while((tmp = fileReader.read())!=-1){
System.out.print((char)tmp);
}
System.out.println();
System.out.println("一次读取多个字符:");
Reader reader = null;
char[] tempchars = new char[25];
reader = new InputStreamReader(new FileInputStream(fileName));
while ((tmp = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((tmp == tempchars.length) && (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < tmp; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
reader.close();
} catch (IOException e){
e.printStackTrace();
} finally{
//关闭流对象
fileReader.close();
}
}
1.2、字节流读取文件
public static void readBytes(String fileName) throws IOException {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(fileName);
int tempbyte;
while ((tempbyte = inputStream.read()) != -1) {
System.out.write((byte)tempbyte);
System.out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStream != null){
inputStream.close();
}
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread;
in = new FileInputStream(fileName);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
1.3、随机读取方式
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (int) (fileLength*(Math.random()));
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
1.4、行读取方式
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
1.5 BufferedInputStream、BufferedOutputStream(缓冲字节流)
缓冲字节流是为高效率而设计的,真正的读写操作还是靠FileOutputStream和FileInputStream,所以其构造方法入参是这两个类的对象也就不奇怪了。
public class IOTest {
public static void write(File file) throws IOException {
// 缓冲字节流,提高了效率
BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(file, true));
// 要写入的字符串
String string = "松下问童子,言师采药去。只在此山中,云深不知处。";
// 写入文件
bis.write(string.getBytes());
// 关闭流
bis.close();
}
public static String read(File file) throws IOException {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
// 一次性取多少个字节
byte[] bytes = new byte[1024];
// 用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 读取到的字节数组长度,为-1时表示没有数据
int length = 0;
// 循环取数据
while ((length = fis.read(bytes)) != -1) {
// 将读取的内容转换成字符串
sb.append(new String(bytes, 0, length));
}
// 关闭流
fis.close();
return sb.toString();
}
}
1.6 BufferedReader、BufferedWriter(字符缓冲流)
字符流适用于文本文件的读写,OutputStreamWriter类其实也是借助FileOutputStream类实现的,故其构造方法是FileOutputStream的对象
public class IOTest {
public static void write(File file) throws IOException {
// BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream(file, true), "UTF-8"));
// FileWriter可以大幅度简化代码
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
// 要写入的字符串
String string = "松下问童子,言师采药去。只在此山中,云深不知处。";
bw.write(string);
bw.close();
}
public static String read(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
// 用来接收读取的字节数组
StringBuilder sb = new StringBuilder();
// 按行读数据
String line;
// 循环取数据
while ((line = br.readLine()) != null) {
// 将读取的内容转换成字符串
sb.append(line);
}
// 关闭流
br.close();
return sb.toString();
}
}
2、文件拷贝
3.1、字符流拷贝
private static void copy() {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader("test03.txt");
writer = new FileWriter("b.txt");
//一个一个字符拷贝
// int tmp = 0;
// while((tmp = reader.read()) != -1){
// writer.write(tmp);
// }
//缓冲数组
char[] buf = new char[256];
int len = 0;
while((len = reader.read(buf)) != -1){
writer.write(buf, 0, len);
//字符数组 写入
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.2、字节流拷贝
package Metaphase06;
import java.io.*;
/**
* 字节流
*
*/
public class byteStreamTest {
public static void main(String[] args) throws IOException {
String fileName = "D:\\program\\ProgramOfIdea\\MyJavaStudy2\\src\\Metaphase06\\test01.jpg";
String fileName2 = "D:\\program\\ProgramOfIdea\\MyJavaStudy2\\src\\Metaphase06\\result01.jpg";
String fileName3 = "D:\\program\\ProgramOfIdea\\MyJavaStudy2\\src\\Metaphase06\\result02.jpg";
File file = new File(fileName);
File file2 = new File(fileName2);
File file3 = new File(fileName3);
if (!file2.exists()) {
file2.createNewFile();
} else {
System.out.println("result01.jpg存在!");
}
if (!file3.exists()) {
file3.createNewFile();
} else {
System.out.println("result02.jpg存在!");
}
FileInputStream fileInputStream = new FileInputStream(file);
FileInputStream fileInputStream2 = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(file2);
FileOutputStream fileOutputStream2 = new FileOutputStream(file3);
System.out.println("1正在读取复制......");
int tmp;
while ((tmp = fileInputStream.read()) != -1) {
fileOutputStream.write(tmp);
}
fileInputStream.close();
fileOutputStream.close();
System.out.println("2正在读取复制......");
byte[] tmpByte = new byte[1024];
int byteRead = 0;
byteStreamTest.showAvailableBytes(fileInputStream2);
while ((byteRead = fileInputStream2.read(tmpByte)) != -1) {
fileOutputStream2.write(tmpByte,0,byteRead);
}
fileInputStream2.close();
fileOutputStream2.close();
}
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、序列化与反序列化
1、创建可以序列化的类以便获取可序列化的对象
public class Employee implements Serializable {
public String name;
public String address;
public transient int SSN; //不能序列化
public int number;
public void mailCheck() {
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
2、执行序列化(把对象的信息存储到磁盘上,网络上处理高并发的数据时可以采用序列化将数据储存到磁盘上,“慢慢处理”,且保证数据的完整性;transient关键字表示该变量不能被序列化,即不能被存储下来,多用来对不良关键字传播的限制)。
public class SerializeDemo {
public static void main(String [] args) {
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try {
FileOutputStream fileOut = new FileOutputStream("D:\\program\\ProgramOfIdea\\MyJavaStudy2\\src" +
"\\序列化\\mySerialize.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in mySerialize.txt");
}catch(IOException i) {
i.printStackTrace();
}
}
}
3、执行反序列化
public class DeserializeDemo {
public static void main(String [] args) {
Employee e = null;
try {
FileInputStream fileIn = new FileInputStream("D:\\program\\ProgramOfIdea\\MyJavaStudy2\\src" +
"\\序列化\\mySerialize.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i) {
i.printStackTrace();
return;
}catch(ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
部分代码和思路借鉴于很多博客,如有侵权请联系删除。
5、缓冲流
先百度一下:缓存流是计算机领域中输入输出流的一种常见形式。包括BufferedInputStream/BufferedOutputStream (字节缓冲流)类和BufferedReader/ BufferedWriter(字符缓冲流)类。
这种流把数据从原始流成块读入或把数据积累到一个大数据块后再成批写出,通过减少系统资源的读写次数来加快程序的执行。BufferedOutputstream 或BufferedWriter 类仅仅在缓冲区满或调用flush()方法时才将数据写到目的地。
缓存流是过滤流,在创建具体流时需要给出一个InputStream / OutputStream 类型的流座位前端流,并可以指明缓冲区的大小。
//行都去的时候用到了缓冲流,见上行读取文件的方式
public static void main(String[] args) throws Exception {
//创建缓冲字节输入流对象指定数据源
BufferedInputStream inStream = new BufferedInputStream(new FileInputStream("D:\\program\\ProgramOfIdea" +
"\\MyJavaStudy2\\src\\Metaphase06\\test01" +
".jpg"));
//创建缓冲字节输出流对象指定数据源
BufferedOutputStream otStream = new BufferedOutputStream(new FileOutputStream("20201114.jpg"));
//读写数据
//定义长度
byte[] bytes = new byte[1024];
//定义长度
int len;
while ((len = inStream.read(bytes)) != -1) {
//导出数据
otStream.write(bytes, 0, len);
}
//关闭资源
otStream.close();
inStream.close();
}
//以下为字节缓冲流示例
总结:
InputStreamReader和OutputStreamWriter
在构造这两个类对应的流时,它们会自动进行转换,将平台缺省的编码集编码的字节转换为Unicode字符。对英语环境,其缺省的编码集一般为ISO8859-1。
BufferedReader和BufferedWriter
这两个类对应的流使用了缓冲,能大大提高输入输出的效率。这两个也是过滤器流,常用来对InputStreamReader和OutputStreamWriter进行处理。
注意:普通字节流在传字节数组的时候比高效字节流快.
字符缓冲流特有的方法:
- BufferedReader:public String readLine(): 读一行文字。
- BufferedWriter:public void newLine(): 写一行行分隔符,由系统属性定义符号。
缓冲流引用 :https://blog.youkuaiyun.com/weixin_30872671/article/details/95289763