//File:	IntFiles.java
import java.util.StringTokenizer;
import java.io.*;
public class IntFiles
{
	//			INPUT FUNCTIONS
	
	
	//The following function inputs a list of ints from fileName
	//and returns the number of items in the list
	public static int readIn (String fileName, int [] list)
	{
    		int count=0;
    		try
    		{
			//The FileReader converts byte stream to char stream
			FileReader fr = new FileReader (fileName);
			//The BufferedReader enables efficient buffering of stream
			BufferedReader inFile = new BufferedReader (fr);
     			String line = inFile.readLine(); //readLine() is in BufferedReader class
     			
     			while (line != null)
     			{
     				try
     				{
     					list [count] = Integer.parseInt (line);
     				}
     				catch (NumberFormatException e)
     				{
     					System.out.println ("Error in input. ");
     					System.out.println (line);
     				}
     				count++;
     				line = inFile.readLine();
     			}
     			
     			inFile.close(); //not invoked if throws exception
     				
    		}
    		catch (FileNotFoundException e)
    		{
    			System.out.println ("The file " + fileName + " was not found.");
    		}
    		catch (IOException e)
    		{
    		}
    		
    		return count;
  	}
  	
  	
	//The following function inputs a table of ints, 3 numbers per line,
	//from fileName and returns the number of rows
  	public static int readInTable (String fileName, int [] [] list)
	{
    		int row=0;
    		int col=0;
    		String s;
    		try
    		{
			//The FileReader converts byte stream to char stream
			FileReader fr = new FileReader (fileName);
			//The BufferedReader enables efficient buffering of stream
			BufferedReader inFile = new BufferedReader (fr);
     			String line = inFile.readLine(); //readLine() is in BufferedReader class
     			
     			while (line != null)
     			{
     				StringTokenizer tokenizer = new StringTokenizer (line);
     				for (int k=0; k<3; k++)
     				{
     					s=tokenizer.nextToken();
     					list [row] [k] = Integer.parseInt (s);
     				}
     				row++;
     				line = inFile.readLine();
     			}
     			
     			inFile.close(); //not invoked if throws exception
     				
    		}
    		catch (FileNotFoundException e)
    		{
    			System.out.println ("The file " + fileName + " was not found.");
    		}
    		catch (IOException e)
    		{
      			System.out.println (e);
    		}
    		return row;
  	}
  	
	
	//The following function inputs a table of ints from fileName
	//and returns the number of rows and columns
  	public static int [] readTable (String fileName, int [] [] list)
	{
    		int row=0;
    		int col=0;
    		String s;
    		try
    		{
			//The FileReader converts byte stream to char stream
			FileReader fr = new FileReader (fileName);
			//The BufferedReader enables efficient buffering of stream
			BufferedReader inFile = new BufferedReader (fr);
     			String line = inFile.readLine(); //readLine() is in BufferedReader class
     			
     			while (line != null)
     			{
     				StringTokenizer tokenizer = new StringTokenizer (line);
     				int ncols = tokenizer.countTokens();
     				//countTokens() is member function in StringTokenizer class
     				//It's perfect for this application.
     				
     				for (col=0; col<ncols; col++)
     				{
     					s=tokenizer.nextToken();
     					list [row] [col] = Integer.parseInt (s);
     				}
     				row++;
     				line = inFile.readLine();
     			}
     			
     			inFile.close(); //not invoked if throws exception
     				
    		}
    		catch (FileNotFoundException e)
    		{
    			System.out.println ("The file " + fileName + " was not found.");
    		}
    		catch (IOException e)
    		{
      			System.out.println (e);
    		}
    		
    		int [] temp = new int [2];
    		temp[0]=row;
    		temp[1]=col;
    		
    		return temp;
  	}
  	
  	
  	//			OUTPUT FUNCTIONS
  	
  	//The following function outputs a list of n ints, one number per line,
	//to fileName
	public static void writeOut (String fileName, int [] list, int n)
	{
    		try
    		{
			FileWriter fr = new FileWriter (fileName);
			BufferedWriter buffer = new BufferedWriter (fr);
			PrintWriter out = new PrintWriter (buffer);

			for (int row=0; row<n; row++)
			{
				out.println (list[row]);
			}
			
			out.close();
		}
		catch (IOException e)
		{
		}
  	}
  	
  	
  	//The following function outputs a table of ints containing r rows and c columns
  	//to fileName
  	public static void writeOutTable (String fileName, int [] [] list, int r, int c)
	{
    		try
    		{
			FileWriter fr = new FileWriter (fileName);
			BufferedWriter buffer = new BufferedWriter (fr);
			PrintWriter out = new PrintWriter (buffer);

			for (int row=0; row<r; row++)
			{
				for (int col=0; col<c; col++)
				{
					out.print (list [row] [col] + "\t");
				}
				out.println();
			}
			
			out.close();
		}
		catch (IOException e)
		{
		}
	}
}