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

本文提供了一系列Java输入输出流的实际应用案例,包括文件读写、数据类型存储与读取、文件复制及基本信息获取等,有助于深入理解IO流的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值