/**
 * Read and print to the screen lines contained in
 * a UTF-8 text file specified in args[0].
 */
import java.io.*;

public class UTF8FileInputDemo
{
    public static final String ENCODING = "UTF-8";
    
    public static void main(String[] args)
    {
        
        // make sure number of arguments is correct
        if (args.length != 1)
        {
            System.out.println("Usage: java UTF8FileInputDemo <filename>");
            System.exit(0);
        }
        
        String fileName = args[0];
        BufferedReader src;
        String line;
        
        System.out.println("The file " + fileName + " contains the following lines:");
        
        try
        {
            // open file for reading, utf8
            src = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), ENCODING));
            
            // read input stream line-by-line and print each line to screen
            while ((line = src.readLine()) != null)
            {
                System.out.println(line);
            }
            
            // don't forget to close the file
            src.close();
        }
        catch (FileNotFoundException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);        
        }
        catch (UnsupportedEncodingException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
    }
}