kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

Java I/O

Java 最简单输入就是用 Scanner 类 但是很慢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int a,b;
		while(cin.hasNext())
		{
			a=cin.nextInt(); b=cin.nextInt();
			System.out.println(a+b);
		}
	}
}

输入用 StreamTokenizer ,输出用 PrintWriter 就 很快

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.*;public class Main
{
	public static void main(String[] args) throws IOException
	{
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		int a, b;
		while(in.nextToken() != StreamTokenizer.TT_EOF)
		{
			a = (int)in.nval;
			in.nextToken();
			b = (int)in.nval;
			out.println(a + b);
		}
		out.flush();
	}
}

但是 StreamTokenizer 有所局限, 用 StringTokenizer 能很好解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
import java.io.*;
import java.math.*;public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int a,b;
		BigInteger c,d;
		String line;
		while((line=in.readLine())!=null)
		{
			st=new StringTokenizer(line);
			a=Integer.parseInt(st.nextToken());
			b=Integer.parseInt(st.nextToken());
			System.out.println(a+b);
		   
			line=in.readLine();
			st=new StringTokenizer(line);
			c=new BigInteger(st.nextToken().trim());
			d=new BigInteger(st.nextToken().trim());
			System.out.println(c.add(d));
		}
	}
}

Java 还可以 直接用 BufferedReader 类来输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
import java.io.*;
import java.math.*;public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
		int a,b,rr,t;
		while(true)
		{
			while((rr=read.read())==10 || rr==13 || rr==32);
			if(rr<0)break;
			if(rr=='-'){ t=1; a=0; } else { a=rr-48; t=0; }
			while((rr=read.read())!=32 && rr!=13 && rr!=10)a=a*10+rr-48;
			if(t==1)a=-a;
			while((rr=read.read())==10 || rr==13 || rr==32);
			if(rr=='-'){ t=1; b=0; } else { b=rr-48; t=0; }
			while((rr=read.read())!=10 && rr!=13 && rr!=10)b=b*10+rr-48;
			if(t==1)b=-b;
			System.out.println(a + b);
		}
	}
}

Java 文件输入 输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
import java.io.*;public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(new FileInputStream("in.txt"));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream("out.txt")));
		int a,b;
		while(cin.hasNext())
		{
			a=cin.nextInt(); b=cin.nextInt();
			out.println(a + b);
		}
		out.flush();
	}
} 

Java 向文件中加入数据

1
2
//PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream("out.txt")), false);
PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream("out.txt", true)));

language, java

« Java 基本 Java 常用数据结构 »