“Java输入输出”的课堂示例代码

来源:互联网 发布:ug8.o编程视频教程 编辑:程序博客网 时间:2024/06/05 15:04

《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;}}