package src.bean;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
class Student {
private String name;
private int age;
public Student() {
super();
}
public int getAge() {
return age;
}
public void setAge( int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class UseIO {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
// 創建一個隨機訪問文件對象,並設置為可讀寫模式
randomAccessFile = new RandomAccessFile("src\\bean\\newFile.txt",
"rw");
System.out.println("文件指針當前位置:" + randomAccessFile.getFilePointer());
// 添加內容到文件中去
// 使用writeChars方法把一串字符寫到文件中
// randomAccessFile.writeChars("I am here!If you love me,please give the kiss to me!\nThank you for your love!");
// 使用writeBytes方法把一串字符寫到文件中,使用該方法將會被舍棄字符中的高8位,所以實際上寫入的是字符中的低8位.
randomAccessFile
.writeBytes("I am here!If you love me,please give the kiss to me!\nThank you for your love!");
System.out.println("使用readLine方法讀取數據:");
System.out.println("此時文件指針當前位置:"
+ randomAccessFile.getFilePointer());
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
// 使用readLine讀取文件內容,每個字節的值被轉換為字符的低8位,而字符的高8位被賦予0.因此這個方法不支持unicode字符集.
String content = randomAccessFile.readLine();
while (content != null) {
System.out.println(content);
content = randomAccessFile.readLine();
}
// 使用read方法讀取指定長度的字節
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
byte[] b = new byte[10];
int length = randomAccessFile.read(b);
System.out.println("真正讀取的字節數:" + length);
// 使用當前平台當前的默認字符集把字節數組轉換為字符
String convertStr = new String(b);
System.out.println("轉換后的內容為:" + convertStr);
// 使用skipBytes跳過若干個字節后,讀取后面的字節
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
length = randomAccessFile.skipBytes(10); // 參數可以為負數,當為負數時,則該方法不起作用
System.out.println("實際跳過的字節數:" + length);
content = randomAccessFile.readLine();
while (content != null) {
System.out.println(content);
content = randomAccessFile.readLine();
}
// 之前使用writeBytes寫入內容,所以如果我們使用readChar讀取內容中的一個字符時,可能將出錯
// 出現亂碼的原因在於,使用該方法將從文件中讀取兩個字節做為一個字符高8位和低8位,作為一個unicode字符
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
char c = randomAccessFile.readChar();
System.out.println("讀取一個字符:" + c);
System.out.println("讀取的這個字符的值為:" + ( int) c);
// 設置文件的內容為0字節
randomAccessFile.setLength(0);
// 注意使用UTF格式寫入字符串時,對於英文字符,則占一個字節,中文字符,占三個字節,
// 而且當使用writeUTF時,在文件的開始處將寫入整個字節的長度(注意不是字符串長度),占兩個字節
randomAccessFile.writeUTF("我愛你!i love you!");
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
System.out.println(randomAccessFile.readUTF());
System.out.println("使用writeUTF方法寫入字符串時,文件字節長度為:"
+ randomAccessFile.length());
// 設置文件的內容為0字節
randomAccessFile.setLength(0);
// 創建兩個學生記錄,並寫入文件中
Student[] studs = new Student[] { new Student("andy", 23),
new Student("lili", 22) };
for (Student stud : studs) {
randomAccessFile.writeUTF(stud.getName());
randomAccessFile.writeInt(stud.getAge());
}
// 讀取剛才寫入的內容
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
// 創建學生記錄:
Student[] studCreated = new Student[2];
for ( int i = 0; i < studCreated.length; i++) {
studCreated[i] = new Student();
studCreated[i].setName(randomAccessFile.readUTF());
studCreated[i].setAge(randomAccessFile.readInt());
System.out.println("第" + i + "位學生的記錄:");
System.out.println("name:" + studCreated[i].getName());
System.out.println("age:" + studCreated[i].getAge());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
randomAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
class Student {
private String name;
private int age;
public Student() {
super();
}
public int getAge() {
return age;
}
public void setAge( int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class UseIO {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
// 創建一個隨機訪問文件對象,並設置為可讀寫模式
randomAccessFile = new RandomAccessFile("src\\bean\\newFile.txt",
"rw");
System.out.println("文件指針當前位置:" + randomAccessFile.getFilePointer());
// 添加內容到文件中去
// 使用writeChars方法把一串字符寫到文件中
// randomAccessFile.writeChars("I am here!If you love me,please give the kiss to me!\nThank you for your love!");
// 使用writeBytes方法把一串字符寫到文件中,使用該方法將會被舍棄字符中的高8位,所以實際上寫入的是字符中的低8位.
randomAccessFile
.writeBytes("I am here!If you love me,please give the kiss to me!\nThank you for your love!");
System.out.println("使用readLine方法讀取數據:");
System.out.println("此時文件指針當前位置:"
+ randomAccessFile.getFilePointer());
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
// 使用readLine讀取文件內容,每個字節的值被轉換為字符的低8位,而字符的高8位被賦予0.因此這個方法不支持unicode字符集.
String content = randomAccessFile.readLine();
while (content != null) {
System.out.println(content);
content = randomAccessFile.readLine();
}
// 使用read方法讀取指定長度的字節
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
byte[] b = new byte[10];
int length = randomAccessFile.read(b);
System.out.println("真正讀取的字節數:" + length);
// 使用當前平台當前的默認字符集把字節數組轉換為字符
String convertStr = new String(b);
System.out.println("轉換后的內容為:" + convertStr);
// 使用skipBytes跳過若干個字節后,讀取后面的字節
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
length = randomAccessFile.skipBytes(10); // 參數可以為負數,當為負數時,則該方法不起作用
System.out.println("實際跳過的字節數:" + length);
content = randomAccessFile.readLine();
while (content != null) {
System.out.println(content);
content = randomAccessFile.readLine();
}
// 之前使用writeBytes寫入內容,所以如果我們使用readChar讀取內容中的一個字符時,可能將出錯
// 出現亂碼的原因在於,使用該方法將從文件中讀取兩個字節做為一個字符高8位和低8位,作為一個unicode字符
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
char c = randomAccessFile.readChar();
System.out.println("讀取一個字符:" + c);
System.out.println("讀取的這個字符的值為:" + ( int) c);
// 設置文件的內容為0字節
randomAccessFile.setLength(0);
// 注意使用UTF格式寫入字符串時,對於英文字符,則占一個字節,中文字符,占三個字節,
// 而且當使用writeUTF時,在文件的開始處將寫入整個字節的長度(注意不是字符串長度),占兩個字節
randomAccessFile.writeUTF("我愛你!i love you!");
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
System.out.println(randomAccessFile.readUTF());
System.out.println("使用writeUTF方法寫入字符串時,文件字節長度為:"
+ randomAccessFile.length());
// 設置文件的內容為0字節
randomAccessFile.setLength(0);
// 創建兩個學生記錄,並寫入文件中
Student[] studs = new Student[] { new Student("andy", 23),
new Student("lili", 22) };
for (Student stud : studs) {
randomAccessFile.writeUTF(stud.getName());
randomAccessFile.writeInt(stud.getAge());
}
// 讀取剛才寫入的內容
// 重新把文件指針定位到開始處
randomAccessFile.seek(0);
// 創建學生記錄:
Student[] studCreated = new Student[2];
for ( int i = 0; i < studCreated.length; i++) {
studCreated[i] = new Student();
studCreated[i].setName(randomAccessFile.readUTF());
studCreated[i].setAge(randomAccessFile.readInt());
System.out.println("第" + i + "位學生的記錄:");
System.out.println("name:" + studCreated[i].getName());
System.out.println("age:" + studCreated[i].getAge());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
randomAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
這個例子需要注意的是UTF對中文字符的處理以及使用writeBytes方法與writeChars方法的區別.
運行后的結果為:
使用readLine方法讀取數據:
此時文件指針當前位置:77
I am here!If you love me,please give the kiss to me!
Thank you for your love!
真正讀取的字節數:10
轉換后的內容為:I am here!
實際跳過的字節數:10
If you love me,please give the kiss to me!
Thank you for your love!
讀取一個字符:?
讀取的這個字符的值為:18720
我愛你!i love you!
使用writeUTF方法寫入字符串時,文件字節長度為:23
第0位學生的記錄:
name:andy
age:23
第1位學生的記錄:
name:lili
age:22
此時newFile.txt中的內容為:andy lili