import java.io.*; public class ReadFileExample { public static void main(String[] args) { try { FileInputStream inputFile = new FileInputStream("test.txt"); int data; while ((data = inputFile.read()) != -1) { System.out.print((char) data); } inputFile.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class CopyFileExample { public static void main(String[] args) { try { FileInputStream inputFile = new FileInputStream("image.jpg"); FileOutputStream outputFile = new FileOutputStream("newimage.jpg"); byte[] buffer = new byte[1024]; int length; while ((length = inputFile.read(buffer)) > 0) { outputFile.write(buffer, 0, length); } inputFile.close(); outputFile.close(); } catch (IOException e) { e.printStackTrace(); } } }In this example, we create both a FileInputStream and a FileOutputStream object. We then read the input file in 1024-byte chunks using a buffer, and write each chunk to the output file. Finally, we close both file streams. Determining package library: The FileInputStream class is part of the java.io package, which is included in the core Java libraries.