test.txtという名のファイルができ、内容は "File can't write." の一行である。このファイルには改行コードは含まれてない (od -c コマンドで確認できる)。
再度実行しても、test.txtの内容は変わらない。 FileOutput1.java を実行するたびに、test.txtの内容を空にして、 "File can't write."と書き込んでいるためである。
import java.io.*;
public class FracFileWrite {
// 再帰版 frac
/* static long frac(int n) {
if (n == 0) return 1;
return n*frac(n-1);
} */
// 繰り返し版 frac
static long frac(int n) {
int r = 1;
for (int i = 1; i <= n; i++) {
r *= i;
}
return r;
}
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("frac.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println("8! = " + frac(8));
pw.close();
} catch (IOException e) {
System.err.println("File can't write.");
}
}
}
import java.io.*;
public class AverageScore {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
int sum = 0;
int n = 0;
String s;
while((s = br.readLine()) != null) {
sum += Integer.parseInt(s);
n++;
}
br.close();
System.out.println("Average score is " + (double)sum/n);
} catch (IOException e) {
System.err.println("File can't read.");
}
}
}
import java.io.*;
public class ScoreSorting {
static void bubble_sort(int[] d, int n) {
for (int i = n-1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (d[j] > d[j+1]) {
int t = d[j];
d[j] = d[j+1];
d[j+1] = t;
}
}
}
}
public static void main(String[] args) {
try {
final int data_max = 100;
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
int[] scores = new int[data_max];
int n = 0;
String s;
while((s = br.readLine()) != null) {
if (n > data_max) {
System.err.println("Number of Data is too much!");
System.exit(-1);
}
scores[n] = Integer.parseInt(s);
n++;
}
br.close();
bubble_sort(scores, n);
for (int i = 0; i < n; i++) {
System.out.println(scores[i]);
}
} catch (IOException e) {
System.err.println("File can't read.");
}
}
}
※ このプログラム例では、点数を格納する配列の大きさを100と 固定にしている。このため、data.txtのデータ数が100を超えた場合 エラー処理をしている。
import java.net.*;
import java.io.*;
public class DaytimeClient2 {
public static void main(String[] args) {
try {
String remotehost = null;
if (args.length != 1) {
System.err.println("Usage: java DaytimeClient hostname");
System.exit(-1);
} else {
remotehost = args[0];
}
Socket sock = new Socket(remotehost, 13);
InputStream is = sock.getInputStream();
int c;
while((c=is.read()) != -1) {
System.out.print((char)c);
}
sock.close();
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
}
import java.net.*;
import java.io.*;
public class DaytimeClient3 {
public static void main(String[] args) {
try {
String remotehost = null;
if (args.length != 1) {
System.err.println("Usage: java DaytimeClient hostname");
System.exit(-1);
} else {
remotehost = args[0];
}
Socket sock = new Socket(remotehost, 13);
BufferedReader br = new BufferedReader(new InputStreamReader(
sock.getInputStream()));
String s;
while((s=br.readLine()) != null) {
System.out.println(s);
}
sock.close();
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
}
import java.net.*;
import java.io.*;
public class DaytimeClient4 {
public static void main(String[] args) {
try {
String remotehost = null;
if (args.length != 1) {
System.err.println("Usage: java DaytimeClient hostname");
System.exit(-1);
} else {
remotehost = args[0];
}
Socket sock = new Socket(remotehost, 13);
BufferedReader br = new BufferedReader(new InputStreamReader(
sock.getInputStream()));
System.out.println("Remote Hostname is " +
sock.getInetAddress().getHostName());
System.out.println("Remote Port is " + sock.getPort());
System.out.println("Local Hostname is " +
sock.getLocalAddress().getHostName());
System.out.println("Local Port is " + sock.getLocalPort());
String s;
while((s=br.readLine()) != null) {
System.out.println(s);
}
sock.close();
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
}
※ System.out.println(... + sock.getInetAddress().getHostName()); とあるのは、以下の省略形である。
InetAddress a = sock.getInetAddress();
System.out.println(... + a.getHostName());
このプログラムを実行すると、実行するたびにローカル側のポート番号が 変わっていく(増えていく)であろう。これは、ソケットを生成する時に 自動的に割り当てられたポート番号である。
実行すると、途中でハングアップしたようになる。 これは、サーバに送信すべきデータがバッファに溜ったままになっているためで ある。 クラインアント側はサーバからの返答を待ち、サーバ側もクライアントから の要求を待っているという状態である。
import java.net.*;
import java.io.*;
public class EchoClient2 {
public static void main(String[] args) {
try {
String remotehost = "localhost";
Socket sock = new Socket(remotehost, 7);
PrintWriter out = new PrintWriter(sock.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Connected.");
while(true) {
String s = kin.readLine();
if (s.equals("")) break;
out.print(s + "\r\n");
out.flush();
s = in.readLine();
System.out.println(s);
}
sock.close();
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
}
※ 空行かどうかの判定は、s = ""ではなく、 s.equals("")であることに注意せよ。