En este programa, vamos a aprender cómo leer el texto de un archivo después de saltarse algunos bytes e imprimirlas en la pantalla de salida utilizando FileInputStream .
Dado un archivo con un poco de texto y tenemos que imprimir texto después de saltarse algunos bytes utilizando FileInputStream en Java.
Hay un archivo llamado “IncludeHelp.txt” que se almacena en el sistema de sonido “E:” unidad en “JAVA” carpeta ( puede elegir su camino ), y el archivo contiene el siguiente texto,
The quick brown fox jumps over the lazy dog.
Ejemplo:
File’s content is: "The quick brown fox jumps over the lazy dog."
After skipping starting 10 bytes...
Output: "brown fox jumps over the lazy dog."
Considere el programa
import java.io.*;
public class Skip
{
public static void main(String[] args)
{
//file class object
File file = new File("E:/JAVA/IncludeHelp.txt");
try
{
FileInputStream fin = new FileInputStream(file);
int ch;
System.out.println("File's content after 10 bytes is: ");
//skipping 10 bytes to read the file
fin.skip(10);
while( (ch = fin.read()) != -1 )
System.out.print((char) ch);
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex.toString());
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe.toString());
}
catch (Exception e)
{
System.out.println("Exception: " + e.toString());
}
}
}
salida
File's content after 10 bytes is:
brown fox jumps over the lazy dog.