|
| 1 | +**Access Method in File System:** |
| 2 | + |
| 3 | +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). |
| 4 | + |
| 5 | +1. **Serial Access:** Serial access refers to reading data from a file sequentially, starting from the beginning and proceeding step by step. |
| 6 | + |
| 7 | +2. **Sequential Access:** Sequential access involves reading data in order, allowing direct access to the desired point. |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +4. **Direct (Random) Access:** Direct access allows accessing data from any location within the file, not necessarily in a sequential manner. |
| 12 | + |
| 13 | +Now, let's see how these access methods can be used in Python, Java, and VB.NET 2010. |
| 14 | + |
| 15 | +**Python:** |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +1. **Serial and Sequential Access:** Python uses the same function `read()` to handle both serial and sequential access. |
| 20 | + |
| 21 | + ```python |
| 22 | + file = open("file.txt", "r") |
| 23 | + print(file.read()) |
| 24 | + file.close() |
| 25 | + ``` |
| 26 | + |
| 27 | +2. **Binary Access:** Python uses the 'b' mode for binary file handling. |
| 28 | + |
| 29 | + ```python |
| 30 | + file = open("file.bin", "rb") |
| 31 | + print(file.read()) |
| 32 | + file.close() |
| 33 | + ``` |
| 34 | + |
| 35 | +3. **Direct Access:** Python provides the `seek()` method to move the cursor to any position in the file. |
| 36 | + |
| 37 | + ```python |
| 38 | + file = open("file.txt", "r") |
| 39 | + file.seek(50) # Move to the 50th byte in the file. |
| 40 | + print(file.read()) |
| 41 | + file.close() |
| 42 | + ``` |
| 43 | + |
| 44 | +**Java:** |
| 45 | + |
| 46 | +Java also provides a comprehensive API for file handling. The `File`, `FileInputStream`, and `FileOutputStream` classes are commonly used for file handling in Java. |
| 47 | + |
| 48 | +1. **Serial and Sequential Access:** Java uses the `read()` method of `FileInputStream` for both serial and sequential access. |
| 49 | + |
| 50 | + ```java |
| 51 | + FileInputStream file = new FileInputStream("file.txt"); |
| 52 | + int i; |
| 53 | + while((i=file.read())!=-1) { |
| 54 | + System.out.print((char)i); |
| 55 | + } |
| 56 | + file.close(); |
| 57 | + ``` |
| 58 | + |
| 59 | +2. **Binary Access:** Java uses the `read()` method of `FileInputStream` for binary file handling as well. |
| 60 | + |
| 61 | + ```java |
| 62 | + FileInputStream file = new FileInputStream("file.bin"); |
| 63 | + int i; |
| 64 | + while((i=file.read())!=-1) { |
| 65 | + System.out.print(i); |
| 66 | + } |
| 67 | + file.close(); |
| 68 | + ``` |
| 69 | + |
| 70 | +3. **Direct Access:** Java provides the `RandomAccessFile` class for direct file handling. |
| 71 | + |
| 72 | + ```java |
| 73 | + RandomAccessFile file = new RandomAccessFile("file.txt", "rw"); |
| 74 | + file.seek(50); // Move to the 50th byte in the file. |
| 75 | + int i; |
| 76 | + while((i=file.read())!=-1) { |
| 77 | + System.out.print((char)i); |
| 78 | + } |
| 79 | + file.close(); |
| 80 | + ``` |
| 81 | + |
| 82 | +**VB.NET 2010:** |
| 83 | + |
| 84 | +In VB.NET 2010, file handling is accomplished using various classes such as `StreamReader`, `StreamWriter`, and `BinaryReader`. |
| 85 | + |
| 86 | +1. **Serial and Sequential Access:** VB.NET uses the `StreamReader` class for reading files serially or sequentially. |
| 87 | + ``` |
| 88 | + Dim file As New StreamReader("file.txt") |
| 89 | + Console.WriteLine(file.ReadToEnd()) |
| 90 | + file.Close() |
| 91 | + ``` |
| 92 | + |
| 93 | +2. **Binary Access:** VB.NET utilizes the `BinaryReader` class for binary file handling. |
| 94 | + |
| 95 | + ```vbnet |
| 96 | + Dim file As New BinaryReader(File.Open("file.bin", FileMode.Open)) |
| 97 | + Dim bytes As Byte() = file.ReadBytes(CInt(file.BaseStream.Length)) |
| 98 | + For Each b As Byte In bytes |
| 99 | + Console.Write(b) |
| 100 | + Next |
| 101 | + file.Close() |
| 102 | + ``` |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | + ```vbnet |
| 107 | + Dim file As New FileStream("file.txt", FileMode.Open, FileAccess.ReadWrite) |
| 108 | + file.Seek(50, SeekOrigin.Begin) ' Move to the 50th byte in the file. |
| 109 | + Dim reader As New StreamReader(file) |
| 110 | + Console.WriteLine(reader.ReadToEnd()) |
| 111 | + reader.Close() |
| 112 | + file.Close() |
| 113 | + ``` |
| 114 | + |
| 115 | +These examples should provide a good understanding of serial, sequential, binary, and direct access file handling in Python, Java, and VB.NET 2010. |
0 commit comments