4. Operaciones fundamentales de Procesamiento de Archivos

 

4.1 Tipos de Archivos por su localización


Cuando un archivo desea utilizar un archivo particular ej. "data" , el sistema operativo debe buscar el archivo físico "data" y hacer el supuesto enlace hacia el asignándole un archivo lógico. Este archivo lógico tiene un nombre lógico .

 

4.2 Tipos de Archivos por su contenido (Binarios y de Texto)

Un archivo se dice que es de tipo texto si todo su contenido está almacenado en forma de cadenas de caracteres.
ej:  "cadena de texto   12345"
Un archivo binario por su parte guarda la representación en bytes del tipo de datos que se esté manejando
ej: "cadena de texto cf"

 


4.3  Abriendo y Cerrando Archivos

Abriendo Archivos

Una vez que se ha entablado el enlace con el archivo es necesario declarar lo que intentamos hacer con el:


Esto hace que el archivo este listo para usarse por el programa
Estamos posicionados al principio del archivo, listos para leer o escribir

fd= open (filename, flags [,pmode]);

fd= file descriptor
filename= physical file name
flags= O_APPEND, O_CREAT, O_EXCL, O_RDOINLY, O_RDWR, O_TRUNC, O_WRONLY
pmode=     rwe (Owner)    rwe  (Group)   rwe (Others)

Cerrando archivos
 


fclose(fd)

4.4 Modos de acceso a un archivo

Secuencial

Aleatorio o Directo



4.5 Operaciones comunes al emplear archivos

Lectura


Escritura


Seeking



4.6 Streams

 


Stream (stream of water)
Es una secuencia ordenada de bytes de una longitud sin determinación.

Existen 2 tipos de streams

Estos streams están relacionados con los dispositivos de entrada y salida que se mencionan a continuación y tambien se pueden redireccionar desde los mismos programas
Ejemplo desde Java:  System.setErr(PrintStream err), System.setIn(InputStream in)

Dispositivos de I/O
Stdin, Stdout, Stderr

(0) Stdin: dispositivo estándar de entrada (teclado)
(1) Stdout: dispositivo estándar de salida (consola)
(2) Stderr: dispositivo estándar de error (de nuevo la consola)
 

Redireccionamiento:
    1>filename
       # Redirect stdout to file "filename".
    1>>filename
       # Redirect and append stdout to file "filename".
    2>filename
       # Redirect stderr to file "filename".
    2>>filename
       # Redirect and append stderr to file "filename".
    &>filename
       # Redirect both stdout and stderr to file "filename".
  2>&1
       # Redirects stderr to stdout.
       # Error messages get sent to same place as standard output.
    0< FILENAME
   < FILENAME
       # Accept input from a file.

 


4.7 I/O Apis en los lenguajes de programación

 

4.7.1 Java 

En Java las clases de I/O se dividen en 2 grupos dependiendo si manejan bytes o chars
(el análogo serían los archivos binarios y de texto en otros lenguajes)

Character Streams

Byte Streams

Métodos de las superclases:
Entrada
Reader
              int read()
              int read(char cbuf[])
              int read(char cbuf[], int offset, int length)

InputStream
              int read()
              int read(byte cbuf[])
              int read(byte cbuf[], int offset, int length)

Salida
Writer
              int write(int c)
              int write(char cbuf[])
              int write(char cbuf[], int offset, int length)

OutputStream
              int write(int c)
              int write(byte cbuf[])
              int write(byte cbuf[], int offset, int length)
 
 
 

I/O Streams

Type of I/O Streams Description
Memory CharArrayReader
CharArrayWriter

ByteArrayInputStream
ByteArrayOutputStream

Use these streams to read from and write to memory. You create these streams on an existing array and then use the read and write methods to read from or write to the array.
StringReader
StringWriter

StringBufferInputStream

Use StringReader to read characters from a String in memory. Use StringWriter to write to a String. StringWriter collects the characters written to it in a StringBuffer, which can then be converted to a String.

StringBufferInputStream is similar to StringReader, except that it reads bytes from a StringBuffer

Pipe
PipedReader


PipedWriter

PipedInputStream
PipedOutputStream

Implement the input and output components of a pipe. Pipes are used to channel the output from one thread into the input of another. 
File 
FileReader


FileWriter

FileInputStream
FileOutputStream

Collectively called file streams, these streams are used to read from or write to a file on the native file system. The section 
Concatenation 
N/A
SequenceInputStream
Concatenates multiple input streams into one input stream. The section 
Object


Serialization 

N/A
ObjectInputStream


ObjectOutputStream

Used to serialize objects. 
....implements java.io.Serializable
Data


Conversion 

N/A
DataInputStream


DataOutputStream

Read or write primitive data types in a machine-independent format. . 


DataInputStream

  • readFully(byte[] b)
  • readFloat()
  • readDouble()
  • readInt()

  •  

     
     
     
     
     
     
     

    DataOutputStream

  • writeChars(String s)
  • writeFloat(float v)
  • writeDouble(double v)
  • writeInt(int v)
  • Counting 
    LineNumberReader
    LineNumberInputStream
    Keeps track of line numbers while reading. 
    Peeking Ahead 
    PushbackReader
    PushbackInputStream
    These input streams each have a pushback buffer. When reading data from a stream, it is sometimes useful to peek at the next few bytes or characters in the stream to decide what to do next. 
    Printing 
    PrintWriter
    PrintStream
    Contain convenient printing methods. These are the easiest streams to write to, so you will often see other writable streams wrapped in one of these. 
    Buffering 
    BufferedReader


    BufferedWriter

    BufferedInputStream
    BufferedOutputStream

    Buffer data while reading or writing, thereby reducing the number of accesses required on the original data source. Buffered streams are typically more efficient than similar nonbuffered streams and are often used with other streams. 
    Filtering 
    FilterReader


    FilterWriter

    FilterInputStream
    FilterOutputStream

    These abstract classes define the interface for filter streams, which filter data as it's being read or written. 
    Converting between Bytes and Characters 
    InputStreamReader


    OutputStreamWriter

    A reader and writer pair that forms the bridge between byte streams and character streams.
    An InputStreamReader reads bytes from an InputStream and converts them to characters, using the default character encoding or a character encoding specified by name.
    An OutputStreamWriter converts characters to bytes, using the default character encoding or a character encoding specified by name and then writes those bytes to an OutputStream.
    You can get the name of the default character encoding by calling System.getProperty("file.encoding")


    Es importante revisar el API de Java, así como el tutorial de I/O

    http://java.sun.com/j2se/1.4.1/docs/api/

    http://java.sun.com/docs/books/tutorial/essential/io/index.html



     

    4.7.2 C#

    Class

    Description

    Stream

    The abstract base class Stream supports reading and writing bytes.

    FileStream

    In addition to basic Stream behavior, this class supports random access to files through its Seek method and supports both synchronous and asynchronous operation.

    MemoryStream

    A nonbuffered stream whose encapsulated data is directly accessible in memory. This stream has no backing store and might be useful as a temporary buffer.

    BufferedStream

    A Stream that adds buffering to another Stream, such as a NetworkStream. (FileStream already has buffering internally, and a MemoryStream doesn't need buffering.) A BufferedStream object can be composed around some types of streams to improve read and write performance.

    TextReader

    The abstract base class for StreamReader and StringReader objects. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextReader are designed for Unicode character output.

    StreamReader

    Reads characters from a Stream, using Encoding to convert characters to and from bytes.

    StringReader

    Reads characters from a String. StringReader allows you to treat a String with the same API; thus, your output can be either a Stream in any encoding or a String.

    TextWriter

    The abstract base class for StreamWriter and StringWriter objects. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextWriter are designed for Unicode character input.

    StreamWriter

    Writes characters to a Stream, using Encoding to convert characters to bytes.

    StringWriter

    Writes characters to a String. StringWriter allows you to treat a String with the same API; thus, your output can be either a Stream in any encoding or a String.

    BinaryReader

    Reads binary data from a stream.

    BinaryWriter

    Writes binary data to a stream.

    C# API

    Class Browser


    4.7.3 C/C++

      Para el caso de C/C++ se recomienda leer los siguientes documentos:

    Capítulo de Acceso a Archivos en C
    Capítulo de Acceso a Archivos en C++

    Estos capítulos con tomados del libro: C++ the complete Reference, Herbert Schildt, Editorial Osborne
    Otra buena referencia: C++ a su alcance, un enfoque orientado a objetos, Luis Joyanes Aguilar

    Apis online:
    CPP Reference (C/C++)
    CPlusPlus Resources


     

    4.7.4 Ejemplos


    Archivo ZIP