10 Examples of FileInputStream in Java



The `FileInputStream` class in Java is used to read data from a file as a stream of bytes. Here are 10 examples demonstrating the usage of `FileInputStream`:

1. Reading a File Using FileInputStream:

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


2. Reading a File with Buffer:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt");
BufferedInputStream bis = new BufferedInputStream(fis)) {
int data;
while ((data = bis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}



3. Reading Fixed Number of Bytes:

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamFixedBytes {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


4. Reading File into a byte array:

import java.io.FileInputStream;
import java.io.IOException;

public class FileToByteArrayExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
byte[] data = new byte[fis.available()];
fis.read(data);
System.out.println(new String(data));
} catch (IOException e) {
e.printStackTrace();
}
}
}



5. Skip Bytes in FileInputStream:

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamSkipExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
fis.skip(5); // Skip the first 5 bytes
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


6. Using try-with-resources and Paths:

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileInputStreamWithPaths {
public static void main(String[] args) {
Path filePath = Paths.get("example.txt");
try (FileInputStream fis = new FileInputStream(filePath.toFile())) {
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


7. Reading Binary File:

import java.io.FileInputStream;
import java.io.IOException;

public class BinaryFileReader {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("binaryfile.bin")) {
int data;
while ((data = fis.read()) != -1) {
System.out.print(data + " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


8. Reading File with Charset:

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class FileInputStreamWithCharset {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
byte[] data = fis.readAllBytes();
System.out.println(new String(data, StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
}



9. Reading File Using FileChannel:

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt");
FileChannel channel = fis.getChannel()) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (channel.read(buffer) != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


10. Reading File Using Scanner:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileInputStreamWithScanner {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt");
Scanner scanner = new Scanner(fis)) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Make sure to replace the file names ("example.txt" and "binaryfile.bin") with the actual names of the files you want to read. Also, handle exceptions appropriately in a production environment.




Post a Comment

Previous Post Next Post