Lee un archivo
Java
2.348 visualizaciones desde el 19 de Octubre del 2017
Lector de archivos
import java.io.*;
class ReadBinaryFile
{
public static void main(String[] args)
{
// The name of the file to open.
String fileName = "file.txt";
FileInputStream inputStream = null;
try
{
// Use this for reading the data.
byte[] buffer = new byte[1000];
inputStream = new FileInputStream(fileName);
// read fills buffer with data and returns
// the number of bytes read (which of course
// may be less than the buffer size, but
// it will never be more).
int total = 0;
int nRead = 0;
while ((nRead = inputStream.read(buffer)) != -1)
{
// Convert to String so we can display it.
// Of course you wouldn't want to do this with
// a 'real' binary file.
System.out.println(new String(buffer));
total += nRead;
}
System.out.println("Read " + total + " bytes");
}
catch (FileNotFoundException ex)
{
System.out.println("Unable to open file '" + fileName + "'");
}
catch (IOException ex)
{
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
finally
{
// Always close files.
try
{
if (inputStream != null)
{
inputStream.close();
}
}
catch (IOException e)
{
}
}
}
}
Comentarios sobre la versión: 1.0 (1)