⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 通过实例可以更好的了解java
💻
📖 第 1 页 / 共 2 页
字号:
   }
  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 + -