JavaSE series code 31: Application of RandomAccessFile class

As we have known, body content can have two types of members: member variables and methods. Some of these methods are called construction methods, which are used by a class to create objects and give the initial state of the objects created by the class. The other part of methods can be divided into instance methods and class methods. The objects created by classes can call these methods to form certain algorithms, reflecting that the objects have certain functions. When an object calls a method, the member variable in the method refers to the member variable assigned to the object. Object can’t call constructor. Constructor is used to create object.
The definition of method includes two parts: method declaration and method body.

import java.io.*;
public class Javase_31
{
  public static void main(String[] args) throws IOException
  {
    StringBuffer stfDir=new StringBuffer();
    System.out.println("请输入文件所在的路径");
    char ch;
    while ((ch=(char)System.in.read())!='\n')
      stfDir.append(ch);
    File dir=new File(stfDir.toString());
    System.out.println("请输入欲读取的文件名");
    StringBuffer stfFilename=new StringBuffer();
    char c;
    while ((c=(char)System.in.read())!='\n')
      stfFilename.append(c);
    File readFrom=new File(dir,stfFilename.toString());
    if (readFrom.isFile() && readFrom.canWrite() && readFrom.canRead())
    {
      RandomAccessFile rafFile=new RandomAccessFile(readFrom,"rw");
      while (rafFile.getFilePointer()<rafFile.length())
        System.out.println(rafFile.readLine());
      rafFile.close();
    }
    else
      System.out.println("文件不可读!");
  }
}
Published 52 original articles · Like 162 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/blog_programb/article/details/105386300