Skip to content

zakonweb/serial-sequential-random-filing-AS-A2

Repository files navigation

Access Method in File System:

When we talk about file systems, there are several ways a computer can access data from a file: serial, sequential, binary, and direct (or random).

  1. Serial Access: Serial access refers to reading data from a file sequentially, starting from the beginning and proceeding step by step.

  2. Sequential Access: Sequential access involves reading data in order, allowing direct access to the desired point.

  3. Binary Access: Binary access allows data to be read or written in the form of bytes, enabling direct access to data without going through previous data.

  4. Direct (Random) Access: Direct access allows accessing data from any location within the file, not necessarily in a sequential manner.

Now, let's see how these access methods can be used in Python, Java, and VB.NET 2010.

Python:

Python provides a built-in function open() for file handling. The open() function takes two parameters: the file name and the mode. The mode indicates how the file is going to be opened: 'r' for reading, 'w' for writing, 'a' for appending, and 'b' for binary mode.

  1. Serial and Sequential Access: Python uses the same function read() to handle both serial and sequential access.

    file = open("file.txt", "r")
    print(file.read())
    file.close()
  2. Binary Access: Python uses the 'b' mode for binary file handling.

    file = open("file.bin", "rb")
    print(file.read())
    file.close()
  3. Direct Access: Python provides the seek() method to move the cursor to any position in the file.

    file = open("file.txt", "r")
    file.seek(50)  # Move to the 50th byte in the file.
    print(file.read())
    file.close()

Java:

Java also provides a comprehensive API for file handling. The File, FileInputStream, and FileOutputStream classes are commonly used for file handling in Java.

  1. Serial and Sequential Access: Java uses the read() method of FileInputStream for both serial and sequential access.

    FileInputStream file = new FileInputStream("file.txt");
    int i;
    while((i=file.read())!=-1) {  
        System.out.print((char)i);  
    }
    file.close();  
  2. Binary Access: Java uses the read() method of FileInputStream for binary file handling as well.

    FileInputStream file = new FileInputStream("file.bin");
    int i;
    while((i=file.read())!=-1) {  
        System.out.print(i);  
    }
    file.close();
  3. Direct Access: Java provides the RandomAccessFile class for direct file handling.

    RandomAccessFile file = new RandomAccessFile("file.txt", "rw");
    file.seek(50); // Move to the 50th byte in the file.
    int i;
    while((i=file.read())!=-1) {  
        System.out.print((char)i);  
    }
    file.close();  

VB.NET 2010:

In VB.NET 2010, file handling is accomplished using various classes such as StreamReader, StreamWriter, and BinaryReader.

  1. Serial and Sequential Access: VB.NET uses the StreamReader class for reading files serially or sequentially.

    Dim file As New StreamReader("file.txt")
    Console.WriteLine(file.ReadToEnd())
    file.Close()
    
  2. Binary Access: VB.NET utilizes the BinaryReader class for binary file handling.

    Dim file As New BinaryReader(File.Open("file.bin", FileMode.Open))
    Dim bytes As Byte() = file.ReadBytes(CInt(file.BaseStream.Length))
    For Each b As Byte In bytes
        Console.Write(b)
    Next
    file.Close()
  3. Direct Access: VB.NET does not provide direct support for random file access. However, you can achieve it by using the Seek method of the FileStream class.

    Dim file As New FileStream("file.txt", FileMode.Open, FileAccess.ReadWrite)
    file.Seek(50, SeekOrigin.Begin) ' Move to the 50th byte in the file.
    Dim reader As New StreamReader(file)
    Console.WriteLine(reader.ReadToEnd())
    reader.Close()
    file.Close()

These examples should provide a good understanding of serial, sequential, binary, and direct access file handling in Python, Java, and VB.NET 2010.