Java(11) file operations
1. Basic concepts
1.1. Data persistence
- Store only the attributes of the class
- Storage object-object serialization
1.2. The data structure of the file
1.Frame
2. File organization
- Record-record
- Area-field
- Byte-byte
- Bit
- Area-field
1.3. Buffer
Temporary storage location
1.4. Stream
1. Concept
Data sequence
2. Different levels of flow
3. Read and write
- Sequential file
- random document
4. Attention
Exception handling must be set for all streams
1.5. Common flow
1. Input stream
- Byte input stream
InputStream
- Character input stream
Reader
2. Output stream
- Byte output stream
OutputStream
- Character output stream
Writer
3. File operations
- File class
File
- Random access file
RandomAccessFile
4. Summary
2. Examples
2.1. Byte read and write
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. Character reading and writing
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. Random read and write
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;
}
}