java文件操作總結
RandomAccessFile 相關API的使用
pointer=0; 文件指針
寫方法 raf.write(int) -->只寫一個字節 後8位,同時指針指向下一個位置,準備再次寫入
讀方法 int b = raf.read()-->讀一個字節
讀寫文件完成後一定要關閉
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‘);//只寫了一個字節 System.out.println(raf.getFilePointer()); raf.write(‘B‘); int i=0x7fffffff; //用write方法 每次只能寫一個字節,如果要把i寫進去就得寫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);//寫入字節數組 System.out.println(raf.length()); //讀文件必須把指針移到頭部 raf.seek(0); //一次性讀取。把文件中內容讀取到字節數組中 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()方法源碼,每次寫入一位
image.png
IO流(輸入流,輸出流)
字節流,字符流
1.字節流
1)InputStream、OutputStream
InputStream抽象了應用程序讀取數據的方式
OutputStream抽象了應用程序寫出數據的方式
2)EOF=End 讀到-1就讀到結尾
3)輸入流基本方法
int b=in.read(); 讀取一個字節無符號填充到int的低八位。-1是EOF
in.read(byte[] buf)讀取數據填充到字節數組buf
in.read(byte[] buf,int start,int size)讀取數據填充到字節數組buf,從buf的start位置開始,存放size長度的數據
4)輸出流基本方法
out.write(int b)寫出一個byte到流,b的低8位
out.write(byte[] buf)將buf字節數組都寫入到流
out.write(byte[] buf,int start,int size)
5)FileInputStream--->具體實現了在文件上讀取數據
6)FileOutputStream 實現了向文件中寫出byte數據的方法
7)DataOutputStream/DataInputStream
對流功能的擴展,可以更加方便的讀取Int,long,字符等類型數據
DataOutputStream
writeInt()/writeDouble()/writeUTF()
DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));
8)BufferedInputStream&BufferedOutputStream
這兩個流類為IO提供了帶緩沖區的操作,一般打開文件進行寫入或讀取操作時,都會加上緩沖,這種流模式提高了IO的性能
從應用程序中把輸入放入文件,相當於將一缸水倒入到另一個缸中:
FileOutputStream--->write方法黨羽與一滴一滴把水轉移過去
DataOutputStream--->writexxx()方法會方便一些,相當於一瓢一瓢轉移
BufferedOutputStream--->write方法更方便,相當於一瓢一瓢水先放入桶中,再從桶中導入到另一個缸中
使用in.read()方法
/* 讀取指定文件內容,按照16進制輸出到控制臺 並且每輸出10個byte換行 */ public static void printHex(String filename) throws IOException { //把文件作為字節流進行讀操作 FileInputStream in=new FileInputStream(filename); int b; int i=1; while((b=in.read())!=-1){ if(b<=0xf) { //單位數前面補0 System.out.print("0"); } System.out.print(Integer.toHexString(b)+" ");//將整形b轉換為16進制的字符串 if(i++%10==0) System.out.println(); } in.close(); }
使用in.read(byte[] buf , int start , int size)
將數據讀取到字節數組中,返回數據大小,循環輸出字節數組
public static void printHexByByteArray(String filename)throws IOException{
FileInputStream in=new FileInputStream(filename);
byte[] buf=new byte[20*1024];
//從in中批量讀取字節,放入到buf這個字節數組中,從第0個位置開始放,
//最多放buf.length個 返回的是讀到的字節的個數
/*
int bytes=in.read(buf,0, buf.length);//一次性讀完,說明字節數組足夠大
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();
}*/
//循環利用buf數組,防止開辟數組空間不夠用情況
int bytes=0;
int j=1;
while((bytes=in.read(buf,0,buf.length))!=-1){
for(int i=0;i<bytes;i++) {
//為了避免數據類型轉換錯誤,將高24位清零
System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
if(j++%10==0)
System.out.println();
}
}
}
文件的復制
//如果文件不存在直接創建,如果存在,刪除後創建。 如果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();
}
//利用帶緩沖的字節流
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();//刷新緩沖區
}
bis.close();
bos.close();
}
2.字符流
1)編碼問題
2)認識文本和文本文件
java的文本 (char)是16位無符號整數,是字符的unicode編碼(雙字節編碼)
文件是byte byte byte...的數據序列
文本文件是文本(char)序列按照某種編碼方案(utf-8,utf-16be,gbk)序列化為byte的存儲結果
3)字符流 (reader writer)---> 操作的是文本文件
字符的處理 一次處理一個字符
字符的底層仍然是基本的字節序列
字符流的基本實現
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");//默認項目的編碼
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);
// //單獨寫出換行操作
// bw.newLine();//換行操作
// bw.flush();
}
pw.close();
br.close();
// bw.close();
}
}
歡迎加入學習交流群569772982,大家一起學習交流。
java文件操作總結