Help

Páginas: 11 (2562 palabras) Publicado: 11 de diciembre de 2010
http://msdn.microsoft.com/es-es/library/system.io.filestream(VS.80).aspx

Ejemplo
En el siguiente ejemplo se muestran algunos de los constructores FileStream.
VB
C#
C++
F#
JScript

Copiar
Imports System
Imports System.IO
Imports System.Text

Public Class Test

Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"

' Delete the file if itexists.
If File.Exists(path) Then
File.Delete(path)
End If

'Create the file.
Dim fs As FileStream = File.Create(path)

AddText(fs, "This is some text")
AddText(fs, "This is some more text,")
AddText(fs, Environment.NewLine & "and this is on a new line")
AddText(fs, Environment.NewLine & Environment.NewLine)AddText(fs, "The following is a subset of characters:" & Environment.NewLine)

Dim i As Integer

For i = 1 To 120
AddText(fs, Convert.ToChar(i).ToString())

'Split the output at every 10th character.
If Math.IEEERemainder(Convert.ToDouble(i), 10) = 0 Then
AddText(fs, Environment.NewLine)
End If
Nextfs.Close()

'Open the stream and read it back.
fs = File.OpenRead(path)
Dim b(1024) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)

Do While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
Loop

fs.Close()
End Sub

Private Shared Sub AddText(ByVal fs As FileStream, ByValvalue As String)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(value)
fs.Write(info, 0, info.Length)
End Sub
End Class

En el ejemplo siguiente se abre un archivo o se crea si todavía no existe, y se agrega información al final del archivo.

VB
C#
C++
F#
JScript

Copiar
Imports System
Imports System.IO
Imports System.Text

Class FSOpenWritePublic Shared Sub Main()
Dim fs As New FileStream("c:\Variables.txt", FileMode.Append, FileAccess.Write, FileShare.Write)
fs.Close()
Dim sw As New StreamWriter("c:\Variables.txt", True, Encoding.ASCII)
Dim NextLine As String = "This is the appended text."
sw.Write(NextLine)
sw.Close()
End Sub 'Main
End Class 'FSOpenWrite

Cómo: Escribirtexto en un archivo

VB
C#
C++
F#
JScript

Copiar
Imports System
Imports System.IO

Class Test
Public Shared Sub Main()
' Create an instance of StreamWriter to write text to a file.
Using sw As StreamWriter = New StreamWriter("TestFile.txt")
' Add some text to the file.
sw.Write("This is the ")
sw.WriteLine("header for thefile.")
sw.WriteLine("-------------------")
' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
End Using
End Sub
End Class

VB
C#
C++
F#
JScript

Copiar
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public ClassTextToFile
Private Const FILE_NAME As String = "MyFile.txt"
Public Shared Sub Main()
If File.Exists(FILE_NAME) Then
Console.WriteLine("{0} already exists.", FILE_NAME)
Return
End If
Using sw As StreamWriter = File.CreateText(FILE_NAME)
sw.WriteLine("This is my file.")
sw.WriteLine("I can write ints {0} or floats {1},and so on.", 1, 4.2)
sw.Close()
End Using
End Sub
End Class

Cómo: Leer texto de un archivo

Visual Studio 2005
Otras versiones

• .NET Framework 4
• Visual Studio 2008
Los ejemplos de código siguientes muestran cómo leer texto desde un archivo de texto. El segundo ejemplo ofrece una notificación cuando se detecta el fin del archivo. Esta funcionalidad...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • help
  • help
  • help
  • Help
  • Help me
  • help
  • Help
  • Help me

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS