IO學(xué)習(xí)

1.輸入輸出流

io流.PNG

1.代碼:

package Test;

import org.junit.Test;

import java.io.*;

public class JavaIO {
    /*
     * 字節(jié)型輸入流
     * */
    @Test
  public void fileinputstream(){
      File file=new File("D:/test/aaa.txt");
      System.out.println(file.isFile());
      FileInputStream fis=null;
      try {
          fis=new FileInputStream(file);
          byte[] bit= new byte[5];
          int code=fis.read(bit);
          while (code!=-1){
              String value=new String(bit,0,code);
              System.out.print(value);
              code=fis.read(bit);
          }
      } catch (IOException e) {
          e.printStackTrace();
      }finally {
          try {
              if(fis!=null) {//這個判斷是問了防止file找不到文件報錯導(dǎo)致FileinputStream沒有創(chuàng)建然后空指針
                  fis.close();//關(guān)閉通道
              }
          }catch (IOException e){
              e.printStackTrace();
          }
      }
  }
    /**
     * 字符型輸出流
     */
  @Test
 public void fileoutputstream(){
      File file=new File("D:/test/bbb.txt");
      FileOutputStream outputStream=null;
      try {
          outputStream=new FileOutputStream(file,true);
//          byte[] bytes=new byte[5];
//          bytes[0]=97;
          String str="a+b+c";
          byte[] bytes=str.getBytes();
          outputStream.write(bytes);
          outputStream.flush();
      } catch (IOException e) {
          e.printStackTrace();
      }finally {
          try {
              outputStream.close();
          }catch (IOException e){
              e.printStackTrace();
          }
      }
  }
  /*
  * 文件的復(fù)制,bug沒有判斷是否是文件還是文件夾
  * */
  public void fileCopy(String fileName,String copyPath){
       File file=new File(fileName);
      FileInputStream fis=null;
      FileOutputStream fot=null;
      try {
          fis=new FileInputStream(file);
          byte[] bit= new byte[1024];
          int code=fis.read(bit);
          while (code!=-1){
              fot=new FileOutputStream(copyPath,true);
              //String value=new String(bit,0,code);
              fot.write(bit,0,code);
              code=fis.read(bit);
          }
      } catch (IOException e) {
          e.printStackTrace();
      }finally {
          try {
              if(fis!=null) {//這個判斷是問了防止file找不到文件報錯導(dǎo)致FileinputStream沒有創(chuàng)建然后空指針
                  fis.close();//關(guān)閉通道
                  fot.close();
              }
          }catch (IOException e){
              e.printStackTrace();
          }
      }
  }
  @Test
  public void copyTest(){
     // this.fileCopy("D:/test/bbb.txt","D:/test/ccc.txt");
      File file =new File("D:\\test");
      JavaIO javaIO=new JavaIO();
      javaIO.superCopy(file,"D:\\test");
      //this.superCopy(file,"D:/test1");
  }
  public void superCopy(File file,String newPath){
     File[] files=file.listFiles();
     String oldPath=file.getAbsolutePath();
     String newFilePath=newPath+oldPath.split(":")[1];
     File newFile=new File(newFilePath);
     if(files!=null){
         //file是文件夾
          newFile.mkdir();
         System.out.println(newFilePath+"文件夾創(chuàng)建");
         if(files!=null&&files.length!=0){
             for (File f:files) {
                 this.superCopy(f,newPath);
             }
         }
     }else {
         //file是文件
         FileInputStream fis=null;
         FileOutputStream fot=null;
         try {
             fis=new FileInputStream(file);
             fot=new FileOutputStream(newFile);
             byte[] bit= new byte[1024];
             int code=fis.read(bit);
             while (code!=-1){
                 //String value=new String(bit,0,code);
                 fot.write(bit,0,code);
                 fot.flush();
                 code=fis.read(bit);
             }
         } catch (IOException e) {
             e.printStackTrace();
         }finally {
             try {
                 if(fis!=null) {//這個判斷是問了防止file找不到文件報錯導(dǎo)致FileinputStream沒有創(chuàng)建然后空指針
                     fis.close();//關(guān)閉通道
                     fot.close();
                 }
             }catch (IOException e){
                 e.printStackTrace();
             }
         }
     }
  }
  /*
  * 對象流,使用前需要將對象序列化
  * */
  @Test
  public void objeceInputStream()throws Exception{
    //將對象直接記錄在文件中
//      Person p=new Person("長啥","10010",12);
//      FileOutputStream fos=new FileOutputStream("D:/test/ddd.txt");
//      ObjectOutputStream obs=new ObjectOutputStream(fos);
//      obs.writeObject(p);
//      obs.flush();
//      obs.close();
      FileInputStream ins=new FileInputStream("D:/test/ddd.txt");
      ObjectInputStream ons=new ObjectInputStream(ins);
      Person person=(Person) ons.readObject();
      System.out.println(person.getName());
      person.eat();
  }
}

2.總結(jié)


io總結(jié).PNG
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末山析,一起剝皮案震驚了整個濱河市堰燎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌笋轨,老刑警劉巖秆剪,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異爵政,居然都是意外死亡仅讽,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門茂卦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來何什,“玉大人组哩,你說我怎么就攤上這事等龙。” “怎么了伶贰?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵蛛砰,是天一觀的道長。 經(jīng)常有香客問我黍衙,道長泥畅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任琅翻,我火速辦了婚禮位仁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘方椎。我一直安慰自己聂抢,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布棠众。 她就那樣靜靜地躺著琳疏,像睡著了一般有决。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上空盼,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天书幕,我揣著相機(jī)與錄音,去河邊找鬼揽趾。 笑死台汇,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的篱瞎。 我是一名探鬼主播励七,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼奔缠!你這毒婦竟也來了掠抬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤校哎,失蹤者是張志新(化名)和其女友劉穎两波,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體闷哆,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡腰奋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了抱怔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片劣坊。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖屈留,靈堂內(nèi)的尸體忽然破棺而出局冰,到底是詐尸還是另有隱情,我是刑警寧澤灌危,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布康二,位于F島的核電站,受9級特大地震影響勇蝙,放射性物質(zhì)發(fā)生泄漏沫勿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一味混、第九天 我趴在偏房一處隱蔽的房頂上張望产雹。 院中可真熱鬧,春花似錦翁锡、人聲如沸蔓挖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽时甚。三九已至隘弊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間荒适,已是汗流浹背梨熙。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留刀诬,地道東北人咽扇。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像陕壹,于是被迫代替她去往敵國和親质欲。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容