You are on page 1of 2

Week 1&2

import java.io.FileOutputStream;
import java.io.OutputStream;

public class Main {


public static void main(String args[]) {
String data = "This is a line of text inside the file. 123 sample";
try {
OutputStream out = new FileOutputStream("output.txt");
// Converts the string into bytes
byte[] dataBytes = data.getBytes();
// Writes data to the output stream
out.write(dataBytes); System.out.println("Data is written to the file.");
// Closes the output stream
out.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}

Output:
Data is written to a file.

} 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

Program#2.

Output Point out the error(s) and how they can be fixed
Enter (1) ouput.txt file (2) data.txt file: 55 Data Line 3 remove the sign # and insert *
not found Line 7 fixed the letter s in the word scanner and
make it capitalize S
Line 25 insert another = before the 2
Line 27 add the word File to the word
InputStream
Line 31 add letter r to the word array

You might also like