GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - FileReader Class
Selected Reading
Java - FileReader ClassIntroductionThe Java FileReader class is a convenience class for reading character files.Following are the important points about FileReader −
Class declarationFollowing is the declaration for Java.io.FileReader class − public class FileReader extends InputStreamReader FieldFollowing are the fields for Java.io.FileReader class −
Class constructors
Once you have FileReader object in hand then there is a list of helper methods which can be used to manipulate the files.
Example - Using FileReader(String fileName) to Read a FileThe following example shows the usage of Java FileReader class. We've created a File reference with name Hello1.txt to be created in current directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed. FileReaderDemo.javapackage com.tutorialspoint;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String args[])throws IOException {
File file = new File("Hello1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();
// Creates a FileReader Object
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
fr.close();
}
}
OutputThis is an example Example - Using FileReader(String fileName) to Read a FileThe following example shows the usage of Java FileReader class. FileReaderDemo.javapackage com.tutorialspoint;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) {
String fileName = "example.txt"; // File to be read
try (FileReader fileReader = new FileReader(fileName)) {
int character;
while ((character = fileReader.read()) != -1) {
System.out.print((char) character); // Print each character
}
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Output(if example.txt contains "Hello, World!")Hello, World! Explanation
Example - Read a File Character by CharacterThe following example shows the usage of Java FileReader class. We've created a File reference with name Hello1.txt to be created in the current directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed.
package com.tutorialspoint;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String args[])throws IOException {
File file = new File("Hello1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();
// Creates a FileReader Object
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
fr.close();
}
}
OutputThis is an example Advertisements
| ||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |