package src.bean;java
import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile;web
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(); } } } }dom
這個例子須要注意的是UTF對中文字符的處理以及使用writeBytes方法與writeChars方法的區別. 運行後的結果爲:this
文件指針當前位置:0 使用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