Java(11)ファイル操作

Java(11)ファイル操作

1.基本的な考え方

1.1。データの永続性

  1. クラスの属性のみを保存します
  2. ストレージオブジェクト-オブジェクトのシリアル化

1.2。ファイルのデータ構造

1.フレーム

2.ファイル編成

  • 記録-記録
    • 域-field
      • バイト-バイト
      • ビット

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.例

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