?? 第二十章例子.txt
字號:
}
catch(IOException e) {}
線程1 one =new 線程1(out,in);線程2 two=new 線程2(in,out);
one.start(); two.start();
}
}
class 線程1 extends Thread
{ PipedOutputStream out; PipedInputStream in;
byte b[]={1,2,3};
線程1(PipedOutputStream a,PipedInputStream b)// 構造方法獲取輸入和
{try{ out=a;in=b; //輸出管道
out.connect(in); //將二者連接
}
catch(IOException e){}
}
public void run()
{try {out.write(b,0,3);} //寫數據到輸出管道
catch(IOException e){}
}
}
class 線程2 extends Thread
{ PipedInputStream in; PipedOutputStream out;
byte a[]=new byte[3];
線程2(PipedInputStream a,PipedOutputStream b)// 構造方法獲取輸入和
{ try{ in=a; //輸出管道
out=b;
in.connect(out); //將二者連接
}
catch(IOException e){}
}
public void run()
{ try
{in.read(a,0,3);
for(int i=0;i<=2;i++)
{System.out.println(""+a[i]);} //從輸入管道讀取數據
int c=a[0]+a[1]+a[2];
System.out.println(""+c);
}
catch(IOException e) {}
}
}
20-例子12
import java.io.*;
public class Example20_12
{public static void main(String args[])
{try
{FileOutputStream fos=new FileOutputStream("jerry.dat");
DataOutputStream out_data=new DataOutputStream(fos);
out_data.writeInt(100);out_data.writeInt(10012);
out_data.writeLong(123456);
out_data.writeFloat(3.1415926f); out_data.writeFloat(2.789f);
out_data.writeDouble(987654321.1234);
out_data.writeBoolean(true);out_data.writeBoolean(false);
out_data.writeChars("i am ookk");
}
catch(IOException e){}
try
{ FileInputStream fis=new FileInputStream("jerry.dat");
DataInputStream in_data=new DataInputStream(fis);
System.out.println(":"+in_data.readInt());//讀取第1個int整數。
System.out.println(":"+in_data.readInt());//讀取第2個int整數。
System.out.println(":"+in_data.readLong()); //讀取第long整數 。
System.out.println(":"+in_data.readFloat());//讀取第1個float數。
System.out.println(":"+in_data.readFloat());//讀取第2個float數。
System.out.println(":"+in_data.readDouble());
System.out.println(":"+in_data.readBoolean());//讀取第1個boolean。
System.out.println(":"+in_data.readBoolean());//讀取第2個boolean。
char c;
while((c=in_data.readChar())!='\0') //'\0'表示沒有空字符。
System.out.print(""+c+".");
}
catch(IOException e){}
}
}
20-例子13
import java.awt.*;import java.io.*;import java.awt.event.*;
public class Example20_13
{public static void main(String args[])
{ Frame_FileDialog f=new Frame_FileDialog();f.pack();}
}
class Frame_FileDialog extends Frame implements ActionListener
{ FileDialog filedialog_save,filedialog_load;//聲明2個文件對話框
MenuBar menubar;Menu menu;MenuItem item1,item2;
FileInputStream file_read=null; FileOutputStream tofile=null;
DataInputStream in_data=null; DataOutputStream out_data=null;
TextField 姓名[]=new TextField[30],成績[]=new TextField[30];
Label label=new Label();
Frame_FileDialog()
{ super("學生成績錄入");
setBounds(100,120,100,100); setVisible(true);
menubar=new MenuBar();
menu=new Menu("文件");
item1=new MenuItem("打開文件"); item2=new MenuItem("保存文件");
item1.addActionListener(this); item2.addActionListener(this);
menu.add(item1); menu.add(item2);
menubar.add(menu); setMenuBar(menubar);
//下面創建1個依賴于該窗口的保存成績單對話框:
filedialog_save=new FileDialog(this,"保存成績單話框",FileDialog.SAVE);
filedialog_save.setVisible(false);
//再創建1個依賴于該窗口的打開成績單對話框:
filedialog_load=new FileDialog(this,"打開成績對話框",FileDialog.LOAD);
filedialog_load.setVisible(false);
filedialog_save.addWindowListener(new WindowAdapter()//對話框增加適配器。
{public void windowClosing(WindowEvent e)
{filedialog_save.setVisible(false);}
});
filedialog_load.addWindowListener(new WindowAdapter()//對話框增加適配器。
{public void windowClosing(WindowEvent e)
{filedialog_load.setVisible(false);}
});
addWindowListener(new WindowAdapter() //窗口增加適配器。
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0);}
});
Panel p=new Panel();p.setLayout(new GridLayout(30,2));
for(int i=0;i<=29;i++)
{姓名[i]=new TextField("姓名");成績[i]=new TextField("0");
姓名[i].setBackground(Color.cyan);
p.add(姓名[i]);p.add(成績[i]);
}
ScrollPane scroll=new ScrollPane();scroll.add(p);
add("Center",scroll);
add(new Label("成績錄入表 在左面輸入姓名 在右面輸入分數"),"North") ;
add(label,"South");
}
public void actionPerformed(ActionEvent e) //實現接口中的方法,
//打開或保存一個成績單。
{ if(e.getSource()==item1)
{ filedialog_load.setVisible(true);String s;
try{
File file=new File(filedialog_load.getDirectory(),filedialog_load.getFile());
file_read=new FileInputStream(file);
in_data=new DataInputStream(file_read);//使用數據流。
label.setText(filedialog_load.getDirectory().toString()+
filedialog_load.getFile().toString());
int i=0;
while((s=in_data.readUTF())!=null)
{姓名[i].setText(s); 成績[i].setText(""+in_data.readDouble());i++;}
}
catch(FileNotFoundException e1){}
catch(IOException e2){}
try {in_data.close(); file_read.close();
}
catch(IOException exp){}
}
else if(e.getSource()==item2)
{ filedialog_save.setVisible(true);
try {File file=new File(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=new FileOutputStream(file);
out_data=new DataOutputStream(tofile);//使用數據流。
for(int i=0;i<=29;i++)
{out_data.writeUTF(姓名[i].getText());
out_data.writeDouble(Double.valueOf(成績[i].getText()).doubleValue());
}
}
catch(FileNotFoundException e1){}
catch(IOException e2){}
try {out_data.close(); tofile.close();
}
catch(IOException exp){}
}
}
}
20-例子14
import java.awt.*;import java.awt.event.*;
import java.io.*;
public class Example20_14 extends Frame implements ActionListener
{TextArea text=null; Button 讀入=null,寫出=null;
FileInputStream file_in=null; FileOutputStream file_out=null;
ObjectInputStream object_in=null; //對象輸入流。
ObjectOutputStream object_out=null; //對象輸出流。
Example20_14()
{setLayout(new FlowLayout()); text=new TextArea(6,10);
讀入=new Button("讀入對象"); 寫出=new Button("寫出對象");
讀入.addActionListener(this);寫出.addActionListener(this);
setVisible(true); add(text);add(讀入);add(寫出);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){System.exit(0);}
});
pack();setSize(300,300);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==寫出)
{ try{file_out=new FileOutputStream("tom.txt");
object_out=new ObjectOutputStream(file_out);//創建對象輸出流。
object_out.writeObject(text); //寫對象到文件中。
object_out.close();
}
catch(IOException event){}
}
else if(e.getSource()==讀入)
{ try{
file_in=new FileInputStream("tom.txt");
object_in=new ObjectInputStream(file_in); //創建對象輸入流。
TextArea temp=(TextArea)object_in.readObject();//從文件中讀入對象。
temp.setBackground(Color.pink); this.add(temp);//添加該對象到窗口。
this.pack();this.setSize(600,600);
object_in.close();
}
catch(ClassNotFoundException event)
{System.out.println("不能讀出對象");}
catch(IOException event)
{System.out.println("can not read file");}
}
}
public static void main(String args[])
{Example20_14 win=new Example20_14();}
}
20-例子15
import java.io.*;
class Student implements Serializable//實現接口Serializable的Student類。
{String name=null;double height;
Student(String name,double height)
{this.name=name;this.height=height;
}
public void setHeight (double c)
{this.height=c;
}
}
public class Example20_15
{public static void main(String args[])
{Student zhang=new Student("zhang ping",1.65);
try{ //創建對象輸出流:
FileOutputStream file_out=new FileOutputStream("s.txt");
ObjectOutputStream object_out=new ObjectOutputStream(file_out);
//寫學生對象到文件中:
object_out.writeObject(zhang);
//輸出學生姓名和身高:
System.out.println(zhang.name+"的身高是:"+zhang.height);
//創建對象輸入流:
FileInputStream file_in=new FileInputStream("s.txt");
ObjectInputStream object_in=new ObjectInputStream(file_in);
//從文件中讀入學生對象:
zhang=(Student)object_in.readObject();
zhang.setHeight(1.78); //修改身高。
System.out.println(zhang.name+"現在的身高是:"+zhang.height);
}
catch(ClassNotFoundException event)
{System.out.println("不能讀出對象");}
catch(IOException event)
{System.out.println("can not read file"+event);}
}
}
20-例子16
import java.io.*;
public class IOTest
{ public static void main(String args[])
{try{ FileInputStream in=new FileInputStream("tom.txt") ;
PushbackInputStream push=new PushbackInputStream(in);
int c=0;
byte b[]=new byte[1];
while ( (c=push.read())!=-1)
{ if(c=='a') //回壓的條件
{ push.unread('A'); //push回壓字節'A'。
push.read(b,0,1); //push讀出被回壓的字節,放入數組b。
System.out.print(new String(b,0,1));
}
else
{System.out.print((char)c);
}
}
push.close();
}
catch(IOException e){}
}
}
20-例子17
import java.io.*;
public class IOTest
{ public static void main(String args[])
{try{ FileReader in=new FileReader("Jerry.txt") ;
PushbackReader push=new PushbackReader(in);
int c;
char b[]=new char[1];
while ( (c=push.read(b,0,1))!=-1)//讀取兩個字符放入字符數組b。
{ String s=new String(b);
if(s.equals("搏")) //回壓的條件
{ push.unread('博'); //push回壓字符'博'
push.read(b,0,1); //push讀出被回壓的字符字節,放入數組b。
System.out.print(new String(b));
}
else if(s.equals("晾"))
{ push.unread('涼');
push.read(b,0,1);
System.out.print(new String(b));
}
else
{System.out.print(new String(b));
}
}
push.close();
}
catch(IOException e){}
}
}
20-例子18
import java.awt.*;import java.io.*;import java.awt.event.*;
public class User_Java
{public static void main(String args[])
{ JDK f=new JDK();
f.pack();f.addWindowListener(new WindowAdapter() //窗口增加適配器。
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
f.setBounds(100,120,700,360);
f.setVisible(true);
}
}
class JDK extends Frame implements ActionListener,Runnable
{
Thread compiler=null; //負責編譯的線程。
Thread run_prom=null,run_applet=null; //負責運行程序的線程。
boolean bn=true;
CardLayout mycard;
Panel p=new Panel();
File file_saved=null;
TextArea input_text=new TextArea(),//程序輸入區。
compiler_text=new TextArea(),//編譯出錯顯示區。
dos_out_text=new TextArea();//程序運行時,負責顯示在dos窗口的輸出信息。
Button button_input_text,button_compiler_text,
button_compiler,button_run_prom,button_see_doswin;
TextField input_flie_name_text=
new TextField("輸入被編譯的文件名字.java");
TextField run_file_name_text=
new TextField("輸入應用程序主類的名字");
JDK()
{super("Java編程小軟件");
mycard=new CardLayout();
button_input_text=new Button("程序輸入區(白色)");
button_compiler_text=new Button("編譯結果區(粉色)");
button_compiler=new Button("編譯程序");
button_run_prom=new Button("運行應用程序");
button_see_doswin=new Button("查看應用程序運行時在dos窗口輸出的信息");
p.setLayout(mycard);
p.add("input",input_text);p.add("compiler",compiler_text);
p.add("dos",dos_out_text);
add(p,"Center");
add( button_see_doswin,"South");
compiler_text.setBackground(Color.pink);
dos_out_text.setBackground(Color.blue);
Panel p1=new Panel();p1.setLayout(new GridLayout(4,2));
p1.add(new Label("按扭輸入源程序:"));p1.add(button_input_text);
p1.add(new Label("按扭看編譯結果:"));p1.add(button_compiler_text);
p1.add(input_flie_name_text); p1.add(button_compiler);
p1.add(run_file_name_text); p1.add(button_run_prom);
add(p1,"North");
button_input_text.addActionListener(this);
button_compiler_text.addActionListener(this);
button_compiler.addActionListener(this);
button_run_prom.addActionListener(this);
button_see_doswin.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button_input_text)
{ mycard.show(p,"input");
}
else if(e.getSource()==button_compiler_text)
{ mycard.show(p,"compiler");
}
else if(e.getSource()==button_see_doswin)
{ mycard.show(p,"dos");
}
else if(e.getSource()==button_compiler)
{ compiler=new Thread(this);compiler.start();
mycard.show(p,"compiler");
}
else if(e.getSource()==button_run_prom)
{run_prom=new Thread(this); run_prom.start();mycard.show(p,"dos");
}
}
public void run()
{if(Thread.currentThread()==compiler)
{ compiler_text.setText(null);
String temp=input_text.getText().trim();
byte buffer[]=temp.getBytes();
int b=buffer.length;
String flie_name=input_flie_name_text.getText().trim();
try{ file_saved=new File(flie_name);
FileOutputStream whritefile=
new FileOutputStream(file_saved);
whritefile.write(buffer,0,b);whritefile.close();
}
catch(IOException e5)
{System.out.println("Error ");
}
try{ Runtime ce=Runtime.getRuntime();
InputStream in=ce.exec("javac "+flie_name).getErrorStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte shuzu[]=new byte[100];
int n;boolean bn=true;
while((n=bin.read(shuzu,0,100))!=-1)
{ String s=null;
s=new String(shuzu,0,n);
compiler_text.append(s);
if(s!=null) bn=false;
}
if(bn){ compiler_text.append("編譯正確"); }
}
catch(IOException e1){}
}
else if(Thread.currentThread()==run_prom)
{ dos_out_text.setText(null);
try{ Runtime ce=Runtime.getRuntime();
String path=run_file_name_text.getText().trim();
InputStream in=ce.exec("java "+path).getInputStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte zu[]=new byte[150];
int fine;String s=null;
while((fine=bin.read(zu,0,150))!=-1)
{s=new String(zu,0,fine);
dos_out_text.append(s);
}
}
catch(IOException e1){}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -