《Java輸入輸出》的課堂小例子,針對輸入輸出流、字符流、字節流和文件操作等知識點
示例文件(共9個小例子,把相應注釋去掉後運行即可)
- package com.lecheng;
-
- import java.io.*;
-
- public class MainTest {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- //例子1 :從一個文件讀數據,處理後存入另一個文件
- /* try {
- BufferedReader in = new BufferedReader(new FileReader("in.txt"));
- PrintWriter out = new PrintWriter(new BufferedWriter(
- new FileWriter("out.txt")));
- String s;
- int i = 1;
- while ((s = in.readLine()) != null) {
- out.println("line " + i + "=" + s);
- i++;
- }
- in.close();
- out.close();
- System.out.println("完成!");
- } catch (FileNotFoundException e) {
- System.err.println("無法打開in.txt");
- } catch (IOException e) {
- System.err.println("I/O exception");
- }*/
-
- //例子2:用字符流方式從鍵盤讀入數據
- // try{
- // InputStreamReader isr= new InputStreamReader(System.in);
- // BufferedReader is = new BufferedReader(isr);
- // String inputLine;
- // while((inputLine = is.readLine())!=null) {
- // System.out.println(inputLine);
- // }
- // is.close();
- // }catch(IOException e){
- // System.out.println("IOException: " + e);
- // }
-
- //例子3:將各種數據類型的數據以字節流方式存入文件
- // try{
- // StudentPojo stu = new StudentPojo(1, "張三", "石家莊", 56.8, false);
- // FileOutputStream fout = new FileOutputStream("text.txt");
- // DataOutputStream out = new DataOutputStream(fout);
- // out.writeInt(stu.getId());
- // out.writeChar('\n');
- // out.writeChars(stu.getName());
- // out.writeChar('\n');
- // out.writeChars(stu.getAddress());
- // out.writeChar('\n');
- // out.writeDouble(stu.getWeight());
- // out.writeChar('\n');
- // out.writeBoolean(stu.isSex());
- //
- // out.close();
- // }catch(IOException e){
- // }
- //例子4:將例子3中的內容讀出
- // DataInputStream in=null;
- // try{
- // in = new DataInputStream(new FileInputStream("text.txt"));
- // int id;
- // StringBuffer name, address;
- // double weight;
- // boolean sex;
- // char ch;
- // while(true){
- // id = in.readInt();
- // in.readChar();
- // name = new StringBuffer(20);
- // while((ch = in.readChar())!='\n'){
- // name.append(ch);
- // }
- // address = new StringBuffer(20);
- // while((ch = in.readChar())!='\n'){
- // address.append(ch);
- // }
- // weight = in.readDouble();
- // in.readChar();
- // sex = in.readBoolean();
- // System.out.println("ID:"+id);
- // System.out.println("姓名:"+name);
- // System.out.println("住址:"+address);
- // System.out.println("體重:"+weight);
- // System.out.println("性別:"+sex);
- // }
- // }catch(IOException e){
- // }
- //例子5:使用FileInputStream和FileOutputStream實現文件拷貝
- /* try{
- File inFile=new File("in.txt");
- File outFile=new File("out.txt");
- FileInputStream fis=new FileInputStream(inFile);
- FileOutputStream fos=new FileOutputStream(outFile);
- int i=0, c;
- while((c=fis.read())!=-1){
- fos.write(c);
- }
- fis.close();
- fos.close();
- }catch(FileNotFoundException e) {
- System.err.println("FileStreamsTest: "+e);
- }catch(IOException e) {
- System.err.println("FileStreamsTest: "+e);
- }*/
- //例子6:獲取某路徑下文件信息
- // File files = new File("c:\\");
- // String fileList[] = files.list();
- // for(int i = 0; i < fileList.length; i++){
- // File currfiles = new File("c:\\"+fileList[i]);
- // System.out.print("文件名:" + fileList[i] + "\t");
- // System.out.println("文件大小:" + currfiles.length());
- // }
- //例子7:用字符流方式從鍵盤讀入數據後存入文件
- // try{
- // InputStreamReader isr= new InputStreamReader(System.in);
- // BufferedReader br = new BufferedReader(isr);
- // FileWriter fw = new FileWriter("char.txt");
- // BufferedWriter bw = new BufferedWriter(fw);
- // String str;
- // while(true){
- // System.out.print("請輸入一個字符串:");
- // System.out.flush();
- // str = br.readLine();
- // if(str.length()==0){
- // break;
- // }
- // bw.write(str);
- // bw.newLine();
- // }
- // bw.close();
- // }catch(IOException e){
- // System.out.println("IOException: " + e);
- // }
- //例子8:用字符流方式從文件中讀入數據,用system.out輸出到屏幕
- // try{
- // FileReader fr = new FileReader("char.txt");
- // BufferedReader br = new BufferedReader(fr);
- // int lineNum = 0;
- // String str = br.readLine();
- // while(str != null){
- // lineNum++;
- // System.out.println("第"+lineNum+"行:"+str);
- // str = br.readLine();
- // }
- // }catch(IOException e){
- // System.out.println("IOException: " + e);
- // }
- //例子9:用字符流方式從文件中讀入數據,用流方式輸出到屏幕
- // try{
- // FileReader fr = new FileReader("char.txt");
- // BufferedReader br = new BufferedReader(fr);
- // OutputStreamWriter osw = new OutputStreamWriter(System.out);
- // BufferedWriter bw = new BufferedWriter(osw);
- // int lineNum = 0;
- // String str = br.readLine();
- //
- // while(str != null){
- // lineNum++;
- // bw.write(String.valueOf(lineNum));
- // bw.write(" ");
- // bw.write(str);
- // bw.newLine();
- // str = br.readLine();
- // }
- // bw.close();
- // }catch(IOException e){
- // System.out.println("IOException: " + e);
- // }
-
- }
-
- }
用到的POJO類
- package com.lecheng;
-
- public class StudentPojo {
- private int id;
- private String name;
- private String address;
- private double weight;
- private boolean sex;
- /**
- * @param id
- * @param name
- * @param address
- * @param weight
- * @param sex
- */
- public StudentPojo(int id, String name, String address, double weight,
- boolean sex) {
- this.id = id;
- this.name = name;
- this.address = address;
- this.weight = weight;
- this.sex = sex;
- }
- /**
- * @return the id
- */
- public int getId() {
- return id;
- }
- /**
- * @param id the id to set
- */
- public void setId(int id) {
- this.id = id;
- }
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the address
- */
- public String getAddress() {
- return address;
- }
- /**
- * @param address the address to set
- */
- public void setAddress(String address) {
- this.address = address;
- }
- /**
- * @return the weight
- */
- public double getWeight() {
- return weight;
- }
- /**
- * @param weight the weight to set
- */
- public void setWeight(double weight) {
- this.weight = weight;
- }
- /**
- * @return the sex
- */
- public boolean isSex() {
- return sex;
- }
- /**
- * @param sex the sex to set
- */
- public void setSex(boolean sex) {
- this.sex = sex;
- }
-
- }