RandomAccessFile 相關(guān)API的使用
pointer=0; 文件指針
寫方法 raf.write(int) -->只寫一個字節(jié) 后8位候衍,同時指針指向下一個位置,準(zhǔn)備再次寫入
讀方法 int b = raf.read()-->讀一個字節(jié)
讀寫文件完成后一定要關(guān)閉
public static void testrandomaccessfile() throws IOException {
File demo=new File("demo");
if(!demo.exists())
demo.mkdir();
File file=new File(demo,"raf.dat");
if(!file.exists())
file.createNewFile();
RandomAccessFile raf=new RandomAccessFile(file,"rw");
//指針位置
System.out.println(raf.getFilePointer());
raf.write('A');//只寫了一個字節(jié)
System.out.println(raf.getFilePointer());
raf.write('B');
int i=0x7fffffff;
//用write方法 每次只能寫一個字節(jié)蛇捌,如果要把i寫進(jìn)去就得寫4次
raf.write(i>>>24);//高8位
raf.write(i>>>16);
raf.write(i>>>8);
raf.write(i);
System.out.println(raf.getFilePointer());
//可以直接寫一個int
raf.writeInt(i);
String s ="中";
byte[] gbk=s.getBytes("gbk");
raf.write(gbk);//寫入字節(jié)數(shù)組
System.out.println(raf.length());
//讀文件必須把指針移到頭部
raf.seek(0);
//一次性讀取毕贼。把文件中內(nèi)容讀取到字節(jié)數(shù)組中
byte[] buf=new byte[(int)raf.length()];
raf.read(buf);
System.out.println(Arrays.toString(buf));
for(byte b:buf){
System.out.println(Integer.toHexString(b&0xff)+" ");
}
raf.close();
}
writeInt()方法源碼,每次寫入一位
IO流(輸入流拄衰,輸出流)
字節(jié)流卫漫,字符流
1.字節(jié)流
1)InputStream、OutputStream
InputStream抽象了應(yīng)用程序讀取數(shù)據(jù)的方式
OutputStream抽象了應(yīng)用程序?qū)懗鰯?shù)據(jù)的方式
2)EOF=End 讀到-1就讀到結(jié)尾
3)輸入流基本方法
int b=in.read(); 讀取一個字節(jié)無符號填充到int的低八位肾砂。-1是EOF
in.read(byte[] buf)讀取數(shù)據(jù)填充到字節(jié)數(shù)組buf
in.read(byte[] buf,int start,int size)讀取數(shù)據(jù)填充到字節(jié)數(shù)組buf列赎,從buf的start位置開始,存放size長度的數(shù)據(jù)
4)輸出流基本方法
out.write(int b)寫出一個byte到流镐确,b的低8位
out.write(byte[] buf)將buf字節(jié)數(shù)組都寫入到流
out.write(byte[] buf,int start,int size)
5)FileInputStream--->具體實(shí)現(xiàn)了在文件上讀取數(shù)據(jù)
6)FileOutputStream 實(shí)現(xiàn)了向文件中寫出byte數(shù)據(jù)的方法
7)DataOutputStream/DataInputStream
對流功能的擴(kuò)展包吝,可以更加方便的讀取Int,long,字符等類型數(shù)據(jù)
DataOutputStream
writeInt()/writeDouble()/writeUTF()
DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));
8)BufferedInputStream&BufferedOutputStream
這兩個流類為IO提供了帶緩沖區(qū)的操作,一般打開文件進(jìn)行寫入或讀取操作時源葫,都會加上緩沖诗越,這種流模式提高了IO的性能
從應(yīng)用程序中把輸入放入文件,相當(dāng)于將一缸水倒入到另一個缸中:
FileOutputStream--->write方法黨羽與一滴一滴把水轉(zhuǎn)移過去
DataOutputStream--->writexxx()方法會方便一些息堂,相當(dāng)于一瓢一瓢轉(zhuǎn)移
BufferedOutputStream--->write方法更方便嚷狞,相當(dāng)于一瓢一瓢水先放入桶中,再從桶中導(dǎo)入到另一個缸中
使用in.read()方法
/*
讀取指定文件內(nèi)容荣堰,按照16進(jìn)制輸出到控制臺
并且每輸出10個byte換行
*/
public static void printHex(String filename) throws IOException {
//把文件作為字節(jié)流進(jìn)行讀操作
FileInputStream in=new FileInputStream(filename);
int b;
int i=1;
while((b=in.read())!=-1){
if(b<=0xf) {
//單位數(shù)前面補(bǔ)0
System.out.print("0");
}
System.out.print(Integer.toHexString(b)+" ");//將整形b轉(zhuǎn)換為16進(jìn)制的字符串
if(i++%10==0)
System.out.println();
}
in.close();
}
使用in.read(byte[] buf , int start , int size)
將數(shù)據(jù)讀取到字節(jié)數(shù)組中床未,返回?cái)?shù)據(jù)大小,循環(huán)輸出字節(jié)數(shù)組
public static void printHexByByteArray(String filename)throws IOException{
FileInputStream in=new FileInputStream(filename);
byte[] buf=new byte[20*1024];
//從in中批量讀取字節(jié)振坚,放入到buf這個字節(jié)數(shù)組中薇搁,從第0個位置開始放,
//最多放buf.length個 返回的是讀到的字節(jié)的個數(shù)
/*
int bytes=in.read(buf,0, buf.length);//一次性讀完渡八,說明字節(jié)數(shù)組足夠大
int j=1;
for(int i=0;i<bytes;i++){
if((buf[i]&0xff)>0&&(buf[i]&0xff)<=0xf){
System.out.print("0");
}
System.out.print(Integer.toHexString(buf[i])+" ");
if(j++%10==0)
System.out.println();
}*/
//循環(huán)利用buf數(shù)組啃洋,防止開辟數(shù)組空間不夠用情況
int bytes=0;
int j=1;
while((bytes=in.read(buf,0,buf.length))!=-1){
for(int i=0;i<bytes;i++) {
//為了避免數(shù)據(jù)類型轉(zhuǎn)換錯誤传货,將高24位清零
System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
if(j++%10==0)
System.out.println();
}
}
}
文件的復(fù)制
//如果文件不存在直接創(chuàng)建,如果存在宏娄,刪除后創(chuàng)建问裕。 如果append=true,不存在的時候則追加
FileOutputStream out=new FileOutputStream("demo/out.dat");
public static void copyFile(File srcFile,File destFile)throws IOException{
if(!srcFile.exists())
throw new IllegalArgumentException("文件"+srcFile+"不存在");
if(!srcFile.isFile())
throw new IllegalArgumentException(srcFile+"不是文件");
FileInputStream in=new FileInputStream(srcFile);
FileOutputStream out=new FileOutputStream(destFile);
byte[] buf=new byte[8*1024];
int b;
while((b=in.read(buf,0,buf.length))!=-1){
out.write(buf,0,b);
out.flush();//最好加上
}
in.close();
out.close();
}
//利用帶緩沖的字節(jié)流
public static void copytFileByBuffer(File srcFile,File destFile)throws IOException{
if(!srcFile.exists())
throw new IllegalArgumentException("文件"+srcFile+"不存在");
if(!srcFile.isFile())
throw new IllegalArgumentException(srcFile+"不是文件");
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));
int c;
while((c=bis.read())!=-1){
bos.write(c);
bos.flush();//刷新緩沖區(qū)
}
bis.close();
bos.close();
}
2.字符流
1)編碼問題
2)認(rèn)識文本和文本文件
java的文本 (char)是16位無符號整數(shù)孵坚,是字符的unicode編碼(雙字節(jié)編碼)
文件是byte byte byte...的數(shù)據(jù)序列
文本文件是文本(char)序列按照某種編碼方案(utf-8,utf-16be,gbk)序列化為byte的存儲結(jié)果
3)字符流 (reader writer)---> 操作的是文本文件
字符的處理 一次處理一個字符
字符的底層仍然是基本的字節(jié)序列
字符流的基本實(shí)現(xiàn)
InputStreamReader 完成byte流解析為char流粮宛,按照編碼解析
OutputStreamWriter 提供char流到byte流,按照編碼處理
//注意編碼問題
public class IsrAndOswDemo {
public static void main(String[] args) throws IOException {
FileInputStream in=new FileInputStream("G:\\11.txt");
InputStreamReader isr=new InputStreamReader(in,"utf-8");//默認(rèn)項(xiàng)目的編碼
FileOutputStream out=new FileOutputStream("G:\\12.txt");
OutputStreamWriter osw=new OutputStreamWriter(out,"utf-8");
// int c;
// while((c=isr.read())!=-1){
// System.out.print((char)c);
// }
char[] buffer=new char[8*1024];
int c;
while((c=isr.read(buffer,0,buffer.length))!=-1){
String s=new String(buffer,0,c);
System.out.println(s);
osw.write(buffer,0,c);
osw.flush();
}
isr.close();
osw.close();
}
}
FileReader/FileWriter
public class FrAndFwDemo {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("g:\\11.txt");
FileWriter fw=new FileWriter("g:\\13.txt");
char[] buffer=new char[2056];
int c;
while((c=fr.read(buffer,0,buffer.length))!=-1){
fw.write(buffer,0,c);
fw.flush();
}
fr.close();
fw.close();
}
}
字符流的過濾器
BufferedReader ---> readLine 一次讀一行
BufferedWriter/PrintWriter --->寫一行
public class BrAndBwOrPwDemo {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("g:\\11.txt")));
// BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("g:\\123.txt")));
PrintWriter pw=new PrintWriter("g:\\1234.txt");
String line;
while((line=br.readLine())!=null){
System.out.println(line);
pw.println(line);
pw.flush();
// bw.write(line);
// //單獨(dú)寫出換行操作
// bw.newLine();//換行操作
// bw.flush();
}
pw.close();
br.close();
// bw.close();
}
}