歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 《Java輸入輸出》的課堂示例代碼

《Java輸入輸出》的課堂示例代碼

日期:2017/3/1 10:14:44   编辑:Linux編程

《Java輸入輸出》的課堂小例子,針對輸入輸出流、字符流、字節流和文件操作等知識點

示例文件(共9個小例子,把相應注釋去掉後運行即可)

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

用到的POJO類

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