2009年6月15日月曜日

C++用語

ポリフォーズム
 ポリフォーズムとは、一つの汎用インターフェイスを使用して、複数の異なる機能にアクセスするためのしくみ
カプセル化
 プログラムコードとそれに関するデータの間のつながりを保護する。
 カプセル化されたルーチンへのアクセスは厳密に制御されており、これにより不要な干渉を防ぐことができる
継承
 一つのオブジェクトが他のオブジェクトの特性を受け継ぐプロセスのこと
 継承を使うと階層的分類システムを実現することができる

2009年6月10日水曜日

AWT 正規表現

正規表現の主な記号


^ 行の先頭を示す
$ 行の末尾を示す
. 任意の一文字を示す(改行文字は除く)
[] []内に書かれた文字のいずれか
[^] []内に書かれた文字以外のいずれか
[a-b] aからbまでの範囲のいずれか
* 直前の文字が0個以上連続している
+ 直前の文字が1個以上連続している
? 直前の文字が0個または1個
{a} 直前の文字がa個以上連続している
{a,b} 直前の文字がa個以上b個以下連続している
a|b aまたはb


例.

検索対象:大根は300円です。
検索文字:[o-9]+
置換文字:¥¥$0
置換結果:大根は¥300円です。



検索対象:This is java program.
検索文字:[a-zA-Z]+
置換文字:[$0]
置換結果:[This] [is] [java] [program].



検索対象:300,600,1000,2000,7000,11000,25000
検索文字:¥b[0-9]{4}¥b
置換文字:[$0]
置換結果:300,600,[1000],[2000],[7000],11000,25000



検索対象:私のメールアドレスはnagahashi@mac.comです。
検索文字:[0-9a-zA-Z.]+@[0-9a-zA-Z.]+
置換文字:[$0]
置換結果:私のメールアドレスは[nagahashi@mac.com]です。



検索対象:私のメールアドレスはnagahashi@mac.comです。nagahashi@yahoo.krもあります。
検索文字:[0-9a-zA-Z.]+@yahoo.(co.jp|com)
置換文字:[$0]
置換結果:私のメールアドレスは[nagahashi@mac.com]です。nagahashi@yahoo.krもあります。



検索対象:私のホームページはhttp://homepage.mac.com/nagahashi/です。
検索文字:http://[0-9a-zA-Z.]+/[0-9a-zA-Z./?=-_]*
置換文字:[$0]
置換結果:私のホームページは[http://homepage.mac.com/nagahashi/]です。

AWT テキスト操作

テキスト操作

Stringクラスのテキスト編集用メソッド
・文字の取得

char 変数 = String.charAt(int値);
指定したインデックス位置の文字を取得する


・指定したテキストが最初に登場する位置のインデックス番号を取得する

int 変数 = String.indexOf(String);


・テキストの長さを取得する

int 変数 = String.length();


・テキストの始まり/終わりが指定したテキストで始まり/終わりであるか調べる

boolern 値 = String.startsWith(String);
boolern 値 = String.endsWith(String);


・テキストの一部を取得

String = String.substring(int値,int値);
開始位置から終了位置までを取得する


・テキストの置換

String = String.replace(char値,char値);
第一引数にある文字を全て第二引数の文字に置換する
String = String.replaceAll(String,String);
第一引数のテキストを検索し第二引数のテキストに置換する


例.

public class TextApp extends Frame implements ActionListener{

private static final long serialVersionUID = 1L;

TextArea ta1;
TextField tf1,tf2;
Button b1;

public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
new TextApp();
}

public TextApp(){
this.setSize(300,200);
tf1 = new TextField(10);
tf2 = new TextField(10);
Panel p = new Panel();
p.add(tf1);
p.add(tf2);
this.add(p,BorderLayout.NORTH);
ta1 = new TextArea("12345",20,5,TextArea.SCROLLBARS_VERTICAL_ONLY);
this.add(p,BorderLayout.CENTER);
b1 = new Button("Click");
b1.addActionListener(this);
this.add(b1,BorderLayout.SOUTH);
this.setVisible(true);
}

public void actionPerformed(ActionEvent ev){
String str = ta1.getText();
str = str.replaceAll(tf1.getText(),tf2.getText());
ta1.setText(str);
}
}

注)ta1は表示されない

AWT 動的配列

動的配列
 一般的に複数のオブジェクトを管理する専用のクラスとして「ArrayList」が使用される

要素編集用のメソッド
・インスタンスの追加

ArrayList.add(Object);
引数がインスタンスのみの場合は最後に追加


ArrayList.add(int値,Object);
int値の位置にインスタンスを追加


・要素を削除

ArrayList.remove(int値);
指定したインデックス番号の要素を削除


ArrayList.removeRange(int値,int値);
指定したインデックス番号の範囲内の要素を削除


ArrayList.clear();
全要素を削除


・要素を取得

Object = ArrayList.get(int値);
引数指定したインデックス番号の要素を取得


・要素数の取得

int値 = ArrayList.size();
引数指定したインデックス番号の要素を取得


・全要素の取得

Object配列 = ArrayList.toArray();
インスタンスにある全要素を要素にもつObject配列を返す



例.

public class ArrayApp extends Frame{

ArrayList data;

public static void main(String[] args) {
new ArrayApp();
}

public ArrayApp(){
this.setSize(300,200);
data = new ArrayList();
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent ev){
clicked(ev);
}
});
}

public void clicked(MouseEvent ev){
Point p = ev.getPoint();
data.add(p);
this.repaint();
}

public void paint(Graphics g){
g.setColor(Color.red);
int n = data.size();
int[] x = new int[n];
int[] y = new int[n];
for(int i = 0 ; i < n ; i++){
Point p = (Point)data.get(i);
x[i] = p.x;
y[i] = p.y;
}
g.drawPolyline(x, y, n);
}
}

2009年6月9日火曜日

AWT 和暦

和暦
 ロケール情報(地域や言語などに関する情報)を管理するクラス「Locale」を使用する
 「Locale.setDefault」はデフォルトのロケール情報を引数に指定したものに設定する。
 和暦を扱うためには

Locale.setDefault(new Local("ja","JP","JP"));

 という形でLocaleを作成する
 以降、西暦などのAD/BC表記を示すG記号で元号を表記するようになる。
 例えば、「SimpleDateFormat」の引数に「GGGGyyyy年」という書き方をすると
 「GGGG」=「平成」、「yyyy」=「20」となる

例.
public class WarekiApp extends Frame{

public static void main(String[] args) {
new WarekiApp();
}

public WarekiApp(){
this.setSize(300,200);
Label l1 = new Label();
l1.setFont(new Font("Serif",Font.BOLD,18));
this.add(l1,BorderLayout.CENTER);
Locale.setDefault(new Locale("jp","JP","JP"));
Calendar c1 = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("GGGGyyyy年MM年dd日(E)");
l1.setText(df.format(c1.getTime()));
this.setVisible(true);
}
}

AWT 日付のフォーマット

パターン作成用に用意されているパターン文字

G:紀元(西暦)
y:年
M:月
w:年における週
W:月における週
D:年における日
d:月における日
F:月における曜日
E:曜日
a:午前/午後
H:一日における時(0~23)
k:一日における時(1~24)
K:午前/午後の時(0~11)
h:午前/午後の時(1~12)
m:分
s:秒
S:ミリ秒
z:タイムゾーン(一般的なタイムゾーン)
Z:タイムゾーン(RFC 822 タイムゾーン)


例.

"yyyy/M/d" ⇒ "2008/8/5"
"yy年MM月dd日" ⇒ "08年08月05日"


フォーマット済みテキストを得る
 引数にはCalendarのgetTimeを利用してDateインスタンスを渡すようにする

String = DateFormat.format(DATE);


例.

public class DateFormatApp extends Frame{

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
new DateFormatApp();
}

public DateFormatApp(){
this.setSize(300,200);
Label l1 = new Label();
l1.setFont(new Font("Serif",Font.BOLD,18));
this.add(l1,BorderLayout.CENTER);
Calendar c1 = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("MM月dd日(E)~Gyyyy年~");
l1.setText(df.format(c1.getTime()));
this.setVisible(true);
}
}

AWT 日付の演算

特定の要素の値を得る
 引数には取り出す要素を示すCalendarクラスのフィールドを指定する
 例えばCalendarから日を示す値を得たい場合は「get(Calendar.DATE)」とする

int 変数 = Calendar.get(フィールド);



特定の要素の値を変更する
 Calendarの特定要素の値を変更する
 引数には要素を示すCalendarクラスのフィールドと設定値を指定する方法と、年月日などの全て指定する方法がある
 例えば日の値を「10」に変更したい場合は「set(Calendar.DATE,10)」とし、年月日を「2001年12月24日」にしたい場合は「set(2001,12,24)」とする

Calendar.set = (フィールド,int値);
Calendar.set = (年,月,日);
Calendar.set = (年,月,日,時,分);
Calendar.set = (年,月,日,時,分,秒);


特定の要素の値を加算する
 特定の要素を加算する
 例えば「123日を加算する」には「add(Calendar.DATE,123)」とする

GregorianCalendar.add(フィールド,int値)


基準日時から経過時間を得る
 Javaの時間関係の基準となる1970年1月1日午前0時からの経過ミリ秒を得る

long 変数 = Calendar.getTimeInMillis();


例.2000年元旦からの経過日数と今日から1000日後の日付を計算しlabelに表示する

public class Calendar2App extends Frame{

public static void main(String[] args) {
new Calendar2App();
}

public Calendar2App(){
this.setSize(300,200);
Label l1 = new Label();
l1.setFont(new Font("Serif",Font.BOLD,14));
Label l2 = new Label();
l2.setFont(new Font("Serif",Font.BOLD,14));
Panel p1 = new Panel(new GridLayout(2,1));
p1.add(l1);
p1.add(l2);
this.add(p1,BorderLayout.CENTER);

//2000.1.1と今日のCalendarを用意する
Calendar c1 = new GregorianCalendar(2000,1,1);
Calendar c2 = new GregorianCalendar();
c2.set(c2.get(Calendar.YEAR),c2.get(Calendar.MONTH),c2.get(Calendar.DATE));

//差の計算
long num1 = c1.getTimeInMillis();
long num2 = c2.getTimeInMillis();
int n = (int)((num2-num1)/1000/60/60/24);

//今日から1000日後の計算
c1.add(Calendar.DATE,1000);
int y = c2.get(Calendar.YEAR);
int m = c2.get(Calendar.MONTH);
int d = c2.get(Calendar.DATE);
l1.setText("2000年元旦から今日までの日数:" + n);
l2.setText("今日から1000日後の日付:" + y + "/" + (m+1) + "/" + d);
this.setVisible(true);
}

}

2009年6月8日月曜日

AWT 日時の値

・Calendarクラス
 カレンダー(暦)のクラス
 このクラス自体が抽象クラスで、これを継承して様々な暦のクラスを用意して使う
 「GregorianCalender」グレゴリオ暦のクラスが用意されており、通常は西暦による日時はこれを使用する。

・グレゴリオ暦による日時を示す
 引数は日時を示す
 引数を省略した場合は現在の日時を示すインスタンスとなる

new GregorianCalendar();
new GregorianCalendar(年,月,日);
new GregorianCalendar(年,月,日,時,分);
new GregorianCalendar(年,月,日,時,分,秒);
年,月,日,時,分,秒:各日時の値を示すint値


・Dateクラス
 各国語の表記や時差などに対応する機能をもっていなかったりするので、あまり使用されない。
 日時は「GregorianCalender」グレゴリオ暦のクラスを通常は使用するが、Calenderの内部でDateが重要な働きをしているので使い方は覚えておくこと
 日時のテキスト表現を得る場合、CalendarからgetTimeでDateインスタンスを取得し、そのtoStringを使う

Date = Calendar.getTime();
String = Date.toString();



例.

public class CalenderApp extends Frame implements Runnable{

Label l1;

public static void main(String[] args) {
new CalenderApp();
}

public CalenderApp(){
this.setSize(300,200);
l1 = new Label();
l1.setFont(new Font("Serif",Font.BOLD,18));
this.add(l1,BorderLayout.CENTER);
new Thread(this).start();
this.setVisible(true);
}

public void run(){
while(true){
Calendar c = new GregorianCalendar();
l1.setText(c.getTime().toString());
try{
Thread.sleep(100);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}

2009年6月6日土曜日

AWT タイマー

・Timerクラス
 タイマーの機能を提供するクラス
 実行する処理をスケジューリングすることで指定の処理を定期的に呼び出す

・TimerTaskクラス
 Timerで実行する処理のタスク
 これを継承したクラスを新たに用意し、その中にrunメソッドとしてタイマーによる処理を用意する

①TimerTaskを継承したクラスを用意し、そこにタイマーで実行する処理をrunメソッドとして用意する

class クラス extends TimerTask{
public void run(){
タイマーで実行する処理
}
}

②タイマーを開始する場合は、TimerとTimerTask継承クラスのインスタンスをnewで作成する(Timer/TimerTaskのコンストラクタ)

Timer = new Timer();
TimerTask = new TimerTask;

③「schedule」メソッドを使ってTimerTask継承クラスのインスタンスをスケジューリングする(スケジューリングのメソッド)

Timer.schedule(TimerTask,遅延時間);
Timer.schedule(TimerTask,Date);
Timer.schedule(TimerTask,遅延時間,繰り返し間隔);
Timer.schedule(TimerTask,Date,繰り返し間隔);

遅延時間:スタートするまでの遅延時間(ミリ秒単位のlong型)
繰り返し間隔:呼び出す間隔(ミリ秒単位のlong型)
Date:開始する時刻を示すDateインスタンス

④指定されたスケジュールにTimerTask継承クラスのrunメソッドが実行される
 途中でタイマーを停止する場合はcancelメソッドを呼び出す

Timer.cancel();



例.

public class TimerApp extends Frame implements ActionListener{

Button b1;
Panel p1;
int num = 1;

public static void main(String[] args) {
new TimerApp();
}

public TimerApp(){
this.setSize(300,200);
p1 = new Panel();
this.add(p1,BorderLayout.CENTER);
b1 = new Button("Click");
b1.addActionListener(this);
this.add(b1,BorderLayout.SOUTH);
this.setVisible(true);
}

public void actionPerformed(ActionEvent ev){
p1.setLayout(new GridLayout(num++,1));
Random rnd = new Random();
TimeLabel tl1 = new TimeLabel();
tl1.end = rnd.nextInt(90)+10;
tl1.delay = rnd.nextInt(2500)+500;
p1.add(tl1);
p1.doLayout();
}
}

class TimeLabel extends Label{

int num = 0;
int end = 100;
int delay = 1000;
boolean kiss_of_death = false;

TimeLabel(){
this.setText("count: 0");
this.setFont(new Font("SansSerif",Font.BOLD,18));
this.setForeground(Color.BLUE);
Timer t = new Timer();
t.schedule(new TimeLabelTask(),0,delay);
}

class TimeLabelTask extends TimerTask{
public void run(){
setText("count: "+ num++);
if(num == end){
num = 0;
}
}
}
}

2009年6月5日金曜日

AWT マルチスレッドの一時停止/再開

・synchronized文
 オブジェクト同期の為のもの
 ブロック内の処理を行っている間、引数に指定したインスタンスに外部からアクセスできないようにする

synchronized(インスタンス){
実行する処理
}


・スレッドの一時停止
 notify/notifyAllが呼び出されるまでスレッドを待機させる。
 引数はタイムアウト時間で、タイムアウトすると自動的にスレッドが再開する。


Object.wait();
Object.wait(long値);


・スレッドの再開
 notify:オブジェクトのモニタで待機中のスレッドを1つ再開する。
 notifyAll:オブジェクトのモニタで待機中の全てのスレッドを再開する

Object.notify();
Object.notifyAll();


例.

public class MultiApp2 extends Frame implements ActionListener{

private static final long serialVersionUID = 1L;

Button b1;
Panel p1;
int num =1;

public static void main(String[] args) {
new MultiApp2();
}

public MultiApp2(){
this.setSize(300,200);
p1 = new Panel();
this.add(p1,BorderLayout.CENTER);
b1 = new Button("Click");
b1.addActionListener(this);
this.add(b1,BorderLayout.SOUTH);
this.setVisible(true);
}

public void actionPerformed(ActionEvent ev){
p1.setLayout(new GridLayout(num++,1));
Random rnd = new Random();
TimeLabel tl1 = new TimeLabel();
tl1.end = rnd.nextInt(90) + 10;
tl1.delay = rnd.nextInt(2500) + 500;
p1.add(tl1);
p1.doLayout();
}
}

class TimeLabel extends Label implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
int num = 0;
int end = 100;
int delay = 1000;
boolean kiss_of_death = false;
boolean pending = false; // 一時停止用フラグ

TimeLabel(){
this.setText("count: 0");
this.setFont(new Font("SansSerif",Font.BOLD,18));
this.setForeground(Color.blue);
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent ev){
setPending(!pending);
}
});
new Thread(this).start();
}

public void run(){
while(!kiss_of_death){
this.setText("count: " + num++);
if(num == end){
num=0;
}
try{
Thread.sleep(delay);
synchronized(this){
if(pending){
wait(); // 一時停止
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}

// 一時停止/再開を行うメソッド
public synchronized void setPending(boolean f){
pending = f;
if(pending){
setForeground(Color.red);
}
else{
setForeground(Color.blue);
notify();
}
}
}

AWT マルチスレッド

・threadクラス
 スレッド機能を提供するクラス。
 これをextendsして新たなクラスを定義し、その中にマルチスレッドで実行したい処理を用意する。

class クラス extends Thread{
public void run(){
マルチスレッドで実行する処理
}
}
変数 = new クラス()
変数.start();


・Runnableインターフェイス
 スレッド機能を提供するインターフェイス。
 これをimplementsしたクラスを用意し、その中にマルチスレッドで実行したい処理を用意する。

class クラス implements Runnable{
public void run(){
マルチスレッドで実行する処理
}
}
変数 = new Thread(this)
変数.start();


・「threadクラス」と「Runnableインターフェイス」の違い
 「threadクラス」はインスタンスの作成のみ
 「Runnableインターフェイス」は「new Thread」としてスレッドインスタンスを作成し、その引数にrunを含むインスタンスを指定する


例.

public class MultiAPP extends Frame implements ActionListener{

/**
*
*/
private static final long serialVersionUID = 1L;

Button b1;
Panel p1;
int num =1;

public static void main(String[] args) {
new MultiAPP();
}
public MultiAPP(){
this.setSize(300,200);
p1 = new Panel();
this.add(p1,BorderLayout.CENTER);
b1 = new Button("Click");
b1.addActionListener(this);
this.add(b1,BorderLayout.SOUTH);
this.setVisible(true);
}

public void actionPerformed(ActionEvent ev){
p1.setLayout(new GridLayout(num++,1));
Random rnd = new Random();
TimeLabel tl1 = new TimeLabel();
tl1.end = rnd.nextInt(90) + 10;
tl1.delay = rnd.nextInt(2500) + 500;
p1.add(tl1);
p1.doLayout();
}
}

class TimeLabel extends Label implements Runnable {

int num = 0;
int end = 100;
int delay = 1000;
boolean kiss_of_death = false;

TimeLabel(){
this.setText("count: 0");
this.setFont(new Font("SansSerif",Font.BOLD,18));
this.setForeground(Color.blue);
new Thread(this).start();
}

public void run(){
while(!kiss_of_death){
this.setText("count: " + num++);
if(num == end){
num=0;
}
try{
Thread.sleep(delay);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}


解説
・new Thread(this).start();
 startすることによりrunスレッドが新スレッドで実行される。

2009年6月3日水曜日

AWT オブジェクトのアクセス

オブジェクトの書き出し
 ①FileOutputStreamインスタンスの作成

FileOutputStream os = new FileOutputStream(ファイル名)

 ②FileOutputStreamを元にBufferedOutputStreamインスタンスの作成

BufferedOutputStream = new BufferedOutputStream(FileOutputStreamインスタンス);

 ③BufferedOutputStreamを元にXMLEncoderインスタンスの作成

XMLEncoder = new XMLEncoder(BufferedOutputStreamインスタンス);

 ④データを書き込む

xml.writeObject(TextAreaインスタンス);

 ⑤読み出しが終了したらcloseメソッドによりストリームを開放する

xml.close()


オブジェクトの読み込み
 ①FileInputStreamインスタンスの作成

FileInputStream is = new FileInputStream(ファイル名)

 ②FileInputStreamを元にBufferedInputStreamインスタンスの作成

BufferedInputStream = new BufferedInputStream(FileInputStreamインスタンス);

 ③BufferedInputStreamを元にXMLDecoderインスタンスの作成

XMLDecoder = new XMLDecoder(BufferedInputStreamインスタンス);

 ④データを読み込む

TextArea newTA = (TextArea)xml.readObject();

 ⑤読み出しが終了したらcloseメソッドによりストリームを開放する

xml.close()



例.

public class ObjectApp extends Frame implements ActionListener{

 Button b1,b2;
 TextArea ta;

 public static void main(String[] args) {
  new ObjectApp();
 }
 public ObjectApp(){
  this.setSize(300,200);
  ta = new TextArea();
  this.add(ta,BorderLayout.CENTER);
  b1 = new Button("Load");
  b1.addActionListener(this);
  b2 = new Button("Save");
  b2.addActionListener(this);
  Panel p = new Panel();
  p.add(b1);
  p.add(b2);
  this.add(p,BorderLayout.SOUTH);
  this.setVisible(true);
 }

 public void actionPerformed(ActionEvent ev){
  if(ev.getSource() == b1){
   this.load();
  }
  else if(ev.getSource() == b2){
   this.save();
  }
 }

 public void load(){
  FileInputStream is = null;
  BufferedInputStream bis = null;
  XMLDecoder xml = null;
  try{
   is = new FileInputStream("sample.txt");
   bis = new BufferedInputStream(is);
   xml = new XMLDecoder(bis);
   TextArea newTA = (TextArea)xml.readObject();
   this.remove(ta);
   ta = null;
   ta = newTA;
   this.add(ta,BorderLayout.CENTER);
   this.doLayout();
  } catch(Exception ex){
   ex.printStackTrace();
  } finally {
   try{
    xml.close();
   } catch(Exception ex2){
    ex2.printStackTrace();
   }
  }
 }

 public void save(){
  FileOutputStream os = null;
  BufferedOutputStream bos = null;
  XMLEncoder xml = null;
  try{
   os = new FileOutputStream("sample.txt");
   bos = new BufferedOutputStream(os);
   xml = new XMLEncoder(bos);
   xml.writeObject(ta);
   Random rnd = new Random();
   int r = rnd.nextInt(256);
   int g = rnd.nextInt(256);
   int b = rnd.nextInt(256);
   ta.setForeground(new Color(r,g,b));
  } catch(Exception ex){
   ex.printStackTrace();
  } finally {
   try{
    xml.close();
   } catch (Exception ex2){
    ex2.printStackTrace();
   }
  }
 }
}

2009年6月1日月曜日

HTML 基本

・HTMLタグのエスケープ
 htmlでは『&,<,>,","』をエスケープ処理を行う必要がある
  &⇒&amp;
  <⇒&lt;
  >⇒&gt;
  "⇒&quot;
  \⇒&quot;

・リンクを貼るには

<a href="アドレス">表示名称</a>


・シンタックスハイライタ(Syntax Highlighter)
 色付けしたコードを表示する
 使用可能な言語(下記例の「java」部を修正)
   c,c++,c#,css,delphi,html,java,js,
   pascal,php,python,ruby,sql,vb,xml


<pre name="code" class="java">



</pre>


・太字出力時

<b>



</b>

AWT バイナリファイルのアクセス

バイナリファイルの書き出し
 ①FileOutputStreamインスタンスの作成

FileOutputStream os = new FileOutputStream(ファイル名)

 ②FileOutputStreamを元にBufferedOutputStreamインスタンスの作成

BufferedOutputStream = new BufferedOutputStream(FileOutputStreamインスタンス);

 ③BufferedOutputStreamを元にDataOutputStreamインスタンスの作成

DataOutputStream = new DataOutputStream(BufferedOutputStreamインスタンス);

 ④データを書き込む

boolearn 変数 = DataOutputStream.writeBoolean();
char 変数 = DataOutputStream.writeChar();
byte 変数 = DataOutputStream.writeByte();
int 変数 = DataOutputStream.writeInt();  等

 ⑤読み出しが終了したらcloseメソッドによりストリームを開放する

DataOutputStream.close()


バイナリファイルの読み込み
 ①FileInputStreamインスタンスの作成

   FileInputStream is = new FileInputStream(ファイル名)

 ②FileInputStreamを元にBufferedInputStreamインスタンスの作成

   BufferedInputStream = new BufferedInputStream(FileInputStreamインスタンス);

 ③BufferedInputStreamを元にDataInputStreamインスタンスの作成

   DataInputStream = new DataInputStream(BufferedInputStreamインスタンス);

 ④データを読み込む

   boolearn 変数 = DataInputStream.readBoolean();
   char 変数 = DataInputStream.readChar();
   byte 変数 = DataInputStream.readByte();
   int 変数 = DataInputStream.readInt();  等

 ⑤読み出しが終了したらcloseメソッドによりストリームを開放する

   DataInputStream.close()


例.

 public class BinaryFile extends Frame implements ActionListener{

  Button b1,b2;
  TextArea ta;

  public static void main(String[] args) {
   new BinaryFile();
  }

  public BinaryFile(){
   this.setSize(300,200);
   ta = new TextArea();
   this.add(ta,BorderLayout.CENTER);
   b1 = new Button("Load");
   b1.addActionListener(this);
   b2 = new Button("Save");
   b2.addActionListener(this);
   Panel p = new Panel();
   p.add(b1);
   p.add(b2);
   this.add(p,BorderLayout.SOUTH);
   this.setVisible(true);
  }

  public void actionPerformed(ActionEvent ev){
   if(ev.getSource() == b1){
    this.load();
   }
   else if(ev.getSource() == b2){
    this.save();
   }
  }

  // バイナリファイル読み込み
  public void load(){
   FileInputStream is = null;
   BufferedInputStream bis = null;
   DataInputStream dis = null;
   try{
    is = new FileInputStream("sample.txt");
    bis = new BufferedInputStream(is);
    dis = new DataInputStream(bis);
    int r = dis.readInt();
    int g = dis.readInt();
    int b = dis.readInt();
    String s = dis.readUTF();
    ta.setForeground(new Color(r,g,b));
    ta.setText(s);
   } catch(Exception ex){
    ex.printStackTrace();
   } finally {
    try{
     dis.close();
    } catch(Exception ex2){
     ex2.printStackTrace();
    }
   }
  }

  // バイナリファイル書き込み
  public void save(){
   FileOutputStream os = null;
   BufferedOutputStream bos = null;
   DataOutputStream dos = null;
   try{
    os = new FileOutputStream("sample.txt");
    bos = new BufferedOutputStream(os);
    dos = new DataOutputStream(bos);
    Random rnd = new Random();
    Color c = ta.getForeground();
    int r = c.getRed();
    int g = c.getGreen();
    int b = c.getBlue();
    String s = ta.getText();
    dos.writeInt(r);
    dos.writeInt(g);
    dos.writeInt(b);
    dos.writeUTF(s);
    dos.flush();
    r = rnd.nextInt(256);
    g = rnd.nextInt(256);
    b = rnd.nextInt(256);
    ta.setForeground(new Color(r,g,b));
   } catch(Exception ex){
    ex.printStackTrace();
   } finally {
    try{
     dos.close();
    } catch (Exception ex2){
     ex2.printStackTrace();
    }
   }
  }

 }