[go: up one dir, main page]

0% found this document useful (0 votes)
5 views5 pages

IO-converted

The document provides an overview of Java's IO classes, specifically BufferedReader and BufferedWriter, detailing their constructors and methods for reading and writing data. It includes example programs demonstrating how to use these classes for file input/output and user input. Additionally, it briefly mentions the Scanner class and the Console class for reading input from the console.

Uploaded by

rishavthakkar02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

IO-converted

The document provides an overview of Java's IO classes, specifically BufferedReader and BufferedWriter, detailing their constructors and methods for reading and writing data. It includes example programs demonstrating how to use these classes for file input/output and user input. Additionally, it briefly mentions the Scanner class and the Console class for reading input from the console.

Uploaded by

rishavthakkar02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java-IO

Buffered Reader

public class BufferedReader extends Reader

Constructor Description

BufferedReader(Reader rd) It is used to create a buffered character input stream that


uses the default size for an input buffer.

BufferedReader(Reader rd, int It is used to create a buffered character input stream that
size) uses the specified size for an input buffer.

Methods

Method Description

int read() It is used for reading a single character.

String readLine() It is used for reading a line of text.

boolean ready() It is used to test whether the input stream is ready to be read.
Java-IO
Program1-file

import java.io.*;
public class BufferedReaderExample
{
public static void main(String args[])throws Exception
{
FileReader fr=new FileReader("D:\\ABC.txt");
BufferedReader br=new BufferedReader(fr);

int i;
while((i=br.read())!=-1){
System.out.print((char)i);
}
br.close();
fr.close();
}
}

Program2- user input

import java.io.*;
public class BufferedReaderExample{
public static void main(String args[]) throws Exception{

InputStreamReader r=new InputStreamReader(System.in);


BufferedReader br=new BufferedReader(r);
System.out.println("Enter your name");
String name=br.readLine();
System.out.println("HIIII "+name);
}
}
Java-IO
Java Scanner class
Scanner in = new Scanner(System.in);
Scanner in = new Scanner(file object);
Methods in Scanner class
int nextInt()
String nextLine()
String next()
boolean hasNextInt()

Example Program1

import java.util.*;
class Scanner1{
public static void main(String args[])
{
Scanner1 sc = new Scanner1(System.in);
String name, age;
System.out.println("Enter Your Name :");
name=sc.nextLine();
System.out.println("Your Name is :"+name);
System.out.println("Enter Your age :");
age=sc.nextInt();
System.out.println("Your age is :"+ age);
}
}
Program2

import java.io.File;
import java.util.Scanner;
class Main
{
public static void main(String[] args) throws Exception
{
File file = new File("doc.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine())
{
System.out.println(sc.nextLine());
}
} }
Java-IO
Java Console
The Java Console class is be used to get input from console. It provides methods to read
texts and passwords. If you read password using Console class, it will not be displayed to
the user.

import java.io.Console;

class ReadStringTest{

public static void main(String args[]){

Console c= System.console();

System.out.println("Enter your name: ");

String n=c.readLine();

System.out.println("Welcome "+n);

Class BufferedWriter

public class BufferedWriter extends Writer

It writes text to a character-output stream, buffering characters so as to provide for the


efficient writing of single characters, arrays, and strings. The buffer size may be specified, or
the default size may be accepted. The default is large enough for most purposes.

Constructor

BufferedWriter(Writer out)-Creates a buffered character-output stream that uses a default-


sized output buffer.

BufferedWriter(Writer out, int sz)-Creates a new buffered character-output stream that


uses an output buffer of the given size.

Methods

Void write(int c) - Writes a single character.

Void write(String s, int off, int len) - Writes a portion of a String.


Java-IO
Program-BufferedWriter example

public class BufferedWriterExample {


public static void main(String[] args) throws Exception {
FileWriter writer = new FileWriter("D:\\ABC.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Hello");
buffer.close();
System.out.println("Success");
}
}
Program -Writing File
import java.io.*;
public class Main throws Exception
{
public static void main(String[] args)
{

FileWriter fr = new FileWriter("ABC.txt");

// Initialing BufferedWriter
BufferedWriter bw = new BufferedWriter(fr);
System.out.println("Buffered Writer start writing :)");

// Use of write() method to write the value in 'ABC' file

bw.write("hello"); //String
int i;
for(i=65;i<91;i++)
bw.write(i); //op-A-Z

// Closing BufferWriter to end operation


bw.close();
System.out.println("Written successfully");

}
}

You might also like