java 获取回车字符_java回车键的字符

public class WriteToProcess {

public static void main(String[] args) {

PrintStream ps = null;

try {

// 运行java ReadStandard命令,返回运行该命令的子进程

System.out.println("start");

Process p = Runtime.getRuntime().exec("java ReadStandard");

// 以p进程的输出流创建PrintStream对象

// 这个输出流对本程序是输出流,对p进程则是输入流)

ps = new PrintStream(p.getOutputStream());

// 向ReadStandard程序写入内容,这些内容将被ReadStandard读取

ps.println("普通字符串");

ps.println(new WriteToProcess());

InputStream error = p.getErrorStream();

System.out.println(convertStreamToString(error));

System.out.println("start");

} catch (IOException ex) {

ex.printStackTrace();

} finally {

if (ps != null)

ps.close();

}

}

public static String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

StringBuilder sb = new StringBuilder();

String line = null;

try {

while ((line = reader.readLine()) != null) {

sb.append(line + "/n");

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return sb.toString();

}

}

// 定义一个ReadStandard类,该类可以接受标准输入,

// 并将标准输入写入out.txt文件。

class ReadStandard {

public static void main(String[] args) throws Exception {

System.out.println("ReadStandard.");

// 使用System.in创建Scanner对象,用于获取标准输入

Scanner sc = new Scanner(System.in);

PrintStream ps = new PrintStream(new FileOutputStream("out.txt"));

// 增加下面一行将只把回车作为分隔符

sc.useDelimiter("\n");

// 判断是否还有下一个输入项

while (sc.hasNext()) {

// 输出输入项

ps.println("键盘输入的内容是:" + sc.next());

}

ps.close();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值