Files and Streams

undefined
F
i
l
e
s
 
a
n
d
 
S
t
r
e
a
m
s
W
h
a
t
 
i
s
 
a
 
f
i
l
e
?
Up until now, any stored data within a program is
lost when the program closes.
A file is a permanent way to store data
I
n
t
r
o
d
u
c
t
i
o
n
 
t
o
 
F
i
l
e
s
 
a
n
d
 
S
t
r
e
a
m
s
System.IO classes are used to work with files and
streams
F
i
l
e
 
-
 
b
l
o
c
k
 
o
f
 
i
n
f
o
r
m
a
t
i
o
n
 
s
t
o
r
e
d
 
o
n
 
d
i
s
k
 
o
r
 
a
n
o
t
h
e
r
 
m
e
d
i
a
T
e
x
t
 
f
i
l
e
 
-
 
f
i
l
e
 
t
h
a
t
 
c
o
n
t
a
i
n
s
 
l
i
n
e
s
 
o
f
 
w
r
i
t
t
e
n
 
i
n
f
o
r
m
a
t
i
o
n
 
t
h
a
t
c
a
n
 
b
e
 
s
e
n
t
 
d
i
r
e
c
t
l
y
 
t
o
 
t
h
e
 
s
c
r
e
e
n
 
o
r
 
p
r
i
n
t
e
r
B
i
n
a
r
y
 
f
i
l
e
 
-
 
f
i
l
e
 
t
h
a
t
 
c
o
n
t
a
i
n
s
 
b
i
t
s
 
t
h
a
t
 
d
o
 
n
o
t
 
n
e
c
e
s
s
a
r
i
l
y
r
e
p
r
e
s
e
n
t
 
p
r
i
n
t
a
b
l
e
 
t
e
x
t
 
E
x
a
m
p
l
e
s
:
 
W
o
r
d
 
f
i
l
e
,
 
m
a
c
h
i
n
e
l
a
n
g
u
a
g
e
 
f
i
l
e
You can work with two kinds of files – text files
(containing only characters) and binary files
T
e
x
t
 
v
s
 
B
i
n
a
r
y
 
F
i
l
e
s
A text file displayed in a text editor
A binary file displayed a text editor
Introduction to Processing Textual Data
One way to process textual files is from beginning to
end using sequential access .This type of file is called
a 
sequential file
Sequential files are useful for:
Storing text
Easy implementation in programs
Where real-time editing of file(s) is not required
I
n
t
r
o
d
u
c
t
i
o
n
 
t
o
 
P
r
o
c
e
s
s
i
n
g
 
T
e
x
t
u
a
l
 
D
a
t
a
Sequential files can be categorized into roughly three
types
1.
Free-form
 files have no particular format
2.
Fields in a 
delimited file 
are separated with a special character
called a delimiter
3.
In a 
fixed-field file
, each field occupies the same character
positions in every record
I
n
t
r
o
d
u
c
t
i
o
n
 
t
o
 
P
r
o
c
e
s
s
i
n
g
 
T
e
x
t
u
a
l
 
D
a
t
a
Sequential files can be read one character at a time,
one line at a time, or the entire file can be read at
once
Sequential files are typically read into a string or an
array
T
h
e
 
D
i
r
e
c
t
o
r
y
 
C
l
a
s
s
We need to  add
Imports System.IO
Common methods of the Directory class
Exists (path designation)
CreateDirectory (path designation)
Delete (path designation)
Code that uses some of the Directory methods
      Dim dir As String = "C:\VB 2005\Files\"
         If Not Directory.Exists(dir) Then
 
         
 
Directory.CreateDirectory(dir)
      End If
T
h
e
 
D
i
r
e
c
t
o
r
y
 
C
l
a
s
s
 
T
h
e
 
D
i
r
e
c
t
o
r
y
 
C
l
a
s
s
 The default location where the files we create are
saved is the bin directory of the Windows
Application with which we are working.
T
h
e
 
F
i
l
e
 
C
l
a
s
s
Common methods of the File class
Exists (path)
Delete (path)
Copy (source, dest)
Move (source, dest)
Code that uses some of the File methods
 
    Dim dir As String = "C:\VB 2005\Files\"
       Dim path As String = dir & "Products.txt"
          If File.Exists(path) Then
             File.Delete(path)
       End If
T
h
e
 
F
i
l
e
 
C
l
a
s
s
 
T
h
e
 
F
i
l
e
 
C
l
a
s
s
 
F
i
l
e
s
 
a
r
e
 
m
a
n
i
p
u
l
a
t
e
d
 
i
n
 
3
 
s
t
a
g
e
s
File Open 
: If the file does not exist it is 
created
created
and then opened
and then opened
 by the operating system. A
portion of 
memory (RAM)
memory (RAM)
 is reserved by the
Operating System.
Process File 
: When a file is open it can be 
written
written
to or read
to or read
 from.
Close File 
: When a file has been opened and
processed it 
must then be closed
must then be closed
. The Operating
system will then 
release the memory.
release the memory.
T
h
e
 
S
t
r
e
a
m
-
F
i
l
e
 
C
o
n
n
e
c
t
i
o
n
To perform file processing in Visual Basic,
namespace 
System.IO 
must be referenced.
This namespace includes definitions for stream
classes such as:
FileStream
StreamReader
StreamWriter
BinaryReader
BinaryWriter
Files are opened by creating objects of these stream
classes.
E
s
t
a
b
l
i
s
h
i
n
g
 
C
o
n
n
e
c
t
i
o
n
s
There are several different ways to establish file-
stream connections
    Create a StreamReader object
     
  Create a StreamWriter object
      Create a FileStream object
The results of using these techniques are similar –
they all result in the creation of (or opening of) a
stream (fs) against which all subsequent file
operations are written
T
h
e
 
S
t
r
e
a
m
R
e
d
e
r
 
C
l
a
s
s
The 
StreamReader
 and 
StreamWriter
 classes belong to the
System.IO namespace
The 
StreamReader
 constructor accepts one argument – the
path
 and filename of the sequential file to open.
Dim CurrentReader As StreamReader = New _
System.IO.StreamReader("C:\Demo.txt")
The Close method closes a sequential file.
Always close files when processing is complete to prevent loss of
data
Open files also consume system resources
Example:
 
CurrentReader.Close()
T
h
e
 
S
t
r
e
a
m
R
e
d
e
r
 
C
l
a
s
s
The 
Read
 method reads a single character or many
characters. Without arguments, the Read method
returns the 
Integer code point of the character read
The 
ReadLine
 method reads a record. The carriage
return at the end of the record is discarded. The
method returns a String containing the characters
read.
The 
ReadToEnd
 method reads from the current file
position to the end of the file. The method returns a
String containing the characters read
E
x
a
m
p
l
e
 
r
e
a
d
(
)
Example :Reading Entire Content of File
Reading a Sequential File One Record at a Time
T
h
e
 
S
t
r
e
a
m
W
r
i
t
e
r
 
C
l
a
s
s
The 
StreamWriter
 class of the System.IO namespace
writes a sequential file
The constructor accepts one argument – the file to write
Example:
Dim CurrentWriter As New _
    System.IO.StreamWriter("C:\Demo.txt")
    ' 
Statements to write the file.
CurrentWriter.Close()
T
h
e
 
S
t
r
e
a
m
W
r
i
t
e
r
 
C
l
a
s
s
The 
NewLine
 property contains the character(s) that mark
the end of the line
The 
Close
 method closes the sequential file
It's imperative to close a sequential file once writing is complete
to prevent loss of data
The 
Write
 method writes a character or array of characters
The 
WriteLine
 method writes data terminated by the
character(s) stored in the 
NewLine
 property
If the data type passed to 
Write
 or 
WriteLine
 is not a
string, these methods will call 
toString
Individual variables must be concatenated and separators must
be used
S
t
r
e
a
m
W
r
i
t
e
r
 
E
x
a
m
p
l
e
S
t
r
e
a
m
W
r
i
t
e
r
 
E
x
a
m
p
l
e
Imports System.IO
Module 
Module1
    
Sub Main()
        Dim path As String = 
"C:\testfolder\"
        
Dim filename As String = path & 
"test.txt"
        
Dim writer As New System.IO.
StreamWriter(filename)
            
Dim filecontent As String
        filecontent = 
Console.ReadLine()
        writer.WriteLine(filecontent)
        writer.Close()
    
End Sub
End Module
W
r
i
t
i
n
g
 
a
 
F
r
e
e
f
o
r
m
 
F
i
l
e
A freeform file can be written all at once as follows:
Dim StringData As String = "Freeform text"
Dim CurrentWriter As New _
    System.IO.StreamWriter("C:\Demo.txt")
CurrentWriter.Write(StringData)
CurrentWriter.Close()
W
r
i
t
i
n
g
 
a
 
D
e
l
i
m
i
t
e
d
 
F
i
l
e
T
h
e
 
F
i
l
e
S
t
r
e
a
m
 
C
l
a
s
s
1.
Create a FileStream object
 
 Dim path as String = “C:\VB 2005\Files\Products.txt
    Dim fs as New FileStream(path, FileMode.create, _
        FileAccess.Write)
The syntax for creating a FileStream object
New
 
FileStream(
path, FileMode, FileAccess, share]]
)
Members in the 
FileMode
 enumeration
  Append – opens a file if it exists and place the write pointer at  the end of
the file
  Create – Creates a new file.  If file exists it is overwritten
  CreateNew – Creates a new file.  If file already exists an exception is thrown
Open – Opens an existing file.  If file does not exist, an  exception is thrown
OpenOrCreate – Opens a file if it exists or creates a new file if it does not
exist
Truncate –Opens an existing file and truncates it to zero bytes (erases its
contents)
T
h
e
 
F
i
l
e
S
t
r
e
a
m
 
C
l
a
s
s
Members in the 
FileAccess
 enumeration
Read
ReadWrite
Write
Members in the 
FileShare
 enumeration
None
Read
ReadWrite
Write
Common method of the FileStream class
Close()   // Closing a file disconnects the application from the file
Example
Dim path as String = “C:\VB 2005\Files\Products.txt
Dim fs as New FileStream(path, FileMode.create, FileAccess.Write)
E
x
a
m
p
l
e
undefined
Imports System.IO
Public Class Form1
    Dim filecontent As String
    Dim fn As String = "test1.txt"
    Dim fs As FileStream
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
        fs = New FileStream(fn, FileMode.OpenOrCreate, FileAccess.Write)
        Dim writer As New StreamWriter(fs)
        filecontent = TextBox1.Text
        writer.WriteLine(filecontent)
        writer.Close()
        fs.Close()
        MsgBox("writing complete")
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
        fs = New FileStream(fn, FileMode.Open, FileAccess.Read)
        Dim reader As New StreamReader(fs)
        filecontent = reader.ReadLine()
        TextBox2.Text = filecontent
        reader.Close()
        fs.Close()
    End Sub
End Class
undefined
if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
Slide Note
Embed
Share

In the realm of software development, understanding how to work with files and streams is essential for efficient data handling. This involves reading data from files, writing data to files, and managing data streams for communication between different components of a system. Whether you are a beginner or an experienced developer, grasping the concepts of files and streams opens up a world of possibilities for creating robust and scalable applications. This article provides a comprehensive overview of files and streams, covering key concepts and practical examples to enhance your skills in data management.

  • Files
  • Streams
  • Data handling
  • Software development
  • Communication

Uploaded on Feb 25, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Files and Streams

  2. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store data

  3. Introduction to Files and Streams System.IO classes are used to work with files and streams File - block of information stored on disk or another media Text file - file that contains lines of written information that can be sent directly to the screen or printer Binary file - file that contains bits that do not necessarily represent printable text Examples: Word file, machine language file You can work with two kinds of files text files (containing only characters) and binary files

  4. Text vs Binary Files A text file displayed in a text editor A binary file displayed a text editor

  5. Introduction to Processing Textual Data One way to process textual files is from beginning to end using sequential access .This type of file is called a sequential file Sequential files are useful for: Storing text Easy implementation in programs Where real-time editing of file(s) is not required

  6. Introduction to Processing Textual Data Sequential files can be categorized into roughly three types Free-form files have no particular format Fields in a delimited file are separated with a special character called a delimiter 1. 2. In a fixed-field file, each field occupies the same character positions in every record 3.

  7. Introduction to Processing Textual Data Sequential files can be read one character at a time, one line at a time, or the entire file can be read at once Sequential files are typically read into a string or an array

  8. The Directory Class We need to add Imports System.IO Common methods of the Directory class Exists (path designation) CreateDirectory (path designation) Delete (path designation) Code that uses some of the Directory methods Dim dir As String = "C:\VB 2005\Files\" If Not Directory.Exists(dir) Then Directory.CreateDirectory(dir) End If

  9. The Directory Class

  10. The Directory Class The default location where the files we create are saved is the bin directory of the Windows Application with which we are working.

  11. The File Class Common methods of the File class Exists (path) Delete (path) Copy (source, dest) Move (source, dest) Code that uses some of the File methods Dim dir As String = "C:\VB 2005\Files\" Dim path As String = dir & "Products.txt" If File.Exists(path) Then File.Delete(path) End If

  12. The File Class

  13. The File Class

  14. Files are manipulated in 3 stages File Open : If the file does not exist it is created and then opened by the operating system. A portion of memory (RAM) is reserved by the Operating System. Process File : When a file is open it can be written to or read from. Close File : When a file has been opened and processed it must then be closed. The Operating system will then release the memory.

  15. The Stream-File Connection To perform file processing in Visual Basic, namespace System.IO must be referenced. This namespace includes definitions for stream classes such as: FileStream StreamReader StreamWriter BinaryReader BinaryWriter Files are opened by creating objects of these stream classes.

  16. Establishing Connections There are several different ways to establish file- stream connections Create a StreamReader object Create a StreamWriter object Create a FileStream object The results of using these techniques are similar they all result in the creation of (or opening of) a stream (fs) against which all subsequent file operations are written

  17. The StreamReder Class The StreamReader and StreamWriter classes belong to the System.IO namespace The StreamReader constructor accepts one argument the path and filename of the sequential file to open. Dim CurrentReader As StreamReader = New _ System.IO.StreamReader("C:\Demo.txt") The Close method closes a sequential file. Always close files when processing is complete to prevent loss of data Open files also consume system resources Example: CurrentReader.Close()

  18. The StreamReder Class The Read method reads a single character or many characters. Without arguments, the Read method returns the Integer code point of the character read The ReadLine method reads a record. The carriage return at the end of the record is discarded. The method returns a String containing the characters read. The ReadToEnd method reads from the current file position to the end of the file. The method returns a String containing the characters read

  19. Example read()

  20. Example :Reading Entire Content of File

  21. Reading a Sequential File One Record at a Time

  22. The StreamWriter Class The StreamWriter class of the System.IO namespace writes a sequential file The constructor accepts one argument the file to write Example: Dim CurrentWriter As New _ System.IO.StreamWriter("C:\Demo.txt") ' Statements to write the file. CurrentWriter.Close()

  23. The StreamWriter Class The NewLine property contains the character(s) that mark the end of the line The Close method closes the sequential file It's imperative to close a sequential file once writing is complete to prevent loss of data The Write method writes a character or array of characters The WriteLine method writes data terminated by the character(s) stored in the NewLine property If the data type passed to Write or WriteLine is not a string, these methods will call toString Individual variables must be concatenated and separators must be used

  24. StreamWriter Example

  25. StreamWriter Example Imports System.IO Module Module1 Sub Main() Dim path As String = "C:\testfolder\" Dim filename As String = path & "test.txt" Dim writer As New System.IO.StreamWriter(filename) Dim filecontent As String filecontent = Console.ReadLine() writer.WriteLine(filecontent) writer.Close() End Sub End Module

  26. Writing a Freeform File A freeform file can be written all at once as follows: Dim StringData As String = "Freeform text" Dim CurrentWriter As New _ System.IO.StreamWriter("C:\Demo.txt") CurrentWriter.Write(StringData) CurrentWriter.Close()

  27. Writing a Delimited File

  28. Example

  29. Imports System.IO Public Class Form1 Dim filecontent As String Dim fn As String = "test1.txt" Dim fs As FileStream Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click fs = New FileStream(fn, FileMode.OpenOrCreate, FileAccess.Write) Dim writer As New StreamWriter(fs) filecontent = TextBox1.Text writer.WriteLine(filecontent) writer.Close() fs.Close() MsgBox("writing complete") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click fs = New FileStream(fn, FileMode.Open, FileAccess.Read) Dim reader As New StreamReader(fs) filecontent = reader.ReadLine() TextBox2.Text = filecontent reader.Close() fs.Close() End Sub End Class

  30. if exist "$(TargetPath).locked" del "$(TargetPath).locked" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#