Reading from a text file using Java

[info]You need to have JDK/Eclipse installed on your PC[/info]

1. Open your Eclipse and remember the location of your workspace

2. Create a new Project : MyReadFileProj  [ Click here for steps to create a new project ]

4. Right Click on MyReadFileProj : New -> File

4, Type the name of the text file as :  data.txt

A,B,222222
C,D,223333
F,T,999999

5. Create a class called : MyReadFile

import java.io.*;
import java.util.*;

public class MyReadFile {

	public static void main(String[] args) {
		try{
			File f=new File("data.txt");
			Scanner s=new Scanner(f);
			while(s.hasNextLine()){
				String line=s.nextLine();
				System.out.println(line);
			}
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
}

6. Click on the Green button to compile and run your program.

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *