《Java输入输出》的课堂示例代码


《Java输入输出》的课堂小例子,针对输入输出流、字符流、字节流和文件操作等知识点

示例文件(共9个小例子,把相应注释去掉后运行即可)

  1. package com.lecheng;  
  2.   
  3. import java.io.*;  
  4.   
  5. public class MainTest {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         //例子1 :从一个文件读数据,处理后存入另一个文件   
  12. /*      try { 
  13.             BufferedReader in = new BufferedReader(new FileReader("in.txt")); 
  14.             PrintWriter out = new PrintWriter(new BufferedWriter( 
  15.                     new FileWriter("out.txt"))); 
  16.             String s; 
  17.             int i = 1; 
  18.             while ((s = in.readLine()) != null) { 
  19.                 out.println("line " + i + "=" + s); 
  20.                 i++; 
  21.             } 
  22.             in.close(); 
  23.             out.close(); 
  24.             System.out.println("完成!"); 
  25.         } catch (FileNotFoundException e) { 
  26.             System.err.println("无法打开in.txt"); 
  27.         } catch (IOException e) { 
  28.             System.err.println("I/O exception"); 
  29.         }*/  
  30.   
  31.         //例子2:用字符流方式从键盘读入数据   
  32. //      try{   
  33. //          InputStreamReader isr= new InputStreamReader(System.in);   
  34. //          BufferedReader is = new BufferedReader(isr);   
  35. //          String inputLine;   
  36. //          while((inputLine = is.readLine())!=null) {    
  37. //              System.out.println(inputLine);   
  38. //          }   
  39. //          is.close();   
  40. //        }catch(IOException e){   
  41. //            System.out.println("IOException: " + e);   
  42. //        }   
  43.   
  44.         //例子3:将各种数据类型的数据以字节流方式存入文件   
  45. //      try{   
  46. //          StudentPojo stu = new StudentPojo(1, "张三", "石家庄", 56.8, false);   
  47. //          FileOutputStream fout = new FileOutputStream("text.txt");   
  48. //          DataOutputStream out = new DataOutputStream(fout);   
  49. //          out.writeInt(stu.getId());   
  50. //          out.writeChar('\n');   
  51. //          out.writeChars(stu.getName());   
  52. //          out.writeChar('\n');   
  53. //          out.writeChars(stu.getAddress());   
  54. //          out.writeChar('\n');   
  55. //          out.writeDouble(stu.getWeight());   
  56. //          out.writeChar('\n');   
  57. //          out.writeBoolean(stu.isSex());   
  58. //   
  59. //          out.close();   
  60. //        }catch(IOException e){   
  61. //        }   
  62.         //例子4:将例子3中的内容读出   
  63. //      DataInputStream in=null;   
  64. //      try{   
  65. //          in = new DataInputStream(new FileInputStream("text.txt"));   
  66. //          int id;   
  67. //          StringBuffer name, address;   
  68. //          double weight;   
  69. //          boolean sex;   
  70. //          char ch;   
  71. //          while(true){   
  72. //              id = in.readInt();   
  73. //              in.readChar();   
  74. //              name = new StringBuffer(20);   
  75. //              while((ch = in.readChar())!='\n'){   
  76. //                  name.append(ch);   
  77. //              }   
  78. //              address = new StringBuffer(20);   
  79. //              while((ch = in.readChar())!='\n'){   
  80. //                  address.append(ch);   
  81. //              }   
  82. //              weight = in.readDouble();   
  83. //              in.readChar();   
  84. //              sex = in.readBoolean();   
  85. //              System.out.println("ID:"+id);   
  86. //              System.out.println("姓名:"+name);   
  87. //              System.out.println("住址:"+address);   
  88. //              System.out.println("体重:"+weight);   
  89. //              System.out.println("性别:"+sex);   
  90. //           }   
  91. //      }catch(IOException e){   
  92. //      }   
  93.         //例子5:使用FileInputStream和FileOutputStream实现文件拷贝   
  94. /*      try{ 
  95.               File inFile=new File("in.txt"); 
  96.               File outFile=new File("out.txt"); 
  97.               FileInputStream fis=new FileInputStream(inFile); 
  98.               FileOutputStream fos=new  FileOutputStream(outFile); 
  99.               int i=0, c;   
  100.               while((c=fis.read())!=-1){ 
  101.                   fos.write(c); 
  102.               } 
  103.               fis.close();  
  104.               fos.close(); 
  105.             }catch(FileNotFoundException e) { 
  106.                 System.err.println("FileStreamsTest: "+e); 
  107.             }catch(IOException e) { 
  108.                 System.err.println("FileStreamsTest: "+e); 
  109.             }*/  
  110.         //例子6:获取某路径下文件信息   
  111. //      File files = new File("c:\\");   
  112. //      String fileList[] = files.list();   
  113. //      for(int i = 0; i < fileList.length; i++){   
  114. //          File currfiles = new File("c:\\"+fileList[i]);   
  115. //          System.out.print("文件名:" + fileList[i] + "\t");   
  116. //          System.out.println("文件大小:" + currfiles.length());   
  117. //      }   
  118.         //例子7:用字符流方式从键盘读入数据后存入文件   
  119. //          try{   
  120. //              InputStreamReader isr= new InputStreamReader(System.in);   
  121. //              BufferedReader br = new BufferedReader(isr);   
  122. //              FileWriter fw = new FileWriter("char.txt");   
  123. //              BufferedWriter bw = new BufferedWriter(fw);   
  124. //              String str;   
  125. //              while(true){   
  126. //                  System.out.print("请输入一个字符串:");   
  127. //                  System.out.flush();   
  128. //                  str = br.readLine();   
  129. //                  if(str.length()==0){   
  130. //                      break;   
  131. //                  }   
  132. //                  bw.write(str);   
  133. //                  bw.newLine();   
  134. //              }   
  135. //              bw.close();   
  136. //            }catch(IOException e){   
  137. //                System.out.println("IOException: " + e);   
  138. //            }   
  139.         //例子8:用字符流方式从文件中读入数据,用system.out输出到屏幕   
  140. //      try{   
  141. //          FileReader fr = new FileReader("char.txt");   
  142. //          BufferedReader br = new BufferedReader(fr);   
  143. //          int lineNum = 0;   
  144. //          String str = br.readLine();   
  145. //          while(str != null){   
  146. //              lineNum++;   
  147. //              System.out.println("第"+lineNum+"行:"+str);   
  148. //              str = br.readLine();   
  149. //          }   
  150. //      }catch(IOException e){   
  151. //          System.out.println("IOException: " + e);   
  152. //      }   
  153.         //例子9:用字符流方式从文件中读入数据,用流方式输出到屏幕   
  154. //      try{   
  155. //          FileReader fr = new FileReader("char.txt");   
  156. //          BufferedReader br = new BufferedReader(fr);   
  157. //          OutputStreamWriter osw = new OutputStreamWriter(System.out);   
  158. //          BufferedWriter bw = new BufferedWriter(osw);   
  159. //          int lineNum = 0;   
  160. //          String str = br.readLine();   
  161. //             
  162. //          while(str != null){   
  163. //              lineNum++;   
  164. //              bw.write(String.valueOf(lineNum));   
  165. //              bw.write(" ");   
  166. //              bw.write(str);   
  167. //              bw.newLine();   
  168. //              str = br.readLine();   
  169. //          }   
  170. //          bw.close();   
  171. //      }catch(IOException e){   
  172. //          System.out.println("IOException: " + e);   
  173. //      }   
  174.   
  175.     }  
  176.   
  177. }  

用到的POJO类

  1. package com.lecheng;  
  2.   
  3. public class StudentPojo {  
  4.     private int id;  
  5.     private String name;  
  6.     private String address;  
  7.     private double weight;  
  8.     private boolean sex;  
  9.     /** 
  10.      * @param id 
  11.      * @param name 
  12.      * @param address 
  13.      * @param weight 
  14.      * @param sex 
  15.      */  
  16.     public StudentPojo(int id, String name, String address, double weight,  
  17.             boolean sex) {  
  18.         this.id = id;  
  19.         this.name = name;  
  20.         this.address = address;  
  21.         this.weight = weight;  
  22.         this.sex = sex;  
  23.     }  
  24.     /** 
  25.      * @return the id 
  26.      */  
  27.     public int getId() {  
  28.         return id;  
  29.     }  
  30.     /** 
  31.      * @param id the id to set 
  32.      */  
  33.     public void setId(int id) {  
  34.         this.id = id;  
  35.     }  
  36.     /** 
  37.      * @return the name 
  38.      */  
  39.     public String getName() {  
  40.         return name;  
  41.     }  
  42.     /** 
  43.      * @param name the name to set 
  44.      */  
  45.     public void setName(String name) {  
  46.         this.name = name;  
  47.     }  
  48.     /** 
  49.      * @return the address 
  50.      */  
  51.     public String getAddress() {  
  52.         return address;  
  53.     }  
  54.     /** 
  55.      * @param address the address to set 
  56.      */  
  57.     public void setAddress(String address) {  
  58.         this.address = address;  
  59.     }  
  60.     /** 
  61.      * @return the weight 
  62.      */  
  63.     public double getWeight() {  
  64.         return weight;  
  65.     }  
  66.     /** 
  67.      * @param weight the weight to set 
  68.      */  
  69.     public void setWeight(double weight) {  
  70.         this.weight = weight;  
  71.     }  
  72.     /** 
  73.      * @return the sex 
  74.      */  
  75.     public boolean isSex() {  
  76.         return sex;  
  77.     }  
  78.     /** 
  79.      * @param sex the sex to set 
  80.      */  
  81.     public void setSex(boolean sex) {  
  82.         this.sex = sex;  
  83.     }  
  84.       
  85. }  

相关内容