Download presentation
Presentation is loading. Please wait.
Published byKade Halsell Modified over 9 years ago
2
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: File and Stream Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan dan mengaplikasikan File and Stream pada Visual Basic 2008 Referensi: Deitel Deitel, Visual Basic 2008 (2009), How to Program, Prentice Hall. Chapter 13
3
Pemrograman VisualMinggu …12… Page 2 Agenda Class File and Directory Creating a Sequential-Access text File Reading Data From a Sequential Access Text File Creating a Sequential-Access file using object serialization Reading and Deserializing Data from a Sequential Access Text File
4
Pemrograman VisualMinggu …12… Page 3 Class File and Directory Didalam Namespace System.IO terdapat dua buah class yang dapat kita gunakan untuk mengolah sebuah file dan direktori yaitu class System.IO.File dan System.IO.Directory. Class System I.O File dapat kita manfaatkan untuk melakukan beberapa operasi dasar terhadap sebuah file seperti menyalin file, menghapus file dan sebagainya
5
Pemrograman VisualMinggu …12… Page 4 File class shared methods AppendText –Return a streamwriter that appends text to an existing file or create a file if one does not exist. Copy –Copies a file to a new file. Create –Creates a file and returns its associated FileStream. Delete –Deletes the specified file. Exists –Return true if the specified file exists and false otherwise.
6
Pemrograman VisualMinggu …12… Page 5 File class shared methods (Continued …) GetCreationTime –Returns a DateTime object representing when the file was created. GetLastAccessTime –Returns a DateTime object representing when the file was last access. GetLastWriteTime –Returns a DateTime object representing when the file was last modified. Move –Moves the specified file to a specified location.
7
Pemrograman VisualMinggu …12… Page 6 File class shared methods (Continued …) Open –Returns a FileStream associated with the specified file and equipped with the specified read/write permissions. OpenRead –Returns a read-only FileStream associated with the specified file. OpenText –Returns a StreamReader associated with the specified file. OpenWrite –Returns a read/write FileStream associated with the specified file.
8
Pemrograman VisualMinggu …12… Page 7 Directory class shared methods CreateDirectory –Create a directory and returns its associated DirectoryInfo object. Delete –Deletes the specified directory. Exists –Returns true if the specified directory exists and false otherwise. GetDirectories –Returns a string array containing the names of the subdirectories in the specified directory. GetFiles –Returns a string array containing the names of the files in the specified directory.
9
Pemrograman VisualMinggu …12… Page 8 Directory class shared methods (Continued …) GetCreationTime –Returns a DateTime object representing when the directory was created. GetLastAccessTime –Returns a DateTime object representing when the directory was last accessed. GetLastWriteTime –Returns a DateTime object representing when items were last written to the directory. Move –Moves the specified directory to a specified location.
10
Pemrograman VisualMinggu …12… Page 9 Demonstrating Classes File and Directory See Figure 18.5
11
Pemrograman VisualMinggu …12… Page 10 Creating a Sequential-Access Text File Visual Basic Imposes no structure on files, so it does not recognize concepts like ‘record”. This means that you must structure files to meet the requirement of your applications. A SaveFileDialog is a modal dialog. The three-argument FileStream constructor receive a String containing the name of the file to open, a constant describing how to open the file and a constant describing the file permissions. A StreamWriter object can be constructed with a FileStream argument that specifies the file to which StreamWriter outputs text. Class StreamWriter belongs to the System.IO Namespace
12
Pemrograman VisualMinggu …12… Page 11 Demonstrating Sequential-Access Text File Class BankUIForm See Figure 18.7 Class Record See Figure 18.8 Using a Character Stream to Create an output File See Figure 18.9
13
Pemrograman VisualMinggu …12… Page 12 Reading Data from a Sequential- Access Text File Data is stored in files so that it can be retrieved for processing when it is needed. To retrieve data sequentially from a file, programs normally start from the beginning of the file, reading data consecutively until the desired data is found. It sometimes is necessary to process a file sequentially several times during the execution of a program. An OpenFileDialog allows a user to select files to open. Method ShowDialog displays the dialog. See Figure 18.11-18.12
14
Pemrograman VisualMinggu …12… Page 13 Creating a Sequential-Access File Using Object Serialization Classes that are marked with the serializable attribute indicate to the CLR that objects of the class can be serialized. Objects that we wish to write to or read from a stream must include this attribute in their class definitions or implement the Iserializable interface. In a serializable class, you must ensure that every instance variable of the class is also serializable. By default, all simple type variables are serializable. For Reference type variables, you must check the declaration of the class (any possibly its supperclasses) to ensure that the type is serializable. See Figure 18.13-14
15
Pemrograman VisualMinggu …12… Page 14 Reading and Deserializing Data from a Sequential-Access Text File Method Deserialize (of class BinaryFormatter) reads a serialized object from a stream and reforms the object in memory. Method Deserialize returns a reference of type object which must be cast to the appropriate type to manipulate the object. If an error occurs during deserialization, a SerializationException is thrown. See Figure 18.13-15
16
Pemrograman VisualMinggu …12… Page 15 Questions & Answers
17
Pemrograman VisualMinggu …12… Page 16 Thank You
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.