0% found this document useful (0 votes)
22 views

Programming - Part 4

The document discusses file handling and ciphers in VB.NET. It describes how to perform common file handling operations like creating, opening, reading, writing, and closing files using classes like File, StreamReader, and StreamWriter. It also explains different types of string manipulation ciphers like the Caesar cipher and provides an example implementation of the Caesar cipher in VB.NET to encrypt and decrypt messages.

Uploaded by

shammahzuze05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Programming - Part 4

The document discusses file handling and ciphers in VB.NET. It describes how to perform common file handling operations like creating, opening, reading, writing, and closing files using classes like File, StreamReader, and StreamWriter. It also explains different types of string manipulation ciphers like the Caesar cipher and provides an example implementation of the Caesar cipher in VB.NET to encrypt and decrypt messages.

Uploaded by

shammahzuze05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DEVELOPER SERIES File Handling & Cipher 2023

FILE HANDLING
 File handling in VB.NET refers to the process of reading, writing, and manipulating
files using Visual Basic programming language.

 It involves various operations such as creating, opening, reading, writing, closing, and
deleting files.

 File handling is an essential aspect of programming, as it allows applications to store


and access data persistently.

KEY COMPONENTS OF FILE HANDLING(palevel redu)

File System Object (FSO): Provides methods for creating, deleting, and moving files and
folders.

Stream Classes: Represent a sequence of bytes, enabling reading and writing operations
on files.

Input/Output (I/O) Classes: Facilitate various file operations, such as StreamReader for
reading text files and StreamWriter for writing text files.

Common File Handling Operations

Creating Files: Use File.Create method to create new files.

Opening Files: Use File.Open or File.OpenRead methods to open existing files for
reading or writing.

Reading Files: Use StreamReader class to read text files line by line or byte by byte.

Writing Files: Use StreamWriter class to write text files line by line or byte by byte.

Closing Files: Use Close method of the corresponding StreamReader or StreamWriter


object to release file resources.

Deleting Files: Use File.Delete method to remove files from the system.

Copying and Moving Files: Use File.Copy and File.Move methods to copy or move files
between locations.

File Information Access: Use FileInfo and DirectoryInfo classes to retrieve file and
directory information, such as size, creation date, and attributes.
Creating a file
To create a new file you can use the file.create method.

It takes the file path as a parameter.

For example the following code creates a new file called myfile.txt in the
current directory

Dim file as streamwriter =file.Create(“myfile.txt”)

Writing to file
To write a file use the writeline method of the streamwriter object.

This method takes a string as a parameter.

For example the following code writes the string “welcome to A level computer
experts” to the file we created above.

File.writeline(“welcome to a level computer science expects”)

Reading from file


To read from file you can use the streamReader class.

The streamReader class provides methods for reading lines of text from file.

For example the following code reads the contents of the file myfile.txt and prints
them to console.

Dim file As StreamReader = File.Opentext(“myfile.txt”)

Dim Line As String

Do while line=file.Readline()

Console.Writeline(line)

Loop

File.close()
Closing a File
It is very important(zvakakosha) to close a file after you have finished using it.

This will release file handle and allow other programs to access the file.

The close method of the streamreader and streamwriter classes closes the file.
file.Close()

Here is a complete example of what we talked about above all of it


Imports System.IO

Imports System.Text

Imports System

Module Module1

Sub Main()

'creating a file

Dim file As StreamWriter

file = New StreamWriter("myfile.text")

'writing to file

file.WriteLine("welcome to a level computers")

'closing file

file.Close()

End Sub

End Module
CIPHERS
Are a type of encryption algorithm that utilizes string operations to transform
plaintext into ciphertext and vice versa.

COMMON STRING MANIPULATION CIPHERS


1. Caesar Cipher - Shifts each character of the plaintext by a fixed number of
positions in the alphabet

2. Substitution Cipher - Replaces each character of the plaintext with the


corresponding character from another alphabet .

3. Transposition Cipher - Rearranges the order of the characters in the plaintext


without altering the characters themselves.

Example of the implementation of the Caesar Cipher


'to encrypt

Public Function EncryptCaesar(chibytext As String, shift As Integer) As String

Dim ciphertext As String = ""

For Each character As Char In chibytext

Dim newChar As Char = character

Dim asciiCode As Integer = AscW(character) + shift

If asciiCode < 65 Or asciiCode > 90 Then

newChar = character

Else

newChar = ChrW(asciiCode Mod 26 + 65)

End If
ciphertext &= newChar

Next character

Return ciphertext

End Function

'to decrypt

Public Function DecryptCaesar(ciphertext As String, shift As Integer) As String

Dim plaintext As String = ""

For Each character As Char In ciphertext

Dim newChar As Char = character

Dim asciiCode As Integer = AscW(character) - shift

If asciiCode < 65 Or asciiCode > 90 Then

newChar = character

Else

newChar = ChrW(asciiCode Mod 26 + 65)

End If

plaintext &= newChar

Next character

Return plaintext

End Function
Contact: 0781081816

Email:[email protected]

ISAIAH 43 VS 2

GOD BLESS YOU


======================================================================================

You might also like