zoukankan      html  css  js  c++  java
  • Java中的快速输入输出

     1 /*
     2 使用BufferedReader和BufferedWriter实现快速输入输出
     3 
     4 BufferedReader 和 BufferedWriter 都在 java.io.*包内。
     5 
     6 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     7 主要使用read() 和 readLine()方法
     8 
     9 int read()                                            读取单个字符。
    10 String readLine()                                读取一个文本行。
    11 
    12 需要注意的是 在windows中按一下回车键 一共有两个字符 "
    
    "  而read()只能读取一个字符所以如要要用read来达到吸收回车的目的,需要用两个read();  如果用readLine()的话会将"
    
    "全部吸收 , 所以只需要一个readLine()来吸收回车。
    13 
    14 
    15 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    16 
    17 主要使用 BufferedWriter类中的 write() 类进行输出
    18 
    19 需要注意的是 write() 不能直接输出int类型, 因为write(int a)  会输出其对应的ASCii码的字符。
    20 
    21 所以当需要输出一个int类型的变量时, 可以用Integer.toString(int a)方法 将其变为字符串形式输出。
    22 或者使用 + 拼接一个字符串,这样 参数整体就是一个字符串了,比如加一个换行符。
    23 
    24 
    25 
    26 */
    27 
    28 //实例
    29 
    30 import java.io.*;
    31 
    32 import java.util.*;
    33 
    34  
    35 
    36  
    37 
    38 public class Main{
    39 
    40     static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    41 
    42     static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    43 
    44     public static void main(String[] args) throws IOException{
    45 
    46         //测试writr 不能直接输出int类型
    47 
    48         int a = 65;
    49 
    50         out.write(a);
    51 
    52         out.write("
    ");
    53 
    54         out.write(a + "
    ");  // 使用 + 号拼接个字符串 使参数整体为一个字符串 
    55 
    56         out.write(Integer.toString(a)); // 输出a的字符串形式
    57 
    58         out.write("
    ");
    59 
    60        
    61 
    62         //测试 read() 和  readLine();
    63 
    64         int b = in.read();   // read()只读取一个字符
    65 
    66         int c = in.read();   // 吸收 
    
    67 
    68         int x = in.read();   // 吸收 
    69 
    70        // String e = in.readLine();
    71 
    72         String d = in.readLine();
    73 
    74         out.write("
    ");
    75 
    76         out.write(b + "
    ");
    77 
    78         out.write(c + "
    ");
    79 
    80         out.write(x + "
    ");
    81 
    82         out.write(d + "
    ");
    83 
    84         //out.write(e);
    85 
    86         out.flush();
    87 
    88     }
    89 
    90 }

     

  • 相关阅读:
    eclipse新建工作空间后的常用设置
    Maven将代码及依赖打成一个Jar包的方式
    MemCache详细解读(转)
    memcached单机或热备的安装部署
    memcache的基本操作
    Java中int和String类型之间转换
    Linux中普通用户配置sudo权限(带密或免密)
    Java字符串中常用字符占用字节数
    java各种数据类型的数组元素的默认值
    validator
  • 原文地址:https://www.cnblogs.com/de-ming/p/13462713.html
Copyright © 2011-2022 走看看