Java的输⼊输出(含快速输⼊输出)⽬录
1.主类的名字
由于在蓝桥杯当中主类的名字必须是Main,因此也可以在平时的练习当中习惯性的写成Main。
例如下:
public static void main(String[] args){
}
2.输⼊输出
2.1输⼊
(1)使⽤Scanner进⾏输⼊
需要定义⼀个可以在控制台接收键盘输⼊数据的Scanner对象
Scanner sc=new Scanner(System.in);
然后使⽤这个Scanner对象配合sc.next…()⽅法接收数据:
不同类型的数据使⽤不同的⽅法。
int Int();
double Double();
long Long();
字符串需要使⽤:
<();
或者
<() 从缓冲区接收字符遇到空格后停⽌。
区别:
next():
1、⼀定要读取到有效字符后才可以结束输⼊。
2、对输⼊有效字符之前遇到的空⽩,next() ⽅法会⾃动将其去掉。
3、只有输⼊有效字符后才将其后⾯输⼊的空⽩作为分隔符或者结束符。
next() 不能得到带有空格的字符串。
nextLine():
1、以Enter为结束符,也就是说 nextLine()⽅法返回的是输⼊回车之前的所有字符。
2、可以获得空⽩。
注意:
如果在nextLine(),之前有其他的输⼊的话(不包含nextLine(),也就是说2个nextLine()不会出现这个问
题),nextLine()会⽆法输⼊,原因是:nextLine()会读取上⼀个输⼊的回车,解决⽅法是:加多⼀个nextLine()来读取上⼀次的回车即可;有点类似c++的getchar()来读取上⼀个的回车。
(2)hasNext()的⽅法
sc.hasNext⽤法:
sc.hasNext()的返回值是bool值,作⽤是当在缓冲区内扫描到字符时,会返回true, 否则会发⽣阻塞,等待数据输⼊。
实现多组输⼊可以使⽤while(sc.hasNext()),相当于C++当中的while(cin>>)
按CTRL+Z结束
⽤例:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a,b,c;
while(sc.hasNext()){
Int();
Int();
Int();
System.out.println("输出:"+a+" "+b+" "+c);
}
sc.close();
System.out.println("结束输⼊");
}
运⾏结果:
针对不同的类型⽅法也不同:
in.hasNext() // 判断缓存区中还有没有数据,有返回true, 否则等待输⼊。 in.hasNextInt() //
判断输⼊的是不是int型的数据,是的话返回true 否则继续扫描缓冲区,或者等待输⼊。 in.hasNextDouble() //
判断输⼊的是不是double型的数据,是的话返回true 否则继续扫描缓冲区,或者等待输⼊。
2.2输出
Java常⽤的⼏种输出函数:
System.out.printf(); //和C/C++中的printf⼀样。可使⽤格式控制符进⾏格式化输出。
// 例如:输出⼀个int类型变量  System.out.printf("%d",a);
System.out.print() //不能使⽤格式控制符进⾏格式化输出,仅输出变量
System.out.println() //不能使⽤格式控制符进⾏格式化输出,仅输出变量,但会⾃动输出⼀个换⾏。
2.快速输⼊输出
使⽤BufferedReader和BufferedWriter实现快速输⼊输出
BufferedReader
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
⽅法主要使⽤read() 和 readLine()
String s = in.read() // 读⼊⼀个字符可读⼊空格回车但不抛弃回车
String s1 = in.readLine(); // 读⼊⼀⾏可读⼊空格可读⼊回车但会将回车抛弃
string s2[] = in.readLine().Split(" "); // 使⽤Split通过空格分割读⼊的⼀⾏字符串,存在s2中
需要注意的是 在windows中按⼀下回车键 ⼀共有两个字符 “\n\r”
⽽read()只能读取⼀个字符所以如要要⽤read来达到吸收回车的⽬的,需要⽤两个read();
如果⽤readLine()的话会将"\n\r"全部吸收 , 所以只需要⼀个readLine()来吸收回车。
BufferedWriter
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
主要使⽤ BufferedWriter类中的 write() 类进⾏输出。 当数据量⼤的时候⼀定要使⽤这个类进⾏输出,谨记!
需要注意的是 write() 不能直接输出int类型, 因为write(int a) 会输出其对应的ASCii码的字符 ,⽐如输出 65 会显⽰ A
int a = 65;
char b = '2';
String c = "3";
out.write(a);
out.write("\n");
out.write(b);
out.write("\n");
out.write(c);
out.write("\n");
out.flush();
输出:
A
2
3
主要使⽤以下⼏种⽅法:
int a = 65;
out.write(a + "\n");
out.String(a));
out.flush();
应⽤实例:
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));    public static void main(String[] args) throws IOException{
//测试writr 不能直接输出int类型
int a = 65;
out.write(a);
out.write("\n");
out.write(a + "\n");  // 使⽤ + 号拼接个字符串使参数整体为⼀个字符串
out.String(a)); // 输出a的字符串形式
out.write("\n");
//测试 read() 和  readLine();
int b = in.read();  // read()只读取⼀个字符
int c = in.read();  // 吸收 \n
int x = in.read();  // 吸收 \r
// String e = in.readLine();
String d = in.readLine();
out.write("\n");
out.write(b + "\n");
out.write(c + "\n");
out.write(x + "\n");
out.write(d + "\n");
/
/out.write(e);
out.flush();nextint()方法
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。