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

📄 randomexample.java

📁 使用RandomAccessFile类处理文件. RandomAccessFile类创建的流的指向既可以作为目的,称为随机读写流.
💻 JAVA
字号:
import java.io.*;
public class RandomExample
{
        public static void main(String args[])
        {
                 File f=new File("hello.txt");
                 try{
                        RandomAccessFile random=new RandomAccessFile(f,"rw");
                        random.seek(0);
                        long m=random.length();
                        while(m>=0)
                        {
                               m=m-1;
                               random.seek(m);
                               int c=random.readByte();
                               if(c<=255&&c>=0)
                               {
                                    System.out.println((char)c);
                               }
                               else
                               {
                                     m=m-1;
                                     random.seek(m);
                                     byte cc[]=new byte[2];
                                     random.readFully(cc);
                                     System.out.print(new String(cc));
                                 }
                    }
                    long forthEndPosition=0;
                    int forth=1;
                    byte n=-1;
                    while((forth!=-1)&&(forth<=4))
                    {
                           if(n=='\n')
                           {
                                   forthEndPosition=random.getFilePointer();
                                   forth++;
                            }
                     }
                     random.seek(m);
                     long endPosition=random.getFilePointer();
                     long mark=endPosition;
                     int j=1;
                     while((mark>=0)&&(j<=6))
                     {
                             mark--;
                             random .seek(mark);
                             n=random.readByte();
                             if(n=='\n')
                             {
                                  endPosition=random.getFilePointer();
                                  j++;
                              }
                      }
                      random.seek(mark);
                      long startPoint=random.getFilePointer();
                      while(startPoint<endPosition-1)
                      {
                              n=random.readByte();
                              System.out.print((char)n);
                              startPoint=random.getFilePointer();
                       }
                       random.close();
              }
              catch(IOException ee){}
         }
}

⌨️ 快捷键说明

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