Java IO流--应用场景练习

Java IO流应用场景编程

序言

Java提供了丰富的字节流基类InputStream和OutputStream的子类,字符流Reader和Writer的子类,以适应不同应用场景。在实际使用Java IO时需要对各种IO流进行组合以适应场景需要。

应用场景

1. 从键盘获取用户输入
		Employee e = new Employee();
		System.out.println("Please input Employee name:");
		Scanner in = new Scanner(System.in);
		String name = in.nextLine();
		
		System.out.println("Please input Employee salary:");
		double salary = in.nextDouble();
		
		System.out.println("Please input Employee hireday yymmdd:");
		int year = in.nextInt();
		int month = in.nextInt();
		int dayOfMonth = in.nextInt();
2. 文件拷贝

在这里插入图片描述

public class FileCopyText {

	public static void main(String[] args) {
		int temp = 0;
		// TODO Auto-generated method stub
		try {
			FileInputStream in = new FileInputStream("mF.BMP");
			FileOutputStream out = new FileOutputStream("mFCopy.BMP", true);
			while((temp = in.read()) != -1) {
				out.write(temp);
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		
	}

}

3. Java基本数据类型保存到文件

DataOutputStream作为装饰类,配合文件输出流,可以方便的将Java基本数据类型数据保持到文件。
在这里插入图片描述

public static void WriteEmploy(Employee employee, DataOutputStream out) {
		try {
			out.writeChars(employee.getName());
			out.writeDouble(employee.getSalary());
			out.writeInt(employee.getHireDay().getYear());
			out.writeInt(employee.getHireDay().getMonthValue());
			out.writeInt(employee.getHireDay().getDayOfMonth());
		}catch(IOException  e){
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		Employee employee = readEmploy(in);
		try {
			DataOutputStream out = new DataOutputStream(new FileOutputStream("testfile"));
			WriteEmploy(employee, out);
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
4.文本类型数据保存

PrintWriter(public class PrintWriter extends Writer)
可用来创建一个文件并向文本文件写入数据,支持基础数据类型数据写。

PrintWriter常用方法

public PrintWriter(Writer out,boolean autoFlush)构造方法
public void print(char c)普通方法向文件写入一个字符
public void print(int i)普通方法向文件写入一个整数
public void print(long l)普通方法向文件写入一个long数据
public void print(float f)普通方法向文件写入一个单精度浮点数
public void print(double d)普通方法向文件写入一个双精度浮点数
public void print(String s)普通方法向文件写入一个字符串
public static void writeEmployeeData(Employee employee, PrintWriter out)
	{
		out.println(employee.getName() + "|" + employee.getSalary() + "|" + employee.getHireDay().toString());
		return;
	}
	
	public static void main(String[] args) {
		Employee employee = readEmploy(new Scanner(System.in));
		try(PrintWriter out = new PrintWriter("employee.txt","UTF-8")) {
			writeEmployeeData(employee, out);
		}catch (IOException  e) {
			e.printStackTrace();
		}
	}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值