Java practice io自动创建类(System.in)

System.in
配合
import java.io.IOException;
import java.io.InputStream;
两个包的导入 在try catch下进行操作

 try (InputStream is = System.in;) {
            while (true) {
                int i = is.read();
                System.out.println(i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

每一次输入 都会进行读取再 由System.out.println(); 进行打印
但是不管是输入输出,System.out是输出首选 但是Scanner 比System.in要好用得多。

自动创建类:

通过控制台,获取类名,属性名称,属性类型,根据一个模板文件,自动创建这个类文件,并且为属性提供setter和getter
需要两个文件:一个存模板文件 一个用于存储修改后的文件

package test;
import java.io.*;
import java.util.Scanner;

public class useFile{
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		System.out.println("请输入类的名称:");
		String CLASS =s.nextLine();
		System.out.println("请输入属性的类型:");
		String Type= s.nextLine();
		System.out.println("请输入属性的名称");
		String Property=s.nextLine();
		File f=new File("D:\\Jproject\\流.txt");
		File gotch=new File(String.format("D:\\Jproject\\by.txt", CLASS));
		try(
				FileReader rf=new FileReader(f);
				BufferedReader sd=new BufferedReader(rf);
				FileWriter rw=new FileWriter(gotch);
				PrintWriter we=new PrintWriter(rw);	
			)
		{
			System.out.println("替换后的内容:");
			String str;
			while(null!=(str=sd.readLine()))
			{
			
			for (String zi : str.split("@")) {
				if(zi.equals("class"))
				{
					we.print(CLASS);
					System.out.print(CLASS);
				}
				else if(zi.equals("type"))
				{
					we.print(Type);
					System.out.print(Type);
				}
				else if(zi.equals("property")||zi.equals("Uproperty"))
				{
					we.print(Property);
					System.out.print(Property);
				}
				else
				{
					we.print(zi);
					System.out.print(zi);
				}
			}
			we.println();
			System.out.println();
			}
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	
	}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
使用缓冲流得好处在于 能够一行一行得读取 方便修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值