Saturday, June 16, 2012

CSC435 - INPUT / OUTPUT TEXT FILE


CHAPTER 8 - INPUT / OUTPUT TEXT FILE 

EXAMPLE -



The important thing to remember :


//the classes use included
import java.util.*;
import java.io.*;
import java.util.StringTokenizer;

//the declaration
public static void main (String[] args) throws IOException

//define what file in and what file out
File fileIn = new File ("dorm.dat");
File fileOut1 = new File ("single.out");

// define the file in variable and the file out variable
Scanner in = new Scanner (fileIn);
PrintWriter out_single = new PrintWriter (fileOut1);

//this is how to write to file
out_single.println("Id number\tName\t\tStatus");


// ways to loop the file in
String indata = null;
while (in.hasNext()) {
indata = in.nextLine();
StringTokenizer st = new StringTokenizer(indata, ";");


// don’t forget to close the file
in.close();
out.close();

// error catcher
catch (IOException ioe)
{
System.out.println (ioe.getMessage());
}

catch (Exception ex)
{
System.out.println (ex.getMessage());
}






import java.util.*;
import java.io.*;
import java.util.StringTokenizer;
public class STUDENT
{
    public static void main (String[] args) throws IOException
    {       
       try
       {               
            File fileIn = new File ("dorm.dat");
       
            if (!fileIn.exists())
            {
                System.out.println (“File not exist”);
                System.exit(0);
            }
           
            String studentid,name, status, type;
           
            int total_Single = 0,total_SFreshman = 0,
total_SSenior = 0,total_SJunior = 0,total_SSophomore = 0;
            int total_Double = 0,total_DFreshman = 0,
total_DSenior = 0,total_DJunior = 0,total_DSophomore = 0;
            
           
            File fileOut1 = new File ("single.out");
            File fileOut2 = new File ("double.out");
       
            Scanner in = new Scanner (fileIn);
            PrintWriter out_single = new PrintWriter (fileOut1);
            PrintWriter out_double = new PrintWriter (fileOut2);
           
            out_single.println("Id number\tName\t\tStatus");
            out_double.println("Id number\tName\t\tStatus");
                
            String indata = null;
            while (in.hasNext())
            {
                indata = in.nextLine();
                StringTokenizer st = new StringTokenizer(indata, ";");
               
                studentid = st.nextToken();
                name = st.nextToken();
                status = st.nextToken();
                type = st.nextToken();
               
                if ( type.equalsIgnoreCase("SINGLE") )
                {
                   total_Single++;
                   if ( status.equalsIgnoreCase("SENIOR"))
                        total_SSenior++;
                   else if ( status.equalsIgnoreCase("JUNIOR"))                  
                        total_SJunior++;
                   else if ( status.equalsIgnoreCase("FRESHMAN"))                  
                        total_SFreshman++;
                   else                   
                        total_SSophomore++;
                }
                else
                {
                   total_Double++;
                   if ( status.equalsIgnoreCase("SENIOR"))
                        total_DSenior++;
                   else if ( status.equalsIgnoreCase("JUNIOR"))                  
                        total_DJunior++;
                   else if ( status.equalsIgnoreCase("FRESHMAN"))                  
                        total_DFreshman++;
                   else                   
                        total_DSophomore++;
                }
                   
         }
                     
        
         in.close();
         out.close();
         outPASS.close();
         outFAIL.close();
         System.out.println("Habis Write Files!!!\n\n");
      }
     
      catch (FileNotFoundException fnf)
      {
          System.out.println (fnf.getMessage());
        }
       
      catch (IOException ioe)
      {
          System.out.println (ioe.getMessage());
        }
       
      catch (Exception ex)
      {
          System.out.println (ex.getMessage());
        }
   }
}





0 comments:

Post a Comment

Say whut you want, but be responsible on whut you said...