0% found this document useful (0 votes)
22 views2 pages

Javaprogrammingweek 3

This Java code reads data from an output.txt file into a byte array, prints the available bytes and read data to the console, and closes the input stream. It uses a FileInputStream to read from the file, catches any exceptions, and prints the stack trace if an exception occurs.

Uploaded by

genriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Javaprogrammingweek 3

This Java code reads data from an output.txt file into a byte array, prints the available bytes and read data to the console, and closes the input stream. It uses a FileInputStream to read from the file, catches any exceptions, and prints the stack trace if an exception occurs.

Uploaded by

genriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

} import java.io.

FileInputStream;
import java.io.InputStream;
public class Main { public static void main(String args[]) {
byte[] array = new byte[100];
try {
InputStream input = new FileInputStream("output.txt");
System.out.println("Available bytes in the file: " + input.available());
// Read byte from the input stream
input.read(array);
System.out.println("Data read from the file: ");
// Convert byte array into string
String data = new String(array);
System.out.println(data);
// Close the input stream
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}

Output:

Available bytes in file:51


Data read from the file
This is the line of the text inside the file. 123 sample

Debugging skills

Program#1.

Output Point out the error(s) and how they can be fixed
Output: Enter a string: 66 Line 1 add letter o after the letter i
Data is written to the file Line 2 add letter i before the letter o
(output.txt) Line 5 add letter I to the word static
Line 7 add letter t to the word print
Line 9 add the word File before the word
OutputStream
Line 15 add letter e to the word exception

You might also like