Java IO流題庫

1、    填空題java

  1. Java IO流能夠分爲   節點流   和處理流兩大類,其中前者處於IO操做的第一線,全部操做必須經過他們進行。
  2. 輸入流的惟一目的是提供通往數據的通道,程序能夠經過這個通道讀取數據, read

方法給程序提供了一個從輸入流中讀取數據的基本方法。設計模式

  1. read方法從輸入流中順序讀取源中的單個字節數據,該方法返回字節值(0-255之間的一個整數),若是到達源的末尾,該方法返回  -1   
  2. Java系統的標準輸入對象是System.in,標準輸出對象有兩個,分別是標準輸出System.out和標準錯誤輸出____System.err__。
  3. Java IO體系中,___ ObjectInputStream __是字節輸入流,不只提供了存取全部Java基礎類型數據(如:int,double 等)和String的方法,也提供了提供存取對象的方法。
  4. Java IO體系中,____ DataOutputStream __是字節輸出流,提供了能夠存取全部Java基礎類型數據(如:int,double 等)和String的方法,但沒有提供存取對象的方法。
  5. ___序列化__是指將Java對象轉換成字節序列,從而能夠保存到磁盤上,也能夠在網絡上傳輸,使得不一樣的計算機能夠共享對象。

 

2、    選擇題數組

 

1.網絡

使用Java IO流實現對文本文件的讀寫過程當中,須要處理下列(  B  )異常。(選擇一項)this

 

 

 

 

A編碼

ClassNotFoundExceptionspa

 

B.設計

IOException代理

 

C.code

SQLException

 

D.

RemoteException

 

2.

JavaIO操做中,(  D  )方法能夠用來刷新流的緩衝。(選擇兩項)

 

 

 

 

A

void release()

 

B.

void close()

 

C.

void remove()

 

D.

void flush()

 

3.

Java中,下列關於讀寫文件的描述錯誤的是(  B  )。(選擇一項)

 

 

 

 

A

Reader類的read()方法用來從源中讀取一個字符的數據

 

B.

Reader類的read(int n )方法用來從源中讀取一個字符的數據

 

C.

Writer類的write(int n)方法用來向輸出流寫入單個字符

 

D.

Writer類的write(String str)方法用來向輸出流寫入一個字符串

 

 

 

4.

閱讀下列文件定入的Java代碼,共有(  C  )處錯誤。(選擇一項)

 

import java.io.*;

public class TestIO {

         public static void main(String []args){

                   String str ="文件寫入練習";

                   FileWriter fw = null;        //1

                   try{

                            fw = new FileWriter("c:\mytext.txt");  //2

                            fw.writerToEnd(str);   //3

                   }catch(IOException e){   //4

                            e.printStackTrace();

                   }finally{

                            //此處省略關閉流

                   }

         }

}

 

 

 

 

A

0

 

B.

1

 

C.

2

 

D.

3

 

5.

分析以下Java代碼,有標註的四行代碼中,有錯誤的是第( D )處。(選擇一項)

 

import java.io.FileWriter;

import java.io.IOException;

public class Test {

         public static void main(String[ ] args) {

                   String str = "Hello World";

                   FileWriter fw = null;

                   try {

                            fw = new FileWriter("c:\\hello.txt"); // 1

                            fw.write(str);                     // 2

                   } catch (IOException e) {

                            e.printStackTrace();               // 3

                   } finally {

                            fw.close();                        // 4

                   }

         }

}

 

 

 

 

A

1

 

B.

2

 

C.

3

 

D.

4

 

6.

如下選項中關於以下代碼的說法正確的是(  AD  。(選擇二項)

 

public class TestBuffered {

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

                   BufferedReader br =

                            new BufferedReader(new FileReader("d:/bjsxt1.txt"));

                   BufferedWriter bw =

                            new BufferedWriter(new FileWriter("d:/bjsxt2.txt"));

                   String str = br.readLine();

                   while(str !=null){

                            bw.write(str);

                            bw.newLine();

                            str = br.readLine();

                   }

                   br.close();

                   bw.close();     

         }

}

 

 

 

 

A.

該類使用字符流實現了文件複製,將d:/bjsxt1.txt複製爲d:/bjsxt2.txt

 

B.

FileReader和FileWriter是處理流,直接從文件讀寫數據

 

C.

BufferedReader和BufferedWriter是節點流,提供緩衝區功能,提升讀寫效率

 

D.

readLine()能夠讀取一行數據,返回值是字符串類型,簡化了操做

 

7.

InputStreamReader是轉換流,能夠將字節流轉換成字符流,是字符流與字節流之間的橋樑。它的實現使用的設計模式是(  C  。(選擇一項)

 

 

 

 

A.

工廠模式

 

B.

裝飾模式

 

C.

適配器模式

 

D.

代理模式

 

3、    判斷題

  1. 假設文件」a.txt」的長度爲100字節,那麼當正常運行語句」OutputStream f=new FileOutputStream(new File(「a.txt」));」以後,文件」a.txt」的長度變爲0字節。(  T  )
  2. ByteArrayInutStream和ByteArrayOutputStream對內存中的字節數組進行讀寫操做,屬於字節流,屬於處理流而不是節點流。 (  F  )
  3. 實現Serializable接口的能夠被序列化和反序列化。該接口中沒有定義抽象方法,也沒有定義常量。(  T  )
  4. 序列化是指將字節序列轉換成Java對象,只有實現了Serializable接口的類的對象才能夠被序列化。(  F  )

 

 

4、    簡答題

  1. 輸入流和輸出流的聯繫和區別,字符流和字節流的聯繫和區別

 

  1. 列舉經常使用的字節輸入流和字節輸出流並說明其特色,至少5對。

 

  1. 說明緩衝流的優勢和原理

 

  1. 序列化的定義、實現和注意事項

 

5、    編碼題

1.實現字符串和字節數組之間的相互轉換。必如將字符串「北京尚學堂bjsxt」轉換爲字節數組,並將字節數組再轉換回字符串。

 1 public class TestConvert  2 {  3     public static void main(String[] args) throws IOException  4  {  5         //準備一個字符串
 6         String contents = " 近日,北京尚學堂科技有限公司正式成爲央視網廣告合做夥伴";  7  System.out.println(contents);  8         //String---byte []
 9         byte[] buf = contents.getBytes(); 10         //byte[]----String
11         String contents2 = new String(buf, 0, buf.length); 12  System.out.println(contents2); 13  } 14

2.實現字節數組和任何基本類型和引用類型執行的相互轉換

提示:使用ByteArrayInutStream和ByteArrayOutputStream。

 

 1 public class TestByteArrayStream  2 {  3     public static void main(String[] args) throws IOException,  4  ClassNotFoundException  5  {  6         int num = 50;  7         boolean flag = true;  8         User user = new User("bjsxt", "bjsxt");  9         //使用數據包把數據封裝起來 10         //各類數據類型----->byte[] ByteArrayOutputStream 
11         ByteArrayOutputStream baos = new ByteArrayOutputStream(); 12         ObjectOutputStream oos = new ObjectOutputStream(baos);//包裝流
13  oos.writeInt(num); 14  oos.writeBoolean(flag); 15  oos.writeObject(user); 16         byte[] buf = baos.toByteArray(); 17  baos.close(); 18         //byte[]----------->各類數據類型
19         ByteArrayInputStream bais = new ByteArrayInputStream(buf); 20         ObjectInputStream ois = new ObjectInputStream(bais); 21         int num2 = ois.readInt(); 22         boolean flag2 = ois.readBoolean(); 23         User user2 = (User) ois.readObject(); 24  System.out.println(num2); 25  System.out.println(flag2); 26  System.out.println(user2); 27  bais.close(); 28  } 29 }

3.分別使用文件流和緩衝流複製一個長度大於100MB的視頻文件,並觀察效率的差別。

 

 1 public class TestCopy4  2 {  3     public static void main(String[] args) throws IOException  4  {  5         //建立輸入流和輸出流
 6         InputStream fis = new FileInputStream(new File("d:/1.mp4"));  7         OutputStream fos = new FileOutputStream("d:/2.mp4");  8         //使用輸入流和輸出流複製文件
 9         byte[] buf = new byte[10]; 10         int len = fis.read(buf); 11         while (len != -1) 12  { 13             //
14             fos.write(buf, 0, len); 15             //
16             len = fis.read(buf); 17             //System.out.println(len);
18  } 19         //關閉輸入流和輸出流
20  fis.close(); 21  fos.close(); 22  } 23 } 24 public class TestCopy 25 { 26     public static void main(String[] args) throws IOException 27  { 28         //建立輸入流和輸出流
29         InputStream fis = new FileInputStream(new File("d:/1.mp4")); 30         OutputStream fos = new FileOutputStream("d:/2.mp4"); 31         BufferedInputStream bis = new BufferedInputStream(fis); 32         BufferedOutputStream bos = new BufferedOutputStream(fos); 33         //使用輸入流和輸出流複製文件
34         byte[] buf = new byte[10]; 35         int len = bis.read(buf); 36         while (len != -1) 37  { 38             //
39             bos.write(buf, 0, len); 40             //
41             len = bis.read(buf); 42  } 43         //關閉輸入流和輸出流
44  bis.close(); 45  bos.close(); 46  } 47 }

4.複製文件夾d:/sxtjava下面全部文件和子文件夾內容到d:/sxtjava2。

提示:涉及單個文件複製、目錄的建立、遞歸的使用

 

 1 public class CopyDir  2 {  3     /**
 4  *  5  * 複製單個文件  6  *  7  * @param sourceFile 源文件  8  *  9  * @param targetFile 目標文件  10  *  11  * @throws IOException  12  *  13      */
 14     public static void copyFile(File sourceFile, File targetFile) throws IOException  15  {  16         BufferedInputStream inBuff = null;  17         BufferedOutputStream outBuff = null;  18         try
 19  {  20             // 新建文件輸入流
 21             inBuff = new BufferedInputStream(new FileInputStream(sourceFile));  22             // 新建文件輸出流
 23             outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));  24             // 緩衝數組
 25             byte[] b = new byte[1024 * 5];  26             int len;  27             while ((len = inBuff.read(b)) != -1)  28  {  29                 outBuff.write(b, 0, len);  30  }  31             // 刷新此緩衝的輸出流
 32  outBuff.flush();  33         } finally
 34  {  35             // 關閉流
 36             if (inBuff != null)  37  {  38  inBuff.close();  39  }  40             if (outBuff != null)  41  {  42  outBuff.close();  43  }  44  }  45  }  46     /**
 47  *  48  * 複製目錄  49  *  50  * @param sourceDir 源目錄  51  *  52  * @param targetDir 目標目錄  53  *  54  * @throws IOException  55  *  56      */
 57     public static void copyDirectiory(String sourceDir, String targetDir)  58             throws IOException  59  {  60         // 檢查源目錄
 61         File fSourceDir = new File(sourceDir);  62         if (!fSourceDir.exists() || !fSourceDir.isDirectory())  63  {  64             System.out.println("源目錄不存在");  65             return;  66  }  67         //檢查目標目錄,如不存在則建立
 68         File fTargetDir = new File(targetDir);  69         if (!fTargetDir.exists())  70  {  71  fTargetDir.mkdirs();  72  }  73         // 遍歷源目錄下的文件或目錄
 74         File[] file = fSourceDir.listFiles();  75         for (int i = 0; i < file.length; i++)  76  {  77             if (file[i].isFile())  78  {  79                 // 源文件
 80                 File sourceFile = file[i];  81                 // 目標文件
 82                 File targetFile = new File(fTargetDir, file[i].getName());  83  copyFile(sourceFile, targetFile);  84  }  85             //遞歸複製子目錄
 86             if (file[i].isDirectory())  87  {  88                 // 準備複製的源文件夾
 89                 String subSourceDir = sourceDir + File.separator + file[i].getName();  90                 // 準備複製的目標文件夾
 91                 String subTargetDir = targetDir + File.separator + file[i].getName();  92                 // 複製子目錄
 93  copyDirectiory(subSourceDir, subTargetDir);  94  }  95  }  96  }  97     public static void main(String[] args) throws IOException  98  {  99         copyDirectiory("d:/sxtjava", "d:/sxtjava2"); 100  } 101 }

可選題

1.使用IO包中的類讀取D盤上exam.txt文本文件的內容,每次讀取一行內容,將每行做爲一個輸入放入ArrayList的泛型集合中並將集合中的內容使用增強for進行輸出顯示。

 

 1 public class Test  2 {  3     public static void main(String[] args) throws IOException  4  {  5         String path = "D:\\exam.txt";  6  outputMethod(path);  7  }  8     public static void outputMethod(String path) throws IOException  9  { 10         List<String> list = new ArrayList<String>(); // 建立集合對象 11         // 建立緩衝區對象
12         BufferedReader br = new BufferedReader(new FileReader(path)); 13         String line = br.readLine(); // 讀取數據每次讀一行
14         while (line != null) 15  { 16  list.add(line); 17             line = br.readLine(); 18  } 19         br.close();              //關閉
20         for (String s : list) 21  { 22  System.out.println(s); 23  } 24  } 25 }

2.假設從入學開始全部書寫的Java類代碼都在d:/sxtjava文件夾下,包括多級子文件夾。使用IO流獲取從入學開始,到目前爲止已經寫了多少行Java代碼。

提示:

其實就是獲取d:/sxtjava文件夾及其子文件夾下的全部.java文件,使用readLine()讀取其中每一行,每讀取一行,行數加1。全部的文件讀取完畢,獲得總共已經寫的Java代碼行數。須要結合遞歸實現。

 

 1 public class TestCountDir  2 {  3     private int count;  4     /**
 5  *  6  * 統計一個java文件的行數  7  *  8      */
 9     private void countLine(File sourceFile) throws IOException 10  { 11         BufferedReader br = null; 12         try
13  { 14             // 新建文件輸入流
15             br = new BufferedReader(new FileReader(sourceFile)); 16             while (br.readLine() != null) 17  { 18                 count++; 19                 //System.out.println(count);
20  } 21         } finally
22  { 23  br.close(); 24  } 25  } 26     /**
27  * 28  * 統計一個目錄下全部Java文件的行數 29  * 30      */
31     private void countDir(String sourceDir) throws IOException 32  { 33         // 檢查源目錄
34         File fSourceDir = new File(sourceDir); 35         if (!fSourceDir.exists() || !fSourceDir.isDirectory()) 36  { 37             System.out.println("源目錄不存在"); 38             return; 39  } 40         // 遍歷目錄下的文件或目錄
41         File[] file = fSourceDir.listFiles(); 42         for (int i = 0; i < file.length; i++) 43  { 44             if (file[i].isFile()) 45  { 46                 if (file[i].getName().toLowerCase().endsWith(".java")) 47  { 48                     // System.out.println(file[i].getName());
49  countLine(file[i]); 50  } 51  } 52             //遞歸統計代碼行數
53             if (file[i].isDirectory()) 54  { 55                 // 準備統計的文件夾
56                 String subSourceDir = sourceDir + File.separator + file[i].getName(); 57                 // 統計子目錄
58  countDir(subSourceDir); 59  } 60  } 61  } 62     public static void main(String[] args) throws IOException 63  { 64         TestCountDir tcd = new TestCountDir(); 65         tcd.countDir("d:/sxtjava"); 66  System.out.println(tcd.count); 67  } 68 }

3.由控制檯按照固定格式輸入學生信息,包括學號,姓名,年齡信息,當輸入的內容爲exit退出;將輸入的學生信息分別封裝到一個Student對象中,再將每一個Student對象加入到一個集合中,要求集合中的元素按照年齡大小正序排序;最後遍歷集合,將集合中學生信息寫入到記事本,每一個學生數據佔單獨一行。

 

 1 public class Student implements Comparable<Student>
 2 {  3     private Integer num;  4     private String name;  5     private Integer age;  6     //省略getter和setter方法  7     //省略構造方法
 8     public int compareTo(Student stu)  9  { 10         return this.age - stu.age; 11  } 12     public String toString() 13  { 14         return "Student [age=" + age + ", name=" + name 15                 + ", num=" + num + "]"; 16  } 17 } 18 public class Test 19 { 20     public static void main(String[] args) 21  { 22         Set<Student> stuSet = saveStudentInfo(); 23  outputInfo(stuSet); 24  } 25     private static Set<Student> saveStudentInfo() 26  { 27         Scanner input = new Scanner(System.in); 28         // 保存學生信息的TreeSet集合對象
29         Set<Student> stuSet = new TreeSet<Student>(); 30         while (true) 31  { 32             // 輸入提示
33             System.out.println("請輸入學生信息:(學號#姓名#年齡)"); 34             String inputData = input.nextLine(); 35             // 判斷是否退出 inputData.equals("exit")
36             if ("exit".equals(inputData)) 37  { 38                 break; 39  } 40             // 將用戶輸入的學生信息分割爲String[]
41             String[] info = inputData.split("#"); 42             // 將輸入信息封裝到Student對象中
43  Student stu 44                     = new Student(Integer.parseInt(info[0]), info[1], 45                             Integer.parseInt(info[2])); 46             // 將學生對象加入集合
47  stuSet.add(stu); 48  } 49         return stuSet; 50  } 51     private static void outputInfo(Set<Student> stuSet) 52  { 53         File file = new File("e:/student.txt"); 54         // 建立文件輸出流對象
55         FileWriter fw = null; 56         try
57  { 58             fw = new FileWriter(file); 59             Iterator<Student> it = stuSet.iterator(); 60             while (it.hasNext()) 61  { 62                 String info = it.next().toString(); 63                 // 將info字符串,寫入記事本
64  fw.write(info); 65                 // 完成換行功能
66                 fw.write("\r\n"); 67  } 68         } catch (Exception e) 69  { 70  e.printStackTrace(); 71         } finally
72  { 73             try
74  { 75  fw.close(); 76             } catch (IOException e) 77  { 78  e.printStackTrace(); 79  } 80  } 81  } 82 }
相關文章
相關標籤/搜索