java--第9章 輸入輸出流

      網(wǎng)友投稿 778 2025-03-31

      實驗?zāi)康模?/p>

      1.理解I/O流的概念,掌握其分類。

      2.掌握文本文件讀寫、二進制文件讀寫。

      實驗內(nèi)容:

      1.編程實現(xiàn)任意文件的復(fù)制功能。

      2.利用文件流和緩沖流復(fù)制文件的內(nèi)容。

      3.創(chuàng)建文件和顯示文件的名稱和內(nèi)容。

      4.接收鍵盤輸入的字符串并顯示在屏幕上。

      實驗步驟:

      1.編寫一個程序?qū)⒆址懊髟滤砷g照,清泉石上流?!睂懭隒:\a.txt中,并實現(xiàn)對該文件的讀取。

      源代碼:

      package homework.實驗9_輸入輸出流;

      import Java.io.BufferedWriter;

      import Java.io.FileWriter;

      import java.io.IOException;

      import java.util.Scanner;

      public class sy9_1 {

      public static void main(String [] args){

      Scanner sc=new Scanner(System.in);

      String s=sc.nextLine();

      System.out.println("寫入字符串 : "+s);

      try {

      BufferedWriter bw=new BufferedWriter(new FileWriter("d:\a.txt"));

      bw.write(s);

      bw.flush();

      bw.close(); //關(guān)閉流

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

      }

      運行結(jié)果截圖:

      2. 編寫一個程序利用字節(jié)流將C:\a.txt中的內(nèi)容復(fù)制到另一個文件b.txt中,并提示“文件復(fù)制成功?!?。

      源代碼:

      package homework.實驗9_輸入輸出流;

      import java.io.*;

      public class sy9_2{

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

      FileInputStream read = new FileInputStream(new File("D:\a.txt"));

      FileOutputStream wr = new FileOutputStream(new File("D:\b.txt"));

      byte[] b = new byte[1024];

      int len = 0;

      while((len=read.read(b))!=-1){

      wr.write(b,0,len);

      wr.flush();

      }

      wr.close();

      read.close();

      }

      }

      運行結(jié)果截圖:

      3. 編寫一個程序利用字符流將C:\a.txt中的內(nèi)容復(fù)制到另一個文件b.txt中

      源代碼:

      package homework.實驗9_輸入輸出流;

      import java.io.FileNotFoundException;

      import java.io.FileReader;

      import java.io.FileWriter;

      import java.io.IOException;

      public class sy9_2 {

      public static void main(String[] args) {

      // TODO Auto-generated method stub

      FileReader fr = null;

      FileWriter fw = null;

      int c = 0;

      try {

      fr = new FileReader("d://a.txt");

      fw = new FileWriter("d://c.txt");

      while((c=fr.read())!=-1) {

      fw.write(c);

      }

      System.out.println("文件復(fù)制成功");

      }catch(FileNotFoundException e) {

      System.out.println("文件沒找到");

      }catch(IOException e) {

      System.out.println("輸入錯誤");

      }finally {

      try {

      fw.close();

      } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

      try {

      fr.close();

      } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

      }

      }

      }

      4.以下程序?qū)崿F(xiàn)對象序列化和反序列化,請把程序補充完整。

      Student 類:

      import java.io.Serializable;

      public class Student implements Serializable{

      private String name;

      private int age;

      private String address;

      public String getName() {

      return

      name

      ;

      }

      public void setName(String name) {

      this.name = name;

      }

      public int getAge() {

      return

      age

      ;

      }

      public void setAge(int age) {

      this.age = age;

      }

      public String getAddress() {

      return

      address

      ;

      }

      public void setAddress(String address) {

      this.address = address;

      }

      }

      測試類:

      import java.io.*;

      public class Test {

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

      Student mStudent1 =new Student();

      mStudent1.

      setName

      ("張三");

      mStudent1.setAge(18);

      mStudent1.setAddress("武漢市");

      //ObjectOutputStream代表對象輸出流,它的writeObject(Object obj)方法可對參數(shù)指定的obj對象進行序列化,

      //把得到的字節(jié)序列寫到一個目標(biāo)輸出流中,在這里寫到文件輸出流。

      java--第9章 輸入輸出流

      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/Student.txt"));

      oos.writeObject(

      mStudents1

      );

      oos.close();

      System.out.println("序列化完成");

      //ObjectInputStream代表對象輸入流,它的readObject()方法從一個源輸入流中讀取字節(jié)序列,

      //再把它們反序列化為一個對象,并將其返回。

      ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Student.txt"));

      Student mStudent2 = (

      Student

      ) ois.readObject();

      ois.close();? System.out.println(mStudent2.getName()+"\t"+mStudent2.getAge()+"\t"+mStudent2.getAddress());

      }

      }

      實驗小結(jié)

      通過數(shù)據(jù)流、序列化和文件系統(tǒng)提供系統(tǒng)輸入和輸出。Java把這些不同來源和目標(biāo)的數(shù)據(jù)都統(tǒng)一抽象為數(shù)據(jù)流。Java語言的輸入輸出功能是十分強大而靈活的,美中不足的是看上去輸入輸出的代碼并不是很簡潔,因為你往往需要包裝許多不同的對象。

      序列化和反序列化概念

      把對象轉(zhuǎn)換為字節(jié)序列的過程稱為對象的序列化。

      把字節(jié)序列恢復(fù)為對象的過程稱為對象的反序列化

      對象的序列化主要有兩種用途:

      1) 把對象的字節(jié)序列永久地保存到硬盤上,通常存放在一個文件中;

      2) 在網(wǎng)絡(luò)上傳送對象的字節(jié)序列。

      Java

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:甘特圖怎么看
      下一篇:Excel2016怎么繪制旭日圖?Excel2016旭日圖制作教程
      相關(guān)文章
      成人亚洲综合天堂| 亚洲精品国产精品国自产观看| 亚洲小说图区综合在线| 内射干少妇亚洲69XXX| 伊人久久大香线蕉亚洲| 亚洲AV中文无码乱人伦在线视色| 亚洲一卡2卡3卡4卡5卡6卡| 亚洲国产超清无码专区| 亚洲冬月枫中文字幕在线看| 亚洲国产中文在线视频| 亚洲av无码片在线观看| 亚洲乱码日产精品BD在线观看| 亚洲国产模特在线播放| 亚洲制服丝袜第一页| 亚洲精品伊人久久久久 | 亚洲色欲色欲www| 亚洲AV综合色区无码二区爱AV| 亚洲综合久久久久久中文字幕| 亚洲精品成人网站在线播放 | 亚洲日本中文字幕| 亚洲无砖砖区免费| 亚洲一区中文字幕在线观看| 亚洲人成图片网站| 亚洲永久网址在线观看| 亚洲a∨国产av综合av下载| 亚洲AV网站在线观看| 久久国产成人精品国产成人亚洲| 国产偷国产偷亚洲高清日韩| 亚洲中文字幕无码不卡电影 | 亚洲av日韩av永久无码电影| 色婷婷六月亚洲综合香蕉| yy6080亚洲一级理论| 亚洲午夜无码片在线观看影院猛| 久久亚洲av无码精品浪潮| 国产亚洲成av片在线观看| 无码乱人伦一区二区亚洲| 亚洲成A∨人片在线观看无码| 2020国产精品亚洲综合网| 欧美激情综合亚洲一二区| 亚洲综合最新无码专区| 久久亚洲精品视频|