java IO(四):键盘录入

时间:2023-03-09 15:20:06
java IO(四):键盘录入

*/

.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}

.hljs-comment,
.hljs-template_comment,
.diff .hljs-header,
.hljs-javadoc {
color: #998;
font-style: italic;
}

.hljs-keyword,
.css .rule .hljs-keyword,
.hljs-winutils,
.javascript .hljs-title,
.nginx .hljs-title,
.hljs-subst,
.hljs-request,
.hljs-status {
color: #333;
font-weight: bold;
}

.hljs-number,
.hljs-hexcolor,
.ruby .hljs-constant {
color: #099;
}

.hljs-string,
.hljs-tag .hljs-value,
.hljs-phpdoc,
.tex .hljs-formula {
color: #d14;
}

.hljs-title,
.hljs-id,
.coffeescript .hljs-params,
.scss .hljs-preprocessor {
color: #900;
font-weight: bold;
}

.javascript .hljs-title,
.lisp .hljs-title,
.clojure .hljs-title,
.hljs-subst {
font-weight: normal;
}

.hljs-class .hljs-title,
.haskell .hljs-type,
.vhdl .hljs-literal,
.tex .hljs-command {
color: #458;
font-weight: bold;
}

.hljs-tag,
.hljs-tag .hljs-title,
.hljs-rules .hljs-property,
.django .hljs-tag .hljs-keyword {
color: #000080;
font-weight: normal;
}

.hljs-attribute,
.hljs-variable,
.lisp .hljs-body {
color: #008080;
}

.hljs-regexp {
color: #009926;
}

.hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.lisp .hljs-keyword,
.tex .hljs-special,
.hljs-prompt {
color: #990073;
}

.hljs-built_in,
.lisp .hljs-title,
.clojure .hljs-built_in {
color: #0086b3;
}

.hljs-preprocessor,
.hljs-pragma,
.hljs-pi,
.hljs-doctype,
.hljs-shebang,
.hljs-cdata {
color: #999;
font-weight: bold;
}

.hljs-deletion {
background: #fdd;
}

.hljs-addition {
background: #dfd;
}

.diff .hljs-change {
background: #0086b3;
}

.hljs-chunk {
color: #aaa;
}

#container {
padding: 15px;
}
pre {
border: 1px solid #ccc;
border-radius: 4px;
display: block;
background-color: #f8f8f8;
}
pre code {
white-space: pre-wrap;
}
.hljs,
code {
font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;
}
:not(pre) > code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
background-color: #f9f2f4;
white-space: nowrap;
border-radius: 4px;
}
-->

要读取键盘输入的数据,需要使用输入流,可以是字节输入流,也可以是字节输入流转换后的字符输入流。

关于键盘输入,有几点注意的是:
(1).键盘输入流为System.in,其返回的是InputStream类型,即字节流。
(2).字节流读取键盘的输入时,需要考虑回车符(\r:13)、换行符(\n:10)。
(3).按行读取键盘输入时,需要指定输入终止符,否则输入将被当作here String文本,而非document。
(4).System.in字节流默认一直处于打开状态,可不用对其close(),但如果close()了,后续将不能使用in流。

例如,以字节输入流读取输入,此时只能输入ascii码中的符号。如果需要读取中文字符,使用字节流则需要加很多额外的代码来保证中文字符的字节不会被切分读取。

import java.io.*;

public class KeyB {
public static void main(String[] args) throws IOException {
InputStream in = System.in;
int ch = in.read();
System.out.println(ch);
int ch1 = in.read();
System.out.println(ch1);
int ch2 = in.read();
System.out.println(ch2);
int ch3 = in.read();
System.out.println(ch3);
int ch4 = in.read();
System.out.println(ch4);
}
}

以上示例中,将读取键盘上输入的5个字节,例如输入"ab"回车后,将输出"ab\r\n"的ascii码(97-98-13-10),并继续等待输入下一个字节才退出。

要想一次性读取一行,可以将它们读取到字节数组中,并以回车符、换行符判断一行读取完毕,再将字节数组转换为字符串输出。虽然用字节流能够完成这样的操作,但显然,如果使用字符流,则更轻松。

import java.io.*;

public class KeyB {
public static void main(String[] args) throws IOException { //InputStream in = System.in //键盘输入字节流
//InputStreamReader isr = new InputStreamReader(in); //桥梁,将字节流转换为字符流
//BufferedReader bufr = new BufferedReader(isr); //以BufferReader的readLine()读取行
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); String line = null;
//line = bufr.readLine();System.out.println(line); //输入一行立即退出
while((line = bufr.readLine())!=null) { //循环输入多行,直到输入终止符时才退出
if (line.equals("quit")) { //定义键盘输入终止符quit
break;
}
System.out.println(line);
}
}
}

注意,BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));几乎是涉及到键盘录入时的一条固定代码,属于可以直接背下来的。

以下是一个示例,需求是将键盘输入的结果保存到文件d:\myjava\a.txt中。

import java.io.*;

public class Key2File {
public static void main(String[] args) throws IOException {
//1.源:键盘输入,目标:文件
//2.输入中可能包含中文字符,所以采用字符流,输出也采用字符流
//3.需要使用Buffered流来提高效率并使用其提供的行操作方法
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new FileWriter("d:/myjava/a.txt")); String line = null;
while((line=bufr.readLine())!=null) {
if(line.equals("quit")) {
break;
}
bufw.write(line);
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}

注:若您觉得这篇文章还不错请点击右下角推荐,您的支持能激发作者更大的写作热情,非常感谢!