answersLogoWhite

0

What is FileReader?

Updated: 8/9/2019
User Avatar

Wiki User

12y ago

Best Answer

In java FileReader is a class which is meant for reading streams of characters.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is FileReader?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between File Reader and FileInputStream in java?

FileReader used to read the character stream in the file.i.e a file that contanis only the character means FileReader is the choice to read the file.On the other hand if the file contains image,byte like raw data format means FileInputStream is the choice to read the data in the file,.


To read data which is superior 1. input stream 2. file reader?

First of all, these two classes are on different levels of abstraction. An InputStream is used for reading any stream of bytes, while a FileReader is used to read characters from a file. If you want to ask between a FileInputStream and a FileReader, then we need to look at what type of data you are reading. If you're reading plain-text file, for example, you want to use a FileReader because it was designed to read in characters. For other types of data, the FileInputStream would be better, as it is used to read in generic streams of bytes from a file.


What is BufferedReader in Java Programming?

BufferedReader is a class used to read text from charater-input stream and buffering characters which reads characters, arrays, and new lines. In general, each read request made using "Reader" class, causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example, BufferedReader in = new BufferedReader(new FileReader("foo.in"));


Explain about exception handling with multiple exception?

Exception handling in Java is done through a try-catch-finally block. The "try" block of code is where you put the code which may throw an exception. The "catch" block of code is where you put the code which will execute after an exception is thrown in the try block. This is often used to display an error message, or to mitigate problems caused by the exception. The "finally" block is where you put code that you want to execute after the try and catch blocks have been processed. // example code for catching exception while reading from a file try { // this line of code can throw a FileNotFoundException FileReader in = new FileReader("myfile.txt"); // this line of code can throw an IOException in.read(); } catch(FileNotFoundException ex) { // catch first exception type System.err.println("Cannot find myfile.txt!"); } catch(IOException ex) { //catch second exception type System.err.println("Cannot read from myfile.txt!"); } finally { // no matter what we want to close the file, so do it here // however, this line can also cause an exception, so we need to catch that, too try { in.close(); catch(IOException ex) { // not much we can do about an exception that occurs here } }


Write a simple java program using exceptions?

Here's a Hello World application: import static java.lang.System.out; class HelloWorld { public static void main(string[] arguments) { out.println("Hello world!") //this will print out the words 'Hello world!' to the screen } //End of main method } //End of class HelloWorld


How do you read from an external file in java?

The class I find useful is the FileReader class in java.io. It allows you to read an external file in your program.For instance,Consider a file input.txt:HelloHow are you?1 2 3 4 5You can access it by:FileReader read = new FileReader("input.txt"); // pass the file name as a String//now the read is the file//can scan from the file using ScannerScanner scan = new Scanner(read);System.out.println( scan.nextLine()); // prints HelloSystem.out.println( scan.nextLine()); // prints How are you?while(scan.hasNextInt())System.out.print(scan.nextInt() + " "); // prints 1 2 3 4 5


What are input processes for the Currency Conversion project?

It really depends where are you inputs coming from.From the keyboard by prompting the user: import java.util.Scanner;System.out.print("input: ");Scanner scan = new Scanner(System.in);int input = scan.nextInt();// Do your converting with the inputFrom a file: import java.io.FileReader;import java.util.Scanner;Scanner scan = new Scanner(new FileReader("inputFile.dat"); // the input data fileint input = scan.nextInt(); //similar to reading from keyboard since the file is now your input source//Do your convertingscan.close(); // remember to close your files that you opened, good Java conventions


What is bufferedReader in advance java?

BufferedReader Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example, BufferedReader in = new BufferedReader(new FileReader("foo.in")); will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.


What is the use of BufferedReader in Java programming?

The BufferedReader class provides buffering to your Reader's. Buffering can speed up IO quite a bit. Rather than read one character at a time from the network or disk, you read a larger block at a time. This is typically much faster, especially for disk access and larger data amounts. The main difference between BufferedReader and BufferedInputStream is that Reader's work on characters (text), wheres InputStream's works on raw bytes. To add buffering to your Reader's simply wrap them in a BufferedReader. Here is how that looks: Reader input = new BufferedReader( new FileReader("c:\\data\\input-file.txt"));


Explain different stream classes in Java?

Byte StreamsIf a program needs to read/write bytes (8-bit data), it could use one of the subclasses of the InputStream or OutputStream respectively. The example below shows how to use the class FileInputStream to read a file named abc.dat. This code snippet prints each byte's value separated with white spaces. Byte values are represented by integers from 0 to 255, and if the read() method returns -1, this indicates the end of the stream.import java.io.FileInputStream;import java.io.IOException;public class ReadingBytes {public static void main(String[] args) { FileInputStream myFile = null; try {myFile = new FileInputStream("c:\\abc.dat"); // open the streamboolean eof = false;while (!eof) {int byteValue = myFile.read(); // read the streamSystem.out.println(byteValue);if (byteValue == -1){eof = true;}//myFile.close(); // do not do it here!!!}}catch (IOException e) {System.out.println("Could not read file: " + e.toString());} finally{try{if (myFile!=null){myFile.close(); // close the stream}} catch (Exception e1){e1.printStackTrace();}}}}Please note that the stream is closed in the clause finally. Do not call the method close() inside of the try/catch block right after the file reading is done. In case of exception during the file read, the program would jump over the close() statement and the stream would never be closed!The next code fragment writes into the file xyz.dat using the class FileOutputStream:int somedata[]={56,230,123,43,11,37};FileOutputStream myFile = null;try {myFile = new FileOutputStream("c:\xyz.dat");for (int i = 0; i BufferedInputStream --> DataInputStreamFileInputStream myFile = new FileInputStream("myData.dat");BufferedInputStream buff = new BufferedInputStream(myFile);DataInputStream data = new DataInputStream(buff);try {int num1 = data.readInt();int num2 = data.readInt();float num2 = data.readFloat();float num3 = data.readFloat();float num4 = data.readFloat();double num5 = data.readDouble();} catch (EOFException eof) {...}


How do you solve Usaco Greedy Gift Givers?

/* ID: isurube1 LANG: JAVA TASK: gift1 */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class gift1 { public static void main(String[] args) { try{ BufferedReader fileIn = new BufferedReader(new FileReader("gift1.in")); PrintWriter fileOut = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out"))); try{ int quantity = Integer.parseInt(fileIn.readLine()); String[][] outFile = new String [quantity][2]; for (int i = 0; i < quantity; i++) { outFile [i][0] = fileIn.readLine(); outFile [i][1] = "0"; } for (int i = 1; i <= quantity; i++) { String giver = fileIn.readLine(); String temp = fileIn.readLine(); int money = Integer.parseInt(temp.split(" ")[0]); int receivers = Integer.parseInt(temp.split(" ")[1]); int giversValue = 0; if (money != 0 receivers != 0){ giversValue = (money % receivers) - money; } for (int j = 0; j < outFile.length; j++) { if (outFile[j][0].equals(giver)){ outFile[j][1] = Integer.toString(giversValue + Integer.parseInt(outFile[j][1])); break; } } if(receivers != 0){ money /= receivers; for (int j = 1; j <= receivers; j++) { temp = fileIn.readLine(); for (int k = 0; k < outFile.length; k++) { if (outFile[k][0].equals(temp)){ outFile[k][1] = Integer.toString(money + Integer.parseInt(outFile[k][1])); break; } } } } } for (int i = 0; i < outFile.length; i++) { fileOut.write(outFile[i][0] + " " +outFile[i][1]+"\n"); } } finally { fileIn.close(); fileOut.close(); } } catch (IOException e) { throw new RuntimeException(e); } } }


How do you search the given word in all files from the directory in java?

The program below will search all non-directory files in the starting directory. If you want to search all subdirectories, you'll have to modify it to recurse a bit. Note that this will also only do plain text searches of files. It will most likely not find your string in a MS Word .doc file. Also note that this will not find multiple-line search strings, since it reads and checks one line of the file at a time. { final String dirName; // path of directory you want to search final String str = ""; // string to search for final File dir = new File(dirName); for (File f : dir.listFiles()) { if (!f.isDirectory()) { System.out.println(f + ": " + fileContains(f, str)); } } } // returns true iff f contains str private static final boolean fileContains(final File f, final String str) throws IOException { final BufferedReader in = new BufferedReader(new FileReader(f)); String currentLine; while ((currentLine = in.readLine()) != null) { if (currentLine.contains(str)) { in.close();return true; } } in.close(); return false; }