Aside from the Scanner class, which of the following classes can be used for text-file file input?
- AObjectInputStream
- BBufferedReader
- CStringTokenizer
- DNone of these
BufferedReader wrapped around a FileReader reads text files line by line.
MCQs, short and long questions from lecture 8.Lecture 8 ki MCQs, short aur long sawal.
6 MCQs · 4 short · 2 long questions · 6 MCQs from past papers6 MCQs · 4 short · 2 long sawal · 6 MCQs past papers se
Aside from the Scanner class, which of the following classes can be used for text-file file input?
BufferedReader wrapped around a FileReader reads text files line by line.
Which of the following package needs to be import while handling files?
File and stream classes are in java.io.
Which of the following stream is a filter stream?
BufferedReader wraps another stream and adds buffering, so it is a filter stream.
Based on functionality, the streams can be categorized as________
By function, streams are node streams (connected to a source/sink) or filter streams (wrapping another stream).
Which of the following stream is a Node stream?
FileWriter connects directly to a file (data source), so it is a node stream; the others are filters.
Which class can be used for text file input besides Scanner?
BufferedReader (wrapping a FileReader) reads text files line by line.
No MCQs match this filter.Is filter mein koi MCQ nahi.
How can we use FileReader and FileWriter streams to print on console?
Attach them to the standard file descriptors instead of a file name: new FileReader(FileDescriptor.in) reads from the keyboard (console input) and new FileWriter(FileDescriptor.out) writes to the console. They are character streams, so they read and write characters. Example: FileWriter fw = new FileWriter(FileDescriptor.out); fw.write("Hello"); fw.flush();
Character oriented vs. byte oriented stream?
Byte-oriented streams (classes with "Stream" in the name, such as FileInputStream/FileOutputStream) read and write data as 8-bit bytes and suit any binary data such as images. Character-oriented streams (classes with "Reader" or "Writer" in the name, such as FileReader/FileWriter) read and write 16-bit Unicode characters and handle character encoding, so they are suited to text.
Difference between character oriented and byte oriented streams
Byte-oriented streams (InputStream/OutputStream and their subclasses, names end with "Stream") read and write data 8-bit byte by byte and suit any binary data. Character-oriented streams (Reader/Writer subclasses) read and write 16-bit Unicode characters and handle character encoding, so they suit text.
Diff b/w Input/output streams class hierarchy and Read/Write class hierarchy?
The InputStream/OutputStream hierarchy is byte-oriented: InputStream and OutputStream are the abstract classes at its top and its classes read/write bytes. The Reader/Writer hierarchy is character-oriented: Reader and Writer are the abstract top classes and its classes read/write Unicode characters (text).
How streams handle in Java
Java does I/O with streams, ordered sequences of data from a source (input stream) or to a sink (output stream); the classes are in java.io. By data type they are byte-oriented (InputStream/OutputStream classes) or character-oriented (Reader/Writer classes). By function they are node streams, which connect directly to the source or sink (for example FileReader fr = new FileReader("input.txt");), and filter streams, which wrap another stream to add features such as buffering (for example BufferedReader br = new BufferedReader(fr);). The program opens the stream, reads or writes in a loop, handles IOException and closes the stream.
Following are the contents of file1.txt My name is ali. My roll no is bc020400001. I am studying java. Java is very easy. You are required to write a code which will read this file and create a new file in which lines would be swapped in such a way that first line would be swapped with 2nd and 3rd with 4th and so on. New file should look like: My roll no is bc020400001. My name is ali. Java is very easy. I am studying java. (5 Marks file handling, 5 Marks swapping)
import java.io.*; import java.util.*; public class SwapLines { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader("file1.txt")); ArrayList<String> lines = new ArrayList<String>(); String line; while ((line = br.readLine()) != null) { lines.add(line); } br.close(); PrintWriter pw = new PrintWriter(new FileWriter("file2.txt")); for (int i = 0; i < lines.size(); i += 2) { if (i + 1 < lines.size()) { pw.println(lines.get(i + 1)); // write the 2nd line of the pair first } pw.println(lines.get(i)); } pw.close(); } catch (IOException e) { System.out.println(e); } } }