1. 程式人生 > >Java_RandomAccessFile和IO流

Java_RandomAccessFile和IO流

 

Java RandomAccessFile類

Test2.java 檔案程式碼:

//讀取檔案內容
public class Test1RandomAccessFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//表示對該檔案的訪問許可權是隻讀模式
RandomAccessFile raf=new RandomAccessFile("b.txt","r");
//讀取一個位元組
int d=raf.read();
System.out.println((char)d);//97-->a
int d1=raf.read();
System.out.println((char)d1);//98-->b
int d2=raf.read();
System.out.println(d2);//-1表示讀到檔案末尾
raf.close();
}


}


RandomAccessFile寫檔案

Test3.java 檔案程式碼:

//RandomAccessFile只有寫許可權才可以建立檔案,讀許可權是不行的
public class Test2RandomAccessFile {
public static void main(String[] args) throws IOException {
// //FileNotFoundException 系統找不到指定的檔案
// RandomAccessFile raf=new RandomAccessFile("c.txt", "r");
RandomAccessFile raf=new RandomAccessFile("c.dat","rw");//dat表示音訊視訊格式
byte[] data="HelloWorld".getBytes();//String.getBytes()將字串變成位元組陣列
raf.write(data);//寫位元組陣列
raf.close();
}


}


RandomAccessFile讀檔案

 

下面是一個例子:

Test4.java 檔案程式碼:

public class Test3RandomAccessFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
RandomAccessFile raf=new RandomAccessFile("raf.txt","rw");
byte[] buf=new byte[10];//buf={0,0,0,0,0,0,0,0,0,0}  HelloWorld
int len=raf.read(buf);//將讀取到的位元組陣列放入buf中   buf={H,e,l,l,o,W,o,r,l,d}
System.out.println("實際讀取到的位元組量:"+len);
System.out.println(new String(buf));//將buf陣列變成String
raf.close();
}


}


 

RandomAccessFile是基於指標的

//RandomAccessFile的讀寫操作是基於指標的
public class Test4RandomAccessFile {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.txt", "rw");
//指標預設從0開始(檔案的第一個字元的前面)
System.out.println(raf.getFilePointer());//表示返回當前的指標位置 0
raf.read();//讀一個位元組指標移動一位
System.out.println(raf.getFilePointer());//1
//讀取world,需要跳過ello這四個位元組
raf.skipBytes(4);//跳過的位元組量
System.out.println(raf.getFilePointer());//5
//讀出World
byte[] buf=new byte[5];
int len=raf.read(buf);//buf={W,o,r,l,d}
System.out.println(new String(buf));//World

int d=raf.read();
System.out.println("-1表示讀到檔案末尾d="+d);
//將游標(遊標)移動到檔案初始位置(0)
raf.seek(0);
System.out.println(raf.getFilePointer());
raf.close();
}


}

 

RandomAccessFile檔案複製

 

//RandomAccessFile檔案的拷貝
public class Test5RandomAccessFile {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.txt", "r");//拷貝的原始檔只需要r模式
RandomAccessFile rafCopy=new RandomAccessFile("rafCopy.txt", "rw");//目標檔案
int data=0;//用來儲存實際讀取到的位元組 int值
while((data=raf.read())!=-1){//不等於-1表示沒有讀到檔案末尾,繼續讀
System.out.println("data="+data);
rafCopy.write(data);//將讀取到的位元組寫入rafCopy.txt檔案中
}
System.out.println("複製完成");
raf.close();
rafCopy.close();
}


}


IO流-FileOutputStream

/*
 * 都是以位元組為單位進行讀寫操作的
 * OutputStream 位元組輸出流    是所有位元組輸出流的父類  只能寫內容
 * InputStream  位元組輸入流  是所有位元組輸入流的父類    只能讀內容
 */
public class Test7OutputStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
OutputStream os=new FileOutputStream("os.txt");//向上造型
os.write("helloworld".getBytes());//將字串變成位元組陣列
os.close();//關閉流釋放資源
}


}


FileInputStream的使用

//位元組輸入流InputStream
public class Test8InputStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is=new FileInputStream(new File("os.txt"));
//讀檔案
int d=0;
//is.read()讀取一個位元組
while((d=is.read())!=-1){//-1表示讀取到檔案末尾
System.out.print((char)d);
}
is.close();
}


}


IO流的檔案複製

//輸入輸出流的檔案複製
public class Test9FileCopy {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("osCopy.txt");//目標檔案
FileInputStream fis=new FileInputStream("os.txt");//原始檔
int d=0;
while((d=fis.read())!=-1){//讀取原始檔  以位元組為單位讀取
fos.write(d);//將讀取到的位元組寫到目標檔案中
}
System.out.println("複製完成");
fos.close();
fis.close();
}


}

 

IO流通過位元組陣列賦值

//建立位元組陣列複製檔案
public class Test15FileCopy {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("e:"+File.separator+"360.exe");//原始檔案
FileOutputStream fos=new FileOutputStream("d:"+File.separator+"360.exe");//目標檔案
byte[] data=new byte[1024*8];//1G=1024M  1M=1024K 1K=1024B
int len=0;//表示實際讀取到的位元組量
//len=10   data={我,的,滑,板,鞋,0,0,0,0,0,0,0,0,0,...}
while((len=fis.read(data))!=-1){//data={鞋,0,0}
fos.write(data,0,len);//擷取實際長度,去空格
}
fis.close();
fos.close();
System.out.println("複製完成");
}


}