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...
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...
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 java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT