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

Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
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 program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
This has to be in java and will be uploaded onto zybooks. Write a Java program...
This has to be in java and will be uploaded onto zybooks. Write a Java program that asks the user for a date entered as 4 integers: dayNumber monthNumber date year. Where: dayNumber An integer from 1-7, where 1 = Sunday, 2 = Monday, ..., 7 = Saturday monthNumber An integer from 1-12, where 1 = January, 2 = February, ..., 12 = December date An integer from 1-31 representing the date. year An integer representing the year. Your prompt...
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...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT