Problem - How you can read a text file from a directory ? Solution : java.util.Scanner which can parse primitive types and strings using regular expressions. Program :Step 1 - Coding Create a text file c:/sunilos/ReadFileScanner.java and copy below contents.import java.io.FileReader; import java.util.Scanner; /** * A program to read a file with help of Scanner class. */ public class ReadFileScanner { public static void main(String[] args) throws Exception{ FileReader reader = new FileReader("c:/sunilos/sunilos.txt"); //Scanner sc = new Scanner("c:/sunilos/sunilos.txt"); Scanner sc = new Scanner(reader); while(sc.hasNext()){ System.out.println(sc.nextLine()); } } } Step 2 - Deployment
Step 3 - Testing
OutputA software services and corporate training company specializing in Distributed Object Oriented and Client Server Technologies with a strong background of J2EE. <<Previous | Next>> |