long start = System.currentTimeMillis(); for(int i=1;i<=1000000;i++){} long end = System.currentTimeMillis(); System.out.println("time="+(end-start)+"ms");
publicstaticvoidmain(String[] args)throws Exception { /* 下面这是IO流包装,可以看不懂,直接套用就好*/ StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken();/*nextToken()方法相当于让光标指向下一个位置*/ int n = (int) in.nval;//获取整数; out.println(n); out.flush();/*刷新输出缓冲区并输出*/ }
2.2.2 获取字符串(sval)
1 2 3 4 5 6 7 8 9
publicstaticvoidmain(String[] args)throws Exception { /* 下面这是IO流包装,可以看不懂,直接套用就好*/ StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken();/*nextToken()方法相当于让光标指向下一个位置*/ String str = in.sval;/*注意:sval是获取不带空格的字符串,含有空格的字符串无法全部读取,只会获取空格之前的。并且只能用来获取含字母和数字、中英文句号的字符串。具体情况需要自行尝试,至少写者在测试时是这样的。因为网上有些说的是只能是字母串,不能含数字或其他,否则会返回null。*/ out.println(str); out.flush();/*刷新输出缓冲区并输出*/ }
2.2.3 处理到文件尾(多组输入)
判断是否到达文件结尾,如C语言的while(scanf(“%d”,&n)!=EOF)
1
while (in.nextToken() != StreamTokenizer.TT_EOF) //StreamTokenizer.TT_EOF这个是个参数,就是相当于EOF了。
示例:
1 2 3 4 5 6 7 8 9 10 11 12
publicstaticvoidmain(String[] args)throws Exception { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF){ int n = (int) in.nval; in.nextToken(); int m = (int) in.nval; out.println(n+" "+m); out.flush(); } }
publicstaticvoidmain(String[] args)throws Exception { //高性能输入,用于数据量大,要求输入输出速度快 BufferedReader in =new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out);
///读取一行内容,返回字符串 int i=Integer.parseInt(in.readLine()); //int型输入 out.println(i); out.flush();
long l=Long.parseLong(in.readLine()); //long型输入 out.println(l); out.flush();