免费爱碰视频在线观看,九九精品国产屋,欧美亚洲尤物久久精品,1024在线观看视频亚洲

      Java中的File類和IO流

      Flie類

      1.什么是File類

      File類就是當(dāng)前系統(tǒng)中,文件或文件夾的抽象表示。

      通過(guò)使用File類的實(shí)例對(duì)象,我們就可以通過(guò)代碼實(shí)現(xiàn)計(jì)算機(jī)的文件控制,即文件的增刪改查等操作

      2.File類的使用

      首先為了保證其他文件的安全性,建議先建立一個(gè)Test文件夾專門用于File對(duì)象的聯(lián)系。

      public static void main(String[] args) throws IOException {

      File f1 = new File(“D:/AAAkejian/作業(yè)/test”);

      File f2 = new File(“D:AAAkejian作業(yè)test1”);

      f1.mkdir();

      f2.mkdir();

      f1 = new File(“D:/AAAkejian/作業(yè)/test/test.txt”);

      f1.createNewFile();

      File f3 = new File(“D:”+File.separator+

      “AAAkejian”+File.separator+

      “作業(yè)”+File.separator+”test1”);

      }

      其中:

      D:/AAAkejian/作業(yè)/test,代表路徑名,“ / ”和“”都表示路徑分隔符;

      在windows系統(tǒng)下,兩種分隔符都支持,在Linux和Mac系統(tǒng)下只支持“ / ”

      使用File.separator,表示動(dòng)態(tài)分隔符。但在日常開發(fā)中,由于windows系統(tǒng)中兩者都支持,而且在使用“ ”時(shí),還需要考慮轉(zhuǎn)義字符,因此我們常用的路徑分隔符為“ / ”;

      3.File對(duì)象的使用

      1)創(chuàng)建目錄或文件

      File f1 = new File(“D:/AAAkejian/作業(yè)/test”);

      //創(chuàng)建目錄

      f1.mkdir();

      f1 = new File(“D:/AAAkejian/作業(yè)/test/test.txt”);

      //創(chuàng)建文件

      f1.createNewFile();

      f1 = new File(“D:/AAAkejian/作業(yè)/test/test2”);

      //創(chuàng)建多級(jí)目錄

      f1.mkdirs();

      2)刪除目錄或文件

      //刪除目錄或文件

      f1.delete();

      //當(dāng)程序結(jié)束時(shí)再進(jìn)行刪除

      f2.deleteOnExit();

      3)修改目錄或文件

      IO流

      1.什么是IO流

      IO分別是兩個(gè)單詞的首字母縮寫,I:Inputstream輸入流 O:Outputstream輸出流

      2.IO流的作用:對(duì)文件中的內(nèi)容進(jìn)行操作;

      輸入:讀操作(讀取文件的內(nèi)容) 輸出:寫操作(向文件中寫內(nèi)容)

      這里的輸入輸出是針對(duì)java程序而言,從文件中讀取內(nèi)容到Java程序即為輸入;

      同理,從Java程序向文件中寫入內(nèi)容就叫輸出。

      3.IO流的方向

      1)根據(jù)流的方向

      輸入流:程序可以從中讀取數(shù)據(jù)的流

      輸出流:程序可以從中寫入數(shù)據(jù)的流

      2)根據(jù)流的單位

      字節(jié)流:以字節(jié)為單位傳輸數(shù)據(jù)的流

      字符流:以字符為單位傳輸數(shù)據(jù)的流

      3)根據(jù)功能

      節(jié)點(diǎn)流:直接和文件進(jìn)行交互

      處理流:不直接作用在文件上

      IO流中有(字節(jié)輸入/輸出流、字符輸入/輸出流)四個(gè)基本流,其他的流都是再這四個(gè)流的基礎(chǔ)上進(jìn)行拓展的

      4.Writer字符輸出流

      1)Writer類是所有字符輸出流的跟類

      2)使用Writer向文件中添加內(nèi)容

      public static void main(String[] args) throws IOException {

      Writer w1 = new FileWriter(“D:/AAAkejian/WriterTest/test2.txt”);

      String str = “這是第二遍練習(xí)”;

      w1.write(str);

      //該方式則可以進(jìn)行添加而不覆蓋

      Writer w2 = new FileWriter(“D:/AAAkejian/WriterTest/test2.txt”,true);

      str = “這是添加的內(nèi)容”;

      w2.write(str);

      //append也可用于添加

      w1.append(str);

      //刷新流

      w1.flush();

      //關(guān)閉流

      w1.close();

      w2.flush();

      w2.close();

      }

      3)使用Writer直接向文件內(nèi)添加內(nèi)容時(shí),后添加的會(huì)覆蓋先添加的內(nèi)容,那么在進(jìn)行內(nèi)容追加時(shí)就需要添加上一個(gè)true,表示允許追加內(nèi)容到文件中。見(jiàn)上圖

      5.Reader字符輸入流

      1)Reader類是所有字符輸入流的跟類

      2)使用FileReader實(shí)現(xiàn)類進(jìn)行文件的讀取操作:

      public static void main(String[] args) throws IOException {

      Reader reader = new FileReader(“D:/AAAkejian/ReaderTest/test1.txt”);

      int count = 0;

      char[] cList = new char[10];

      while( (count=reader.read(cList)) !=-1 ){

      String str=new String(cList,0,count);

      System.out.print(str);

      }

      }

      6,結(jié)合輸入輸出流可以實(shí)現(xiàn)文件的復(fù)制功能

      public void test1() throws IOException {

      //創(chuàng)建字符輸入流

      FileReader fr = new FileReader(“D:/AAAkejian/Test/test1.txt”);

      //創(chuàng)建字符輸出流

      FileWriter fw = new FileWriter(“D:/AAAkejian/Test/test2.txt”);

      //記錄讀取到的個(gè)數(shù)

      int count = 0;

      //每次讀取的內(nèi)容放入這個(gè)數(shù)組中

      char[] cList = new char[10];

      while ((count = fr.read(cList))!=-1){

      fw.write(cList,0,count);

      fw.flush();

      }

      fw.close();

      fr.close();

      }

      這里需要注意的是,字符流只適用于文本的輸入輸出,對(duì)于圖片,視頻等二進(jìn)制文件則無(wú)法使用字符流進(jìn)行操作。

      7.字節(jié)流

      1)字節(jié)輸出流——OutputStream(所有字節(jié)輸出流的父類)

      字節(jié)輸出流的使用,以FileOutputStream為例

      public void test1() throws Exception {

      //定義字節(jié)輸出流對(duì)象

      OutputStream osp = new FileOutputStream(“D:/AAAkejian/Test/test1.txt”);

      String str = “Add abcd”;

      //將字符串轉(zhuǎn)化為字節(jié)存入字節(jié)數(shù)組中

      byte[] bList = str.getBytes();

      //寫入文件

      osp.write(bList);

      //刷新流

      osp.flush();

      //關(guān)閉流

      osp.close();

      }

      2)字節(jié)輸入流——InputStream(所有字節(jié)輸入流的父類)

      字節(jié)輸入流的使用,以FileInputStream為例,這里的讀取規(guī)則,與字符輸入流類似,利用循環(huán)進(jìn)行循環(huán)讀取文件內(nèi)容。

      public void test() throws Exception{

      //創(chuàng)建字節(jié)輸入流對(duì)象

      InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test1.txt”);

      byte[] bList = new byte[3000];

      int count = 0;

      while( (count=ips.read(bList))!=-1 ){

      //把byte數(shù)組轉(zhuǎn)換為字符串

      String str=new String(bList,0,count);

      System.out.println(str);

      }

      ips.close();

      }

      3)利用字節(jié)輸入輸出流完成圖片的復(fù)制功能

      public void test()throws Exception{

      //定義字節(jié)輸入流對(duì)象

      InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test3.png”);

      //定義字節(jié)輸出流對(duì)象

      OutputStream ops = new FileOutputStream(“D:/AAAkejian/Test/test1.png”);

      //定義字節(jié)數(shù)組,用于存儲(chǔ)所讀取的內(nèi)容

      byte[] bList = new byte[100];

      int count =0;

      while ((count = ips.read(bList))!=-1){

      ops.write(bList,0,count);

      ops.flush();

      }

      ops.close();

      ips.close();

      }

      8.緩存流

      緩存流是在基礎(chǔ)流(InputStream OutputStream Reader Writer)之上,添加了一個(gè)緩沖池的功能,用于提高IO效率,降低IO次數(shù)

      緩沖流的使用:

      public void test()throws Exception{

      //定義字節(jié)輸出流

      OutputStream ops = new FileOutputStream(“D:/AAAkejian/Test/test1.txt”);

      //定義緩存流對(duì)象

      BufferedOutputStream bfops = new BufferedOutputStream(ops);

      String str = “new content”;

      byte[] bList = str.getBytes();

      //此時(shí)的內(nèi)容在緩沖池中,并未寫入文件

      bfops.write(bList);

      //刷新緩沖池,將緩沖池中的內(nèi)容寫入文件中

      //bfops.flush();

      //h緩存流中的close方法,會(huì)先執(zhí)行flush方法

      bfops.close();

      }

      9.對(duì)象流

      對(duì)象流的意義在于數(shù)據(jù)的持久化,例如游戲存到,就是一種對(duì)象流的使用。

      在日常程序運(yùn)行中,內(nèi)容都是存儲(chǔ)在內(nèi)存中的,而將內(nèi)存中的數(shù)據(jù),存儲(chǔ)到磁盤中,就實(shí)現(xiàn)了數(shù)據(jù)的持久化。

      1)對(duì)象流輸出的使用——ObjectOutputStream–序列化(存檔):

      public class Role implements Serializable {

      private String name;

      private int level;

      private String power;

      public Role(String name, int level, String power) {

      this.name = name;

      this.level = level;

      this.power = power;

      }

      public String getName() {

      return name;

      }

      public void setName(String name) {

      this.name = name;

      }

      public int getLevel() {

      return level;

      }

      public void setLevel(int level) {

      this.level = level;

      }

      public String getPower() {

      return power;

      }

      public void setPower(String power) {

      this.power = power;

      }

      }

      public void test()throws Exception{

      OutputStream ops = new FileOutputStream(“d:/AAAkejian/Test/test3.txt”);

      ObjectOutputStream oops = new ObjectOutputStream(ops);

      //使用對(duì)象流調(diào)用輸出流的輸出方法,被輸出的對(duì)象的類必須實(shí)現(xiàn)Serializable接口

      Role r1 = new Role(“gjx”,55,”撒潑打滾”);

      oops.writeObject(r1);

      oops.close();

      }

      2)對(duì)象輸入流的使用——ObjectInputStream–反序列化(讀檔):

      public void test()throws Exception{

      InputStream ips = new FileInputStream(“D:/AAAkejian/Test/test3.txt”);

      ObjectInputStream oips = new ObjectInputStream(ips);

      Object o = oips.readObject();

      System.out.println(o);

      oips.close();

      }

      鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
      (0)
      用戶投稿
      上一篇 2022年6月22日 15:34
      下一篇 2022年6月22日 18:09

      相關(guān)推薦

      聯(lián)系我們

      聯(lián)系郵箱:admin#wlmqw.com
      工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息