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();
}
}
}
使用缓冲流得好处在于 能够一行一行得读取 方便修改