how2j.cn


工具版本兼容问题
System.out 是常用的在控制台输出数据的
System.in 可以从控制台输入数据


本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频



4分36秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器



步骤 1 : System.in   
步骤 2 : Scanner读取字符串   
步骤 3 : Scanner从控制台读取整数   
步骤 4 : 练习-自动创建类   
步骤 5 : 答案-自动创建类   

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 :

Scanner读取字符串

edit
使用System.in.read虽然可以读取数据,但是很不方便
使用Scanner就可以逐行读取了
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); } } }
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 :

Scanner从控制台读取整数

edit
使用Scanner从控制台读取整数
Scanner从控制台读取整数
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); } }
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);
	}
}
步骤 4 :

练习-自动创建类

edit  姿势不对,事倍功半! 点击查看做练习的正确姿势
自动创建有一个属性的类文件。
通过控制台,获取类名,属性名称,属性类型,根据一个模板文件,自动创建这个类文件,并且为属性提供setter和getter
练习-自动创建类
public class @class@ { public @type@ @property@; public @class@() { } public void set@Uproperty@(@type@ @property@){ this.@property@ = @property@; } public @type@ get@Uproperty@(){ return this.@property@; } }
public class @class@ {
    public @type@ @property@;
    public @class@() {
    }
    public void set@Uproperty@(@type@  @property@){
        this.@property@ = @property@;
    }
     
    public @type@  get@Uproperty@(){
        return this.@property@;
    }
}
步骤 5 :

答案-自动创建类

edit
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
查看本答案会花费4个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
查看本答案会花费4个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
账号未激活 账号未激活,功能受限。 请点击激活
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频

5分27秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器


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


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2022-09-15 自动创建类
ljbeiji




虽然代码是一行,但是在编辑器里一键重排就能恢复格式了
加载中
//所有待替换的实体名称
String className;
String type;
String property;
String uProperty;
File template = new File("C:\\Users\\ljbeiji\\Desktop\\Test Folder\\xyz\\templet.txt");
try (FileWriter fw = new FileWriter(template);
     PrintWriter pw = new PrintWriter(fw)) {
    if (!template.exists() || template.length() == 0) {
        template.createNewFile();
        pw.println("public class @class@ {");
        pw.println("    public @type@ @property@;");
        pw.println("    public @class@() {");
        pw.println("    }");
        pw.println("    public void set@Uproperty@(@type@ @property@){");
        pw.println("        this.@property@ = @property@;");
        pw.println("    }");
        pw.println("    public @type@ get@Uproperty@(){");
        pw.println("        return this.@property@;");
        pw.println("    }");
        pw.println("}");
    }
} catch (IOException e) {
    e.printStackTrace();
}
Scanner scan = new Scanner(System.in);
//控制输入,防止为空
System.out.println("请输入类的名称");
while (true) {
    className = scan.nextLine();
    if (!className.isEmpty() && className.charAt(0) < 91 && className.charAt(0) > 64)
        break;
    else
        System.out.println("类名格式不正确");
}
System.out.println("请输入属性的类型");
while (true) {
    type = scan.nextLine();
    if (!type.isEmpty() && Character.isLetter(type.charAt(0)))
        break;
    else
        System.out.println("属性类型格式不正确");
}
System.out.println("请输入属性的名称");
while (true) {
    property = scan.nextLine();
    if (!property.isEmpty() && Character.isLetter(property.charAt(0)))
        break;
    else
        System.out.println("属性名称格式不正确");
}
//属性名称首字母大写
uProperty = property.substring(0, 1).toUpperCase() + property.substring(1);
File f = new File(template.getParent() + "\\" + className + ".java");
try (FileReader fr = new FileReader(template);
     BufferedReader br = new BufferedReader(fr);
     FileWriter fw = new FileWriter(f);
     PrintWriter pw = new PrintWriter(fw);) {
    f.createNewFile();
    //最终结果存到line中
    String line = "";
    //临时存储从模板文件读出的每一行代码
    String temp;
    //将代码拼接到line中
    while ((temp = br.readLine()) != null)
        line += temp;
    //将line中的所有占位符替换为真正的名称
    line = line.replaceAll("@class@", className);
    line = line.replaceAll("@type@", type);
    line = line.replaceAll("@property@", property);
    line = line.replaceAll("@Uproperty@", uProperty);
    //将替换后的代码写到新文件中
    pw.println(line);
} catch (IOException e) {
    e.printStackTrace();
}

							


1 个答案

xiaoba_java
答案时间:2023-10-20
一步到位



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2022-05-25 答案
大D




答案
public static void getclass() throws IOException {
        File f=new File("d:/xyz/template.txt");
        File f2=new File("d:/xyz/dog.java");
        Scanner sc=new Scanner(System.in);
        System.out.println("输入类名称: ");
        String className = sc.nextLine();
        System.out.println("输入属性类型: ");
        String fieldType = sc.nextLine();
        System.out.println("输入属性name: ");
        String fileldName=sc.nextLine();
        BufferedReader bf=new BufferedReader(new FileReader(f));
        String name =className.substring(0,1).toUpperCase()+className.substring(1);
        PrintWriter pw =new PrintWriter(new FileWriter(f2));
        String s="";
        while ((s=bf.readLine())!=null){
             s=s.replace("@class@",name);
            s=s.replace("@type@",fieldType);
            s=s.replace("@property@",fileldName);
            s=s.replace("@Uproperty@",name);
            pw.print(s);
            pw.flush();
        }

    }

							


2 个答案

xiaoba_java
答案时间:2023-10-20
我的答案(方法+main方法)

cainiao小Y
答案时间:2022-12-22



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2021-11-30 练习-自动创建类
2021-11-27 自动创建类
2021-08-17 自动创建类-我的答案


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 34 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 中级-I/O-System.in 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图