字符流和字節流
字符流
1.1Reader/Write(字符流的輸入和輸出流,相對於java文件而言)
1.2能夠用TXT編輯器打開的文件,且不亂碼就是字符文件可以用字符流來操作
而不能打開的文件,則是字節文件。
2.輸入流Reader類(抽象類)
2.1常用方法
int read(); //讀取單個字符
int read(byte [] c) //返回實際讀取的字符數
int read(char[] c,int off,int len)
void colse() //關閉流
2.2FileReader類(是Reader的子類)
2.2.1 讀取方法:File f=new File("文件路徑");
Reader fr=new FileReader("文件路徑"/f);
Reader fr=null; StringBuffer sbf=null; try { fr=new FileReader("D:\\Test\\test.txt"); sbf=new StringBuffer(); int length; while((length=fr.read())!=-1){ char c=(char)length; sbf.append(c); } System.示例out.println(sbf.toString()); //輸出數據 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fr!=null){try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.3字符輸入流(BufferedReader類)
2.3.1:優勢:提高了讀取文件的效率。
2.3.2:創建對象:
Reader fr=new FileReader("文件路徑(相對或絕對路徑)");
BufferedReader br=new BufferedReader(fr);或BufferedReader br=new BufferedReader(new FileReader(“文件路徑(相對或絕對路徑)”));
2.3.3: br.readerLine(); 讀取一行數據
Reader fr=null; BufferedReader br=null; try { //先創建一個FileReader對象 fr=new FileReader("D:\\Test\\test.txt"); //在創建一個BufferedReader對象 br=new BufferedReader(fr); //讀取每一行數據 String line=br.readLine(); while(line!=null){ System.out.println(line); line=br.readLine();//再次賦值進行判斷 } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); //先關 fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }示例
3.字符輸出流Writer類
3.1常用方法:
write(String s); 將str字符串裏包含的字符輸出到指定的輸出流中
write(String s,int off,int len)
void close() 關閉輸出流
void flush() 刷新輸出流
3.2FilerWrite(是write的子類)
3.3讀取方法:
Writer fr=new FileWriter("文件路徑");
Writer fw=null; try { //創建一個FileWrite對象 fw=new FileWriter("D:\\Test\\簡介.txt"); //寫入信息 fw.write("我熱愛我的團隊!"+"\r\n"); fw.write("張三"+"\r\n"); //\r\n進行換行 fw.write("18歲"); fw.flush(); //刷新緩沖區 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }示例
3.4:字符輸出流BufferedWrite類
3.4.1:創建對象:
BufferdWriter bw=new BufferedWriter(new FileWrite(“文件路徑”))
Writer fw=null; BufferedWriter bw=null; try { fw=new FileWriter("D:\\Test\\hello.txt"); bw=new BufferedWriter (fw); //寫入信息 bw.write("大家好"); bw.write("我正在學習BufferedWriter。"); bw.newLine(); //換行 bw.write("請多多指教!"); bw.flush(); //刷新緩沖區 bw.close(); //關閉流 System.out.println("成功插入數據!"); //讀取文件內容 FileReader fr=new FileReader("D:\\Test\\hello.txt"); StringBuffer sbf=new StringBuffer(); int length; while((length=fr.read())!=-1){ char c=(char)length; sbf.append(c); } System.out.println(sbf.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }示例
字節流
2.1:字節輸入流(InputStream):
2.1.1: 常用方法(數據從文件到java代碼中)
int read(); //讀取一個字節
int read(byte[]); //讀取一串字節
long avaliable; //文件長度
2.1.2: FileInptstream(字節文件輸入流,是InputStream的子類)
new FileInptstream(File)
new FileInptstream("文件路徑+文件名")
//聲明流對象 FileInputStream fis=null; try { fis =new FileInputStream("D:\\Test\\hello.txt"); int data; System.out.println("文件內容為:"); //循環讀取數據 while((data=fis.read())!=-1){ System.out.print((char)data+""); } }catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(fis!=null){ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }示例
2.2:字節輸出流(OutPutStream):
2.2.1: 常用方法:(數據從java代碼中,寫到文件或其他介質中)
void write (字節);//寫入一個字節
void write(byte[]); //寫入字節數組
2.2.2: FileOutputStream(字節文件輸出流,是OutputStream的子類)
new FileOutputStream(file)
new FileOutputStream("文件路徑+文件名")
new FileOutputStream("文件路徑+文件名",boolean)
註意:a.boolean:表示是否向文件末尾追加,如果是true,表示追加,false表示不追加(也就是覆蓋),默認值為false
b.創建FileOutputStream實咧時,如果相應的文件並不存在,則會自動創建一個空的文件。
// File file=new File("D:\\Test"); // showInfo(file); //非菠蘿契數列:1 1 2 3 5 8 ...... //int [] muns=new int[10]; //定義數組 Scanner input=new Scanner(System.in); System.out.println("請輸入您要計算的位置:"); int a=input.nextInt(); System.out.println(suan(a-1)); } private static void showInfo(File f){ File [] file=f.listFiles(); for(int i=0;i<file.length;i++){ System.out.println(file[i].getName()); //得到一級目錄中的文件和文件夾 if(file[i].isDirectory()){ //判斷一級目錄中是否含有文件夾 showInfo(file[i]);//調用自己方法 } } } private static int suan(int i){ if(i==0 || i==1){ return i=1; }else{ return suan(i-2)+suan(i-1); }遞歸算法
字符流和字節流