Java provides java.io package inwards JDK API for reading File inwards Java from File system e.g. C:\ or D:\ get inwards Windows or whatever other directory inwards UNIX. First, y'all tin purpose FileInputStream to opened upward a file for reading. FileInputStream takes a String parameter which is a path for the file y'all desire to read. Be careful land specifying File path equally path separator is dissimilar on Window as well as UNIX. Windows uses dorsum slash land UNIX uses forwards slash equally a path separator. By the way, Java supports forwards slash inwards Windows, It way if purpose forwards slash your programme volition operate on both Windows as well as UNIX. Alternatively, y'all tin too larn PATH separator from System property path.separator. In this Java tutorial, nosotros volition code representative of How to read File inwards Java.
Java programme to read text file inwards Java
read String from InputStream inwards Java, nosotros own got used BufferedReader to read file trace of piece of occupation past times line for amend performance.
Program to read a text file inwards Java
/**
*
* Java programme to demonstrate how to read text file inwards Java.
* Java API provides FileInputStream to opened upward file for reading.
* for improved functioning its advised that purpose Reader for reading contents
* By using InputStreamReader y'all tin convert an InputStream into Reader.
* BufferedReader provides amend functioning land reading file inwards Java.
* @author jdk67.blogspot.com
*/
public class FileReaderExample {
public static void main(String args[]) {
try {
//opening file for reading inwards Java
FileInputStream file = new FileInputStream("C:/test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
//reading file content trace of piece of occupation past times line
String trace of piece of occupation = reader.readLine();
while(line != null){
System.out.println(line);
trace of piece of occupation = reader.readLine();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(CollectionTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CollectionTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Output:
Java programming linguistic communication has fantabulous back upward for File IO.
Java API provides several ways to read files from FileSystem.
From Java 7 onwards a new File API has been introduced which provides to a greater extent than features as well as improved performance.
*
* Java programme to demonstrate how to read text file inwards Java.
* Java API provides FileInputStream to opened upward file for reading.
* for improved functioning its advised that purpose Reader for reading contents
* By using InputStreamReader y'all tin convert an InputStream into Reader.
* BufferedReader provides amend functioning land reading file inwards Java.
* @author jdk67.blogspot.com
*/
public class FileReaderExample {
public static void main(String args[]) {
try {
//opening file for reading inwards Java
FileInputStream file = new FileInputStream("C:/test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
//reading file content trace of piece of occupation past times line
String trace of piece of occupation = reader.readLine();
while(line != null){
System.out.println(line);
trace of piece of occupation = reader.readLine();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(CollectionTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CollectionTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Output:
Java programming linguistic communication has fantabulous back upward for File IO.
Java API provides several ways to read files from FileSystem.
From Java 7 onwards a new File API has been introduced which provides to a greater extent than features as well as improved performance.
If y'all await at this program, nosotros own got commencement opened FileInputStream pointing to file ("C:/test.txt") and thence afterward read it.That's all on How to read File inwards Java.
Further Learning
Complete Java Masterclass
How to convert Date to String inwards Java
No comments:
Post a Comment