Java(11)文件操作

Java(11)文件操作

一.基本概念

1.1.数据持久化

  1. 只存储类的属性
  2. 存储对象-对象序列化

1.2.文件的数据结构

1.框架

2.文件组织

  • 记录-record
    • 域-field
      • 字节-byte
      • 位-bit

1.3.缓冲

临时存放的位置

1.4.流

1.概念

数据序列

2.流的不同层次

3.读写

  1. 顺序文件
  2. 随机文件

4.注意

所有流都要设置异常处理

1.5.常见流

1.输入流

  1. 字节输入流
    InputStream
  2. 字符输入流
    Reader

2.输出流

  1. 字节输出流
    OutputStream
  2. 字符输出流

Writer

3.文件操作

  1. 文件类
    File
  2. 随机存取文件
    RandomAccessFile

4.总结

二.举例

2.1.字节读写

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



public class FileStream {
    
    
	public Boolean StreamWriter(byte[] message,String path) {
    
    
		FileOutputStream outputStream;
		try {
    
    
			outputStream = new FileOutputStream(path);
			outputStream.write(message);
			outputStream.close();
		}catch (FileNotFoundException e) {
    
    
			// TODO: handle exception
			e.printStackTrace();
		}
		catch (IOException e) {
    
    
			// TODO: handle exception
			e.printStackTrace();
		}
		return true;
	}
	public Boolean StreamReader(String path) {
    
    
		FileInputStream inputStream;
		byte[] message = new byte[50];
		try {
    
    
			inputStream = new FileInputStream(path);
			inputStream.read(message);
			inputStream.close();
		}catch (FileNotFoundException e) {
    
    
			// TODO: handle exception
			e.printStackTrace();
		}
		catch (IOException e) {
    
    
			// TODO: handle exception
			e.printStackTrace();
		}
		return true;
	}
	public static void main(String[] args) {
    
    
		byte[] message  = "中南大".getBytes();
		String path = "./text.txt";
		FileStream fileStream = new FileStream();
		fileStream.StreamWriter(message, path);
		System.out.println(fileStream.StreamReader(path));
	}
}

2.2.字符读写

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;



public class FileIo {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		char[] message = "中南大".toCharArray();
		String path = "./File_IO.txt";
		CharIO io = new CharIO();
		try {
    
    
			io.charWrite(path,message);
		} catch (IOException e) {
    
    
			// TODO: handle exception
			System.out.println(e);
		}
		char[] text = io.charRead(path);
		String text1 = new String(text);
		System.out.println(text1);
	}
	

}
class CharIO{
    
    
	public boolean charWrite(String name,char[] message) throws IOException  {
    
    
		File file = new File(name);
		FileWriter fileWriter = new FileWriter(file);
		fileWriter.write(message);
		fileWriter.close();
		System.out.println("文件读写成功"+String.valueOf(message));
		return true;
	}
	public char[] charRead(String name) {
    
    
		char[] text = new char[20];
		FileReader fileReader;
		try {
    
    
			fileReader = new FileReader(name);
			fileReader.read(text);
			fileReader.close();
		} catch (IOException e) {
    
    
			// TODO: handle exception
			e.printStackTrace();
		}
		return text;
		
	}
}

2.3.随机读写

import java.io.File;
import java.io.RandomAccessFile;


public class RandomIO {
    
    
	public static void main(String[] args) {
    
    
		Student[] studentArray = {
    
    new Student("关于","123"),
				new Student("郭德纲", "456"),
				new Student("叶蕴屏", "789")};
		RandomAccessFile randomWriter = null;
		RandomAccessFile randomReader = null;
		try {
    
    
			File file = new File("Student.txt");
			randomWriter = new RandomAccessFile(file, "rw");
			for(int i=0;i<studentArray.length;i++) {
    
    
				randomWriter.write(studentArray[i].getName().getBytes());
				randomWriter.write(studentArray[i].getNumber().getBytes());
			}
			randomWriter.close();
			int len = 0;
			String str = null;
			int length = (int)file.length()/studentArray.length;
			byte[] buf = new byte[length];
			randomReader = new RandomAccessFile(file, "r");
			//跳过前面字符
			randomReader.skipBytes(length);
			System.out.println("指针位置是"+randomReader.getFilePointer());
			len = randomReader.read(buf);
			
			//转到字符串
			str = new String(buf,0,len);
			System.out.println("第二个记录"+str);
			
			randomReader.seek(0);
			System.out.println("指针位置"+randomReader.getFilePointer());
			len = randomReader.read(buf);
			str = new String(buf,0,len);
			System.out.println("第一个记录"+str);
			
			randomReader.skipBytes(length);
			System.out.println("指针位置"+randomReader.getFilePointer());
			len = randomReader.read(buf);
			str = new String(buf,0,len);
			System.out.println("第三个记录"+str);
			randomReader.close();

		} catch (Exception e) {
    
    
			// TODO: handle exception
			System.out.println(e);
		}
		
	}
}
class Student{
    
    
	private String name;
	private String number;
	public Student(String name,String number) {
    
    
		this.name = name;
		this.number = number;
	}
	public String getName() {
    
    
		return this.name;
	}
	public String getNumber() {
    
    
		return this.number;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44179485/article/details/113529007