Thread: File I/O

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    3
    Rep Power
    0

    File I/O


    Please someone help me.
    I suppose to use file input and output. Now I am able to read file, but I can't get print out the data to the output file. I made a file, but nothing there..... I have no idea what to do... also I have to sort the data by last name....
    this is my code.

    import java.io.*;
    import java.util.*; //for Stringtokenizer class

    class Child
    {
    String name;
    String surname;
    int age;
    String group;

    public Child(String n, String sn, String gp, int a)
    {
    name = new String(n);
    surname = new String(sn);
    group = new String(gp);
    age = a;
    }

    public String toString()
    {
    return( name + " " + surname + "\n"
    +"Group: " + group + "\n"
    + "Age: " + age );
    }

    } //end Child class

    class Kindergarten
    {
    public static void main(String[] args) throws IOException
    {
    for( int i = 0; i < args.length; i ++ )
    listfile( args[i] );
    }

    public static void listfile( String fileName ) throws IOException
    {
    Child[] children = new Child[3];
    int nchildren = 0;

    BufferedReader fileIn = null;
    PrintWriter fileOut ;
    StringTokenizer info;
    String inputLine;

    try
    {
    fileIn = new BufferedReader( new FileReader(fileName) );
    fileOut = new PrintWriter( new FileWriter("Name.txt" ));

    fileOut.println( " Name List ");

    }
    catch(IOException e )
    {
    System.out.println(e);
    System.exit(1);
    }


    while ( nchildren < 3)
    {
    try
    {
    inputLine = fileIn.readLine();
    info = new StringTokenizer(inputLine);
    if(info.countTokens() != 4)
    throw new IOException();
    String nm = info.nextToken();
    String sn = info.nextToken();
    String gp = info.nextToken();
    int a = Integer.parseInt(info.nextToken());
    children[nchildren] = new Child(nm,sn,gp,a);
    nchildren++;

    }
    catch ( Exception e)
    {
    System.out.println(e);
    System.exit(1);
    }
    }

    for(int j = 0; j < 3; j++)
    {
    System.out.println();
    System.out.println(children[j] + "\n");
    }
    }
    } //end Kindergarten
    I know I am missig some code, but I can't even get the words "Name List".

    I have no idea, so please help me.
    thank you
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I assume you read my reply to your other post about the sorting. You are going to have to implement a custom sort algorithm using the Arrays class. The documentation has directions.

    http://java.sun.com/j2se/1.4/docs/ap...il.Comparator) look at the sort(Object[] a, Comparator c) method

    http://java.sun.com/j2se/1.4/docs/ap...omparator.html


    The reason you are getting no output to the Name.txt file is because you are not flushing or closing the stream. The program ends and the contents you have written are still in a buffer and never written. Incidentally, you never close the input stream either, which is very bad form.

    When you are sure you are done with the output stream call output.flush() and ouput.close(). When you are sure you are done with the input stream make sure and close it also by calling input.close().
    Last edited by Nemi; January 31st, 2003 at 01:51 PM.

IMN logo majestic logo threadwatch logo seochat tools logo