Dude look at your while loop. While loop would break when line is null. So
when you write to the file the line obviously null.

Here is one way you can do the stuff

try {
    String username = "tree_l";
    String group = "foreign";
    File grp = new File("/home/ejantk_a/temp/groups");
    FileReader raf = new FileReader(grp);
    BufferedReader in = new BufferedReader(raf);
    String line;

    /** Code added **/
    StringBuffer buffer = new StringBuffer();

    //Search for group name, and replace with group name plus username.
    while((line = in.readLine()) != null) {
         Perl5Util perl = new Perl5Util();
         if(perl.match("/" + group + "/", line)) {
            line = perl.substitute("s/" + group + "/($1) " + username + ",
/", line);
          }
           buffer.append(line).append(File.separatorChar);
    }

    //Write to File Object.
    FileWriter fw = new FileWriter(grp);
    PrintWriter pw = new PrintWriter(fw);
    pw.println(line);
    fw.close();

    } catch (IOException e) {
          System.out.println("Error -- " + e.toString());
    }


-----Original Message-----
From: Ajay Ejantkar [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 05, 2002 3:18 PM
To: [EMAIL PROTECTED]
Subject: edit file


I am attempting to edit a file called groups by adding a username to it. My
code reads the file with filereader. Each line is readin, and searched for
the group (foreign in this case). Then "foreign username," is substituted
into the string for foreign. Then the file is written. The result is the
groups file shows just null in it. My Code is below, any help on this would
be greatly appreciated. Thanks

try {
    String username = "tree_l";
    String group = "foreign";
    File grp = new File("/home/ejantk_a/temp/groups");
    FileReader raf = new FileReader(grp);
    BufferedReader in = new BufferedReader(raf);
    String line;

    //Search for group name, and replace with group name plus username.
    while((line = in.readLine()) != null) {
         Perl5Util perl = new Perl5Util();
         if(perl.match("/" + group + "/", line)) {
            line = perl.substitute("s/" + group + "/($1) " + username + ",
/", line);
          }
    }

    //Write to File Object.
    FileWriter fw = new FileWriter(grp);
    PrintWriter pw = new PrintWriter(fw);
    pw.println(line);
    fw.close();

    } catch (IOException e) {
          System.out.println("Error -- " + e.toString());
    }

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to