1.大文件的讀寫方法
2.字符流的使用方法
總結(jié),大文件讀寫,創(chuàng)建一個(gè)數(shù)組,用一個(gè)循環(huán),每次從文件中讀取一部分放入數(shù)組
這個(gè)循環(huán)當(dāng)讀寫到文件尾部的時(shí)候,停.
最后,要記得
file.close();
import java.io.*; class Test{ public static void main(String argsp[]){ FileInputStream fis = null; FileOutputStream fos = null; try{ fis = new FileInputStream("E:/marschen/fileop/form.txt"); fos = new FileOutputStream("E:/marschen/fileop/to.txt"); byte [] buffer = new byte[1024]; while(true){ int temp = fis.read(buffer,0,buffer.length); if(-1 == temp){ break; } fos.write(buffer,0,temp); } } catch(Exception e){ System.out.println(e); } finally{ try{ fis.close(); fos.close(); } catch(Exception e){ System.out.println(e); } } } }
//字符流:讀寫文件時(shí),以字符為基礎(chǔ) //字符輸入流:Reader <----FileReader int read(char [] c,int off,int len); //字符輸出流:Writer <----FileWriter int write(char [] c,int off,int len); import java.io.*; public class TestChar{ public static void main(String args[]){ FileReader fr = null; FileWriter fw = null; try{ fr = new FileReader("E:/marschen/fileop/form.txt"); fw = new FileWriter("E:/marschen/fileop/to.txt"); char [] buffer = new char[100]; int temp = fr.read(buffer,0,buffer.length); fw.write(buffer,0,temp); /*for(int i = 0; i < buffer.length; i++) { System.out.println(buffer[i]); }*/ } catch(Exception e){ System.out.println(e); } finally{ try{ fr.close(); fw.close(); } catch(Exception e){ System.out.println(e); } } } }