자바 콘솔 입력 스트림 System.in 및 스캐너
System.out으로는 콘솔 출력의 공통 데이터 인
System.in의 데이터 콘솔에서 입력 될 수있다
1 단계 : System.in
package stream;
import java.io.IOException;
import java.io.InputStream;
public class TestStream {
public static void main(String[] args) {
// 控制台输入
try (InputStream is = System.in;) {
while (true) {
// 敲入a,然后敲回车可以看到
// 97 13 10
// 97是a的ASCII码
// 13 10分别对应回车换行
int i = is.read();
System.out.println(i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2 단계 : 스캐너 문자열 읽기
당신이 데이터를 읽을 수 있지만 System.in.read를 사용하지만, 매우 쉽게하는
라인으로 라인을 읽을 수있는 스캐너를 사용
package stream;
import java.util.Scanner;
public class TestStream {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(true){
String line = s.nextLine();
System.out.println(line);
}
}
}
3 단계 :. 스캐너는 콘솔에서 정수를 읽고
콘솔에서 정수를 읽어 스캐너를 사용하여
package stream;
import java.util.Scanner;
public class TestStream {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
System.out.println("第一个整数:"+a);
int b = s.nextInt();
System.out.println("第二个整数:"+b);
}
}
운동 : 자동으로 생성 카테고리
자동 속성 클래스 파일을 만들 수 있습니다.
콘솔을 통해 템플릿 파일에 따라, 클래스 이름, 속성 이름, 속성 유형을 얻을 파일이 자동으로 클래스를 생성하고 속성에 대한 세터와 게터를 제공합니다
public class @class@ {
public @type@ @property@;
public @class@() {
}
public void set@Uproperty@(@type@ @property@){
this.@property@ = @property@;
}
public @type@ get@Uproperty@(){
return this.@property@;
}
}
대답 :
package stream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class TestStream {
public static void main(String[] args) {
// 接受客户输入
Scanner s = new Scanner(System.in);
System.out.println("请输入类的名称:");
String className = s.nextLine();
System.out.println("请输入属性的类型:");
String type = s.nextLine();
System.out.println("请输入属性的名称:");
String property = s.nextLine();
String Uproperty = toUpperFirstLetter(property);
// 读取模版文件
File modelFile = new File("E:\\project\\j2se\\src\\Model.txt");
String modelContent = null;
try (FileReader fr = new FileReader(modelFile)) {
char cs[] = new char[(int) modelFile.length()];
fr.read(cs);
modelContent = new String(cs);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//替换
String fileContent = modelContent.replaceAll("@class@", className);
fileContent = fileContent.replaceAll("@type@", type);
fileContent = fileContent.replaceAll("@property@", property);
fileContent = fileContent.replaceAll("@Uproperty@", Uproperty);
String fileName = className+".java";
//替换后的内容
System.out.println("替换后的内容:");
System.out.println(fileContent);
File file = new File("E:\\project\\j2se\\src",fileName);
try(FileWriter fw =new FileWriter(file);){
fw.write(fileContent);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件保存在:" + file.getAbsolutePath());
}
public static String toUpperFirstLetter(String str){
char upperCaseFirst =Character.toUpperCase(str.charAt(0));
String rest = str.substring(1);
return upperCaseFirst + rest;
}
}