Posted on 2009-11-11 13:45
王之昊 阅读(263)
评论(0) 编辑 收藏 引用
由于我们接触的主要是文本文件。所以用BufferedReader 或 Scanner 处理读入比较方便。
标准输入输出:
import java.util.*;
public class Main
{
public static void main(String args[])throws Exception{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
System.out.println(cin.next());
}
}
import java.io.*;
import java.util.*;
public class Main {
private static BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;
public static void main(String[] argv) throws Exception {
while(havenext())
System.out.println(next());
}
private static int nextInt() throws Exception {
return Integer.parseInt(next());
}
private static double nextDouble() throws Exception {
return Double.parseDouble(next());
}
private static boolean havenext() throws IOException{
String L;
while ( (st == null || !st.hasMoreTokens() ) && ( L = in.readLine() ) != null )
st = new StringTokenizer(L);
return !(st == null || !st.hasMoreTokens());
}
private static String next() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
}
1 import java.io.*;
2 public class Main {
3
4 public static void P(Object t){System.out.println(t);}
5
6 public static void main(String av[]) throws Exception {
7 StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
8 while(st.nextToken() != st.TT_EOF){
9 int n = (int)st.nval;
10 P(n);
11
12 st.nextToken();
13 double m = st.nval;
14 P(m);
15
16 st.nextToken();
17 String s = st.sval;
18 P(s);
19 }
20
21 }
22 }
文件输入输出:
import java.io.*;
import java.util.*;
//要求project里有in文件
public class Main {
public static void main(String[] argv) throws Exception {
Scanner cin = new Scanner(new FileInputStream("in"));
PrintWriter MyWriter = new PrintWriter(new FileOutputStream("out"));
while(cin.hasNext()){
MyWriter.println(cin.next());
}
MyWriter.flush();
MyWriter.close();
}
}
import java.io.*;
import java.util.*;
//要求project里有in文件
public class Main {
private static BufferedReader in;
private static StringTokenizer st;
private static PrintWriter MyWriter;
public static void main(String[] argv) throws Exception {
in = new BufferedReader(new FileReader("in"));
MyWriter = new PrintWriter(new FileOutputStream("out"));
while(havenext())
MyWriter.println(next());
MyWriter.flush();
MyWriter.close();
}
private static int nextInt() throws Exception {
return Integer.parseInt(next());
}
private static double nextDouble() throws Exception {
return Double.parseDouble(next());
}
private static boolean havenext() throws IOException{
String L;
while ( (st == null || !st.hasMoreTokens() ) && ( L = in.readLine() ) != null )
st = new StringTokenizer(L);
return !(st == null || !st.hasMoreTokens());
}
private static String next() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
}