0% found this document useful (0 votes)
46 views6 pages

Java Programming Week 2

This Java program reads data from a text file called "output.txt" and displays it. It uses a FileInputStream to open the file, reads the bytes into an array, and converts it to a String. It prints the available bytes in the file and the string data read from the file. It closes the input stream after reading. Any exceptions are printed via e.getStackTrace().

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)
46 views6 pages

Java Programming Week 2

This Java program reads data from a text file called "output.txt" and displays it. It uses a FileInputStream to open the file, reads the bytes into an array, and converts it to a String. It prints the available bytes in the file and the string data read from the file. It closes the input stream after reading. Any exceptions are printed via e.getStackTrace().

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/ 6

} 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

Answer the following questions.


1. Why does Java use I/O streams?
The java.io package contains nearly every class you might ever need to perform input and
output (I/O) in Java. All these streams represent an input source and an output destination. The
stream in the java.io package supports many data such as primitives, object, localized characters,
etc

2. How do you write an InputStream in Java?


InputStream = Byte stream uses InputStream and OutputStream classes for input and output
operation.

3. What is FileOutputStream in Java?


The Java.io.FileOutputStream class is an output stream for writing data to a File or to a
FileDescriptor.

Activity 2(Answers)
1.A
2.A
3.B
4.A
5.A
Princess Maan M. Arellano Java Programming
Grade 11- App Developer Week 3-4/Quarter 4

import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Main {


StringBuilder sb = new
StringBuilder();
String strLine = "";
DateFormat formatter = new
SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date tdy= new Date();
JFrame f;
Main(){
f=new JFrame("Example");
final JLabel label0 = new
JLabel();
final JLabel label = new
JLabel();
final JLabel label2 = new
JLabel();
final JLabel label3 = new
JLabel();

label0.setText("Enter transaction number");


label0.setBounds(30,5, 200,60);
JTextField t1;
t1=new JTextField("12345");
t1.setBounds(30,45, 190,20);
DefaultListModel<String> l1 = new DefaultListModel<>();
JList<String> list = new JList<>(l1);
JScrollPane jsp=new JScrollPane(list);
jsp.setBounds(30, 215, 280, 150);
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(300,290);
label2.setHorizontalAlignment(JLabel.CENTER);
label2.setSize(300,350);
label3.setText("Enter Amount");
label3.setBounds(30,45,200,60);
JTextField t2;
t2=new JTextField("15800");
t2.setBounds(30,85,190,20);
DefaultListModel<String> l3 = new DefaultListModel<>();
JList<String> Twist=new JList<>(l3);
JScrollPane gsp=new JScrollPane(Twist);
gsp.setBounds(30, 215, 280, 150);
JButton b=new JButton("Show");
b.setBounds(250, 110,80,20);
JButton b2=new JButton("Save");
b2.setBounds(30,195,100,20);
String software[]={"Deposite","Withdraw"};
final JComboBox cb=new JComboBox(software);
cb.setBounds(30, 110,190,20);
f.add(label0); f.add(t1);f.add(cb); f.add(label); f.add(label2); f.add(b); f.add(b2); f.add(jsp);
f.add(label3); f.add(t2); f.add(gsp)
; f.setLayout(null);
f.setSize(350,500)
; f.setVisible(true); b.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) {
String txt1 = null;
String data = "Selected: " + cb.getItemAt(cb.getSelectedIndex());
label.setText(data);
if (cb.getItemAt(cb.getSelectedIndex())=="Deposit") {
txt1="Deposit 8500";
}
if (cb.getItemAt(cb.getSelectedIndex())=="Withdraw") {
txt1="Withdraw 15800";
}
label2.setText(txt1 +" "+formatter.format(tdy));
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
String filename= "mydata.txt"; FileWriter fw = new FileWriter(filename,true);
//appends the string to the file
String data0= t1.getText();
String data1= " "+cb.getItemAt(cb.getSelectedIndex());
String data2= label2.getText();
fw.write(data0);
fw.write(" ");
fw.write(data1);
fw.write(" ");
fw.write(data2);
fw.write("\n");
fw.close();
JOptionPane.showMessageDialog(f, "Data Saved Successfully");
BufferedReader br = new BufferedReader(new FileReader("mydata.txt"));
//read the file content
while (strLine != null)
{
sb.append(strLine);
sb.append(System.lineSeparator());
strLine = br.readLine();
l1.addElement(strLine);
}
br.close();
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}
});
} public static void main(String[] args) { new Main();
}
}
Answer the following questions.
1. What is Java Swing used for?
Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building
optimized window-based applications. It is a part of the JFC (Java Foundation Classes).
2. Why do we use import java util.*?
Java. util package contains the collections framework, legacy collection classes, event model,
date and time facilities, internationalization, and miscellaneous utility classes. This reference
will take you through simple and practical methods available in java.

Activity 2(Answers)
1.A
2.C
3.D
4.B
5.B

You might also like