Created
March 8, 2025 19:52
-
-
Save pablohdzvizcarra/ca2be55a053e7b3486642da28ed0e7a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
public class Example4 { | |
public static void main(String[] args) { | |
String filePath = "data.txt"; | |
int bufferSize = 100; // number of bytes | |
try (FileInputStream fis = new FileInputStream(filePath); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | |
byte[] buffer = new byte[bufferSize]; | |
int bytesRead; | |
while ((bytesRead = fis.read(buffer)) != -1) { | |
baos.write(buffer, 0, bytesRead); | |
// do something with each chunk | |
} | |
byte[] fileContent = baos.toByteArray(); | |
System.out.println(new String(fileContent)); | |
} catch (IOException e) { | |
System.out.println("An error ocurred reading the file: " + e.getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment