I have the following script to create 3 files:
set serveroutput on
declare
nombreArchivo varchar2(30);
f_out UTL_FILE.FILE_TYPE;
begin
nombreArchivo :='fich_fseek.txt';
dbms_output.put_line('Abrir el fichero '||nombrearchivo||' en modo escritura.');
f_out:=UTL_FILE.FOPEN('TEMPORAL',nombreArchivo,'w');
UTL_FILE.PUT_LINE(f_out,'Hola, me llamo Álvaro.');
UTL_FILE.PUT_LINE(f_out,'Esto es una prueba para ver cómo funcionan las funciones FSEEK y FGETPOS.');
UTL_FILE.PUT_LINE(f_out,'Espero que te diviertas.');
UTL_FILE.NEW_LINE(f_out,1);
UTL_FILE.PUT_LINE(f_out,'Atentamente,');
UTL_FILE.PUT_LINE(f_out,'el que esto escribe');
UTL_FILE.FCLOSE(f_out);
nombreArchivo :='caracter.txt';
dbms_output.put_line('Abrir el fichero '||nombrearchivo||' en modo escritura.');
f_out:=UTL_FILE.FOPEN('TEMPORAL',nombreArchivo,'w');
UTL_FILE.PUT(f_out,'a');
UTL_FILE.FCLOSE(f_out);
nombreArchivo :='vacio.txt';
dbms_output.put_line('Abrir el fichero '||nombrearchivo||' en modo escritura.');
f_out:=UTL_FILE.FOPEN('TEMPORAL',nombreArchivo,'w');
UTL_FILE.FCLOSE(f_out);
exception
when others then -- así me aseguro que cualquier flujo abierto será cerrado
dbms_output.put_line('Se ha producido un error: '||SQLERRM);
UTL_FILE.FCLOSE_ALL;
end;
/
the problem arises when I create another script to read the files without making use of the exception clause :
set serveroutput on
declare
nombreArchivo varchar2(30):='fich_fseek.txt';
--nombreArchivo varchar2(30):='caracter.txt';
--nombreArchivo varchar2(30):='vacio.txt';
f_out UTL_FILE.FILE_TYPE;
texto varchar2(100);
posición pls_integer := 0;
existe boolean;
tamaño_archivo number;
tamaño_bloque number;
begin
UTL_FILE.FGETATTR('TEMPORAL', nombreArchivo, existe, tamaño_archivo, tamaño_bloque);
if existe then
if tamaño_archivo > 0 then
dbms_output.put_line('Abrir el fichero '||nombrearchivo||' en modo lectura, que tiene un tamaño de '||tamaño_archivo||' bytes.');
f_out:=UTL_FILE.FOPEN('TEMPORAL',nombreArchivo,'r');
posición := UTL_FILE.FGETPOS(f_out);
while posición < tamaño_archivo loop
UTL_FILE.GET_LINE(f_out, texto);
dbms_output.put_line('pre Posición '||posición);
dbms_output.put_line(texto);
posición := UTL_FILE.FGETPOS(f_out);
dbms_output.put_line('post Posición '||posición);
end loop;
UTL_FILE.FCLOSE(f_out);
else
dbms_output.put_line('El fichero '||nombrearchivo||' está vacío (0 bytes).');
end if;
else
dbms_output.put_line('El archivo '||nombrearchivo||' no existe');
end if;
end;
/
Then an ORA-01403 "no data found"
happens, but only with the 'fich_fseek.txt' file.