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

📄 l.txt

📁 清华本科全套java课件 经典不用多说
💻 TXT
字号:
//-------------1------File类及方法-----------------
import java.io.*;

public class a
{
  public static void main(String args[])
  {
    try {
      File fdir1 = new File("test");
      if ( fdir1.exists() && fdir1.isDirectory()){
  	System.out.println("test目录存在");
        String[] farray = fdir1.list() ;
  	System.out.println(" 列出test目录下的内容");
        for( int i=0; i< farray.length; i++)    //列出目录下内容
           System.out.println( farray[i]);
        File fdir2=new File("test\\subtest");
	if(!fdir2.exists())  fdir2.mkdir();	//创建原不存在的目录
	System.out.println(" 创建目录后再次列出test目录下的内容");
        farray = fdir1.list() ;	
        for( int i=0; i< farray.length; i++) //检查目录是否已建立
	   System.out.println( farray[i]);
        fdir2.delete() ;
 
        File ffile = new File("test\\a.java");    
	System.out.println(" 列出a.java文件的有关信息 :");
        System.out.println(ffile.getName() +"   " + ffile.getPath()
				+ "   " + ffile.length());
       }
       else 
          System.out.println("test不存在或不是目录");
     }   // try 块
     catch(Exception e)
     {
	System.out.println(e.toString());
     }
  }  // end of main()
}  //end of class a

//------------------------2  列目录树 递归算法----------
import java.io.*;
import java.util.*;

public class a  {
  public static void main(String args[]){
      File path = new File(args[0]);
      if ( path.exists() && path.isDirectory())
                Ls.list(path) ;
  }
}
class Ls {
  static int m_indentLevel = -1 ;

  static void list(File d){
    File[] files  ;
    files=d.listFiles() ;
    Arrays.sort (files) ;  //排序
    m_indentLevel++ ;
    for(int i=0,n=files.length;i<n;i++) {
      for (int indent=0; indent<m_indentLevel ; indent++){
         System.out.print("     ");
      }
      System.out.println(files[i].toString());
      if (files[i].isDirectory()) {
              list(files[i]);
      }
    }
    m_indentLevel--;
  }
}
            
//-----------------  3 文件读写,对话框 ----------
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class a {
  public static void main(String args[]) {
    new FileFrame();
  }
}

class FileFrame extends Frame implements ActionListener {
  TextArea ta;
  Button open,quit;
  FileDialog fd;

  FileFrame() {
    super("获取并显示文本文件");
    fd = new FileDialog(this,"打开文件",FileDialog.LOAD);
    ta = new TextArea(10,45);
    open = new Button("打开");
    quit = new Button("关闭");
    open.addActionListener(this);
    quit.addActionListener(this);
    setLayout(new FlowLayout());
    add(ta);
    add(open);
    add(quit);
    setSize(350,250);
    show();
  }
  public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand()=="打开") {
      fd.show();//弹出并显示文件对话框,程序暂停直至用户选定一文件
      try{
        File myfile = new File(fd.getDirectory(),fd.getFile());
        RandomAccessFile raf = new RandomAccessFile(myfile,"r");
        while(raf.getFilePointer()<raf.length()) {
          ta.append(raf.readLine()+"\n");
        }
      }
      catch(IOException ioe) {
	System.err.println(ioe.toString());
      }
    }
    if(e.getActionCommand()=="关闭") {
      dispose();
      System.exit(0);
    }
  }
}

//-------------------4 学生信息维护   -----------------
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Stack;

public class a {
  public static void main(String args[]) {
    new FileFrame();
  }
}

class FileFrame extends Frame  {
  Label snolabel = new Label("学号");
  Label snamelabel = new Label("          姓名");
  Label sexlabel = new Label("性别");
  Label classlabel = new Label("       班级");
  Label resumelabel = new Label("简历");
  Label imagelabel = new Label("相片");
  TextField sno = new TextField(6);   
  TextField sname = new TextField(10);   
  TextField msg = new TextField(30);   
  TextArea resume = new TextArea(5,20);  
  CheckboxGroup sex = new CheckboxGroup();
  Checkbox sex1 = new Checkbox("男", true,sex );
  Checkbox sex2 = new Checkbox("女", false,sex );
  Toolkit toolkit ;
  boolean drawPicture = false;
  Image  stuImage ;
  TextField picname = new TextField(10);
  Button reset = new Button("  reset   ");
  Button next =  new Button("   next   ");
  Button prior = new Button("   prior  ");   
  Button insert = new Button("   insert ");
  Button exit = new Button("    exit   ");
  List clss = new List(5,false);
  Panel p1,p2,p3,p4,p5 ;
  File f ;
  RandomAccessFile fs ;
 
  FileFrame() {

    super("学生信息维护界面");
    setSize(550,400);
    setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

   p1 = new Panel();
    p1.add(snolabel);p1.add(sno);p1.add(snamelabel);p1.add(sname); 
   add(p1);

   p2 = new Panel();
    clss.add("力01");  clss.add("文01"); clss.add("物01");
    clss.add("生01");  clss.add("热01"); clss.add("化01");
    p2.add(sexlabel);p2.add(sex1);p2.add(sex2);p2.add(classlabel);p2.add(clss); 
   add(p2);

   p3 = new Panel();
    p3.add(resumelabel);p3.add(resume);p3.add(imagelabel);p3.add(picname);
    
   add(p3);

   p4 = new Panel();
   p4.add(reset);p4.add(next);p4.add(prior);
   p4.add(insert);p4.add(exit); 
    reset.addActionListener(new eset());
    next.addActionListener(new en());
    prior.addActionListener(new ep());
    insert.addActionListener(new ei());
    exit.addActionListener(new ee());
   add(p4);
   p5 = new Panel();
    p5.add(msg);
   add(p5);
  try{
    f = new File("students");
    fs = new RandomAccessFile(f,"rw");
  }
  catch(Exception ex){
    msg.setText(ex.getMessage());
  } 
  toolkit=getToolkit();
  setVisible(true);
}

  public void paint(Graphics g) {
      if (drawPicture)g.drawImage(stuImage ,360,180,this);
  }

  class en implements ActionListener{
    public void actionPerformed(ActionEvent e){
     try{
      sno.setText(Integer.toString(fs.readInt()));  //读学号
      sname.setText(fs.readUTF());                  //读姓名 
      if(fs.readChar() =='m')                   //读性别(m/w)
        sex1.setState(true);
      else
        sex2.setState(true);
      String s1 = fs.readUTF();                      //读班级 
      for(int i=0;i<clss.getItemCount();i++) {
        if(clss.getItem(i).indexOf(s1)>-1)  clss.select(i);
      }
      String s2 = fs.readUTF();                      //读简历
      resume.setText("");  resume.append(s2);
      String s3 = fs.readUTF();                 //读相片文件名
      picname.setText(s3);
      stuImage = toolkit.getImage(s3);
      drawPicture = true ;
      repaint();
     }
     catch(Exception ex){
      msg.setText(ex.getMessage());
     }      
    }  //end of method
  }  //end of class eq

  class eset implements ActionListener{
    public void actionPerformed(ActionEvent e){
      try{
        fs.seek(0);
        sno.setText("");
        sname.setText(""); 
        sex1.setState(true);
        clss.select(0);
        resume.setText("");
        picname.setText("");
        drawPicture = false ;
        repaint();
      }catch(Exception eSeek){
         msg.setText(eSeek.getMessage());
       }
    }
  }

  class ep implements ActionListener{
    public void actionPerformed(ActionEvent e){
      
    }
  }

  class ei implements ActionListener{
    public void actionPerformed(ActionEvent e){
      try{
        fs.seek(fs.length());
        fs.writeInt(Integer.parseInt(sno.getText()));
        fs.writeUTF(sname.getText());
        if(sex1.getState()) 
            fs.writeChar('m');
        else
            fs.writeChar('w');
        fs.writeUTF(clss.getSelectedItem());
        fs.writeUTF(resume.getText());
        fs.writeUTF(picname.getText());       
      repaint();
      }
      catch(Exception ex) {
	msg.setText(ex.getMessage());
      }  
    }
  }  
  class ee implements ActionListener{
    public void actionPerformed(ActionEvent e){
      dispose();
      System.exit(0);
    }
  }  
}

---------------------------5 输入输出 -----------------------
import java.io.*;
import java.util.*;
class a {
  public static void main(String[] args){
    try {
      String s  ;
      BufferedReader stdin =  new BufferedReader(
                                new InputStreamReader(System.in));
      System.out.println("------输入一行数据-----------");
      s = stdin.readLine();      //从标准输入读
      System.out.println("------几种流的测试-----------");
      System.out.println(s+"标准输入输出");     //往标准输出写

      StringReader sr = new StringReader(s);
      int c ;
      while((c = sr.read()) != -1)         //从字符串读
          System.out.print((char)c);
      System.out.println("读字符串流");

      PrintWriter  fout = new PrintWriter(new FileWriter("test1.txt"));
      fout.println(s+"文件字符流读写");  fout.close();
      BufferedReader fin =  new BufferedReader(new FileReader("test1.txt"));
      System.out.println(fin.readLine()) ;

      System.out.println("------有无缓冲区对比测试-----------");
      Reader reader = new FileReader("test2.txt");
      Date d1 = new Date() ;
      while((c =reader.read()) !=-1) {}
      Date d2 = new Date() ;
      System.out.println("time1 is  "  + (d2.getTime()-d1.getTime())) ;
      reader.close();

      reader = new BufferedReader(new FileReader("test2.txt"));
      d1 = new Date() ;
      while((c =reader.read()) !=-1) {}
      d2 = new Date() ;
      System.out.println("time2 is  "  + (d2.getTime()-d1.getTime())) ;

      System.out.println("------基本数据类型:文件读写-----------");
      DataOutputStream out = new DataOutputStream(new FileOutputStream("test3"));
      out.writeInt(980001);
      out.writeUTF("wang hong");
      out.writeFloat(485.2F);
      out.writeInt(980002);
      out.writeUTF("王宏");
      out.writeFloat(500.2F);   
      out.flush() ; out.close() ;
      DataInputStream in = new DataInputStream(new FileInputStream("test3"));
      System.out.println(in.readInt() +"\t"+ in.readUTF() +"\t"+ in.readFloat());
      System.out.println(in.readInt() +"\t"+ in.readUTF() + "\t"+in.readFloat());
    }
    catch(Exception e){}
  }
}

---------------------6 对象流--------------------
import java.io.* ;
import java.util.* ;
class a{
  public static void main(String[] s){
   try{ 
    FileOutputStream outStream = new FileOutputStream("t.tmp");
    ObjectOutputStream oOut = new ObjectOutputStream(outStream); 
    oOut.writeInt(12345); 
    oOut.writeObject("Today"); 
    oOut.writeObject(new Date()); 

    Book b1 = new Book("计算机文化基础",23);
    Book b2 = new Book("数据库与计算机管理",32);
    Book b3 = new Book("面向对象程序设计",40);
    Book[] book1={b1,b2,b3};
    Reader r1 = new Reader(980001,"王山",book1);
    oOut.writeObject(r1);

    oOut.flush(); 
    outStream.close(); 

    FileInputStream inStream = new FileInputStream("t.tmp"); 
    ObjectInputStream oIn = new ObjectInputStream(inStream);
    int i = oIn.readInt(); 
    String today = (String)oIn.readObject(); 
    Date date = (Date)oIn.readObject(); 
    Reader r2 = (Reader)oIn.readObject() ;
    inStream.close(); 

    System.out.println("i=  "+i) ;
    System.out.println(today) ;
    System.out.println(date.toString()) ;
    System.out.println(r2.getRname());
    Book[] book2 = r2.getBooks();
    for(int j=0 ;j<book2.length ;j++){
      System.out.println(book2[j].getName());
    }  
   }catch(Exception e){}
  }
}
class Book implements Serializable{
 String bname;
 int price ;

 Book(String p1,int p2) {
    bname=p1 ;
    price=p2 ;  }
 String getName(){
    return bname;}
 int getPrice() {
    return price ; }
}

class Reader implements Serializable{
   int rno ;            
   String rname ;
   Book[] books ;
   Reader(int p1,String p2,Book[] p3) {
     rno=p1;
     rname=p2;
     books = p3 ;
   }
   String getRname(){
      return rname;
   }
   Book[] getBooks(){
      return books ;
   }
}
---------------------------7 管道流---------------
import java.io.* ;
class a {
  public static void main(String[] args) {
    T2 t2 = new T2("t2");

    try{
      Thread.currentThread().sleep(1000);
    }catch(Exception e){}

    T1 t1 = new T1("t1");

    try{
     t1.join();
     t2.join();
    }catch(Exception e){}
    System.out.println(" ----- end of all threads -----");
  }
}

class T1 extends Thread {
  PipedWriter pipeOut ;
  T2 t2 = null ;

  T1(String name){
      super(name);
      pipeOut = new PipedWriter();
      start();
  }

  public void run(){
    ThreadGroup group = Thread.currentThread().getThreadGroup();
    while(group.getParent() != null) {
      group = group.getParent() ;
    }
    Thread[] threadList = new Thread[group.activeGroupCount()+10];
    int count = group.enumerate(threadList);
    for (int j=0;j<count;j++){
      if(threadList[j].getName().equals("t2")) {
         t2 = (T2)threadList[j];
         break ;
      }
    }
    if (t2 == null) {
      System.out.println("there is not t2 thread! ");
      return ;
    }
    if (t2.connect(pipeOut)){
      try{
        pipeOut.write(1);
        pipeOut.write(2);
        pipeOut.write(0);
        pipeOut.close();
      }catch(Exception  e){}
    } else {
      System.out.println("connect error");
      return ;
    }
  }
}

class T2 extends Thread {
  PipedReader pipeIn ;
  boolean waitFor = true ;
  int i ;

  T2(String name){
    super(name);
    start();
  }

  public synchronized void run(){
    while(waitFor){
      try{wait();} catch(Exception e1){}
    }

    try{
      while( (i = pipeIn.read()) != 0) {
        System.out.println(i);
      }
      System.out.println("end pipe");
      return ;
    }catch(Exception e2){}
  }

  public synchronized boolean connect(PipedWriter pw){
    try{
      pipeIn = new PipedReader(pw) ;
    }catch(Exception e3){}
    waitFor = false ;
    notify() ;
    return true ;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -