954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

read external text doc

hi, as far as I know if i wanna read external text document, i can either do:

BufferedReader reader = new BufferedReader(new FileReader("xxx.txt")); 

or 

Scanner reader = new Scanner(new File("xxx.txt"));


my first question is, what is the difference between BufferedReader and Scanner? And what is the difference between FileReader("xxx.txt") and just File("xxx.txt") Which one do most of the people use the most? I would rather know and stick with that one.

And I used Scanner one and it couldn't find the file. (I placed the text file in eclipse's src, bin and outside too // everywhere.. and it still cannot be found) Please give some suggestion. thanks.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;


public class Namebody extends Name
{
	public static void main(String[] args)
	{
		Name nameHolder[] = new Name[350];
		
		int inID;
		String inLastname, inFirstname,inPhone;
		double inPrice;
	
		int count=0;
	
	
		Scanner reader = null;
	
	
		String filename="names.txt";
	
		try
		{
			reader = new Scanner(new FileReader(filename));
		}
	
		catch(FileNotFoundException e)
		{
			System.out.println("File not found!");
			System.exit(0);
		}
	
		while(reader.hasNextLine())
		{
			inID=reader.nextInt();
			inLastname= reader.next();
			inFirstname= reader.next();
			inPhone= reader.next();
			inPrice= reader.nextDouble();
		
			nameHolder[count].setName(inID, inLastname, inFirstname, inPhone, inPrice);
			count++;
		}
		
		for(int i=0; i<=count; i++)
		{
			nameHolder[i].output();
		}
		
	
	}
}
k2k
Posting Whiz
352 posts since Nov 2007
Reputation Points: 15
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You