Question

In: Computer Science

How to write IO program in java?

How to write IO program in java?

Solutions

Expert Solution

Java brings different Streams with its I/O package that helps the user to perform all the input-output operations.

These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations.

Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide which are also most common in use:

  1. System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
  2. System.out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen.

    Here is a list of the various print functions that we use to output statements:

  • print(): This method in Java is used to display a text on the console.

  • println(): This method in Java is also used to display a text on the console. It prints the text on the console and the cursor moves to the start of the next line at the console.

3.System.err: This is the standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device.

code:

import java.io.IOException;
import java.io.InputStreamReader;

public class fileStreamTest
{
    public static void main(String args[]) throws IOException
    {
        InputStreamReader inp = null;

        inp = new InputStreamReader(System.in);

        System.out.println("Enter characters according your choice " + " and Enter '0' to         
quit.");
        char c;
        do {
            c = (char)inp.read();
            System.out.println(c);
        } while (c != '0');
    }
}

output:

Enter characters according your choice and Enter '0' to quit.
my name is Bob
m
y

n
a
m
e

i
s

B
o
b


0
0

Process finished with exit code 0

streams can be divided into two primary classes:


1) Input Stream: These streams are used to read data that must be taken as an input from a file or any peripheral device.

For eg., FileInputStream, BufferedInputStream etc.

2) Output Stream: These streams are used to write data as outputs into an array or file or any output peripheral device.

For eg., FileOutputStream , ByteArrayOutputStream etc.

code

import java.io.FileReader;
import java.io.IOException;
public class fileStreamTest
{
    public static void main(String args[]) throws IOException
    {
        FileReader sourceStream = null;
        try {
            sourceStream = new FileReader("test.txt");   //read test.text file
            int temp;
            while ((temp = sourceStream.read())!= -1)
                System.out.println((char)temp);
        }
        finally {     
         
            if (sourceStream != null) sourceStream.close();
        }
    }
}

output:

M
y

n
a
m
e

i
s

A
l
i
c
e
code(SS):

output(SS):

we can various program using IO.


Related Solutions

In a Java program, how could I write a program that can assign values that would...
In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
This program must be done in JAVA. Write a program to monitor the flow of an...
This program must be done in JAVA. Write a program to monitor the flow of an item into an out of a warehouse. The warehouse has numerous deliveries and shipments for this item (a widget) during the time period covered. A shipment out (an order) is billed at a profit of 50% over the cost of the widget. Unfortunately each incoming shipment may have a different cost associated with it. The accountants of the firm have instituted a last-in, first...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT