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

📄 io13.txt

📁 输入输出流 综合应用实例 可是自学者更轻松的理解 输入输出流的知识
💻 TXT
字号:
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
class ThreadOne extends Thread
{
   RandomAccessFile input;
   int b=0;
   byte tom[]=new byte[50];
   ThreadOne(String fileName)
   {
      try{
          input=new RandomAccessFile(fileName,"rw");
         }
      catch(FileNotFoundException e)
         {
         }
   }
   public void run()
   { try{ 
          FileChannel channel=input.getChannel();
          while((b=input.read(tom,0,50))!=-1) 
           { 
             FileLock lock=channel.tryLock();
             String str=new String (tom,0,b);
             System.out.printf("\n%s,",getName()); 
             System.out.printf("读取:%s",str);
             try {
                    sleep(1000);
                 }
             catch(InterruptedException eee)
                 {
                 }
             lock.release();
           }
         input.close();
        }  
      catch(Exception ee)
        {
          System.out.println(ee);
        }
   }
}
class ThreadTwo extends Thread
{
   FileInputStream input;
   int b=0;
   byte tom[]=new byte[20];
   ThreadTwo(String fileName)
   {  
      try{
            input=new FileInputStream(fileName);
         } 
      catch(FileNotFoundException e)
         {
         }
   }
   public void run()
   {         
       while(true)
         {
            try
             {
                b=input.read(tom,0,20);
                if(b==-1)
                   return;
                String str=new String (tom,0,b);
                System.out.printf("\n%s,",getName()); 
                System.out.printf("\n我读取的内容:%s",str);
             }
           catch(IOException ee)
             {
               System.out.printf("\n%s引起堵塞,%s,",getName(),ee.toString());
             }
           try
             {
                sleep(1000);
             }
             catch(InterruptedException eee)
             {
             }
         }
      }
}
public class Example
{
  public static void main(String args[])
  {
     ThreadOne one= new ThreadOne("Example.java");
     ThreadTwo two=new ThreadTwo("Example.java");
     two.start();
     one.start();
    
  }
}

⌨️ 快捷键说明

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