The Artima Developer Community
Sponsored Link

Java Answers Forum
adding lines in a file

1 reply on 1 page. Most recent reply: May 1, 2002 2:52 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
patrick

Posts: 23
Nickname: patrick
Registered: Mar, 2002

adding lines in a file Posted: Apr 30, 2002 9:52 AM
Reply to this message Reply
Advertisement
hi,
i have to read a file find a specific word and when found add one or more lines to this file
how can i do that?
thank you


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: adding lines in a file Posted: May 1, 2002 2:52 PM
Reply to this message Reply
No tested, nothing ...
But it should be it - or close to !
Look at the posts if you have troubles with some file types. It was an discussion with Maysoon.

Thomas,

package test.io;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileWriter;
 
import java.io.IOException;
 
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
/**
  * @author <a href="tsmets@lautre.net">Thomas SMETS</a>, Brussels
  */
public class ReadingLineAndProcessing
{
  public static Category log = Category.getInstance (ReadingLineAndProcessing.class);
  public static final String USAGE 
    = "java test.io.ReadingLineAndProcessing <aFileNameToProcess>";  
  public static final String PROPERTY_LINE_SEPARATOR = "line.separator";
  public static final String LINE_SEPARATOR = System.getProperty (PROPERTY_LINE_SEPARATOR);
  public static final String TEMP = "Temp_";
  
 
  public static void main (String[] args)  
  {
    BasicConfigurator.resetConfiguration ();
    BasicConfigurator.configure ();
    
    if (args.length < 1)
      printUsage ();
    
    BufferedReader in = new BufferedReader(new FileReader( args[0] ));
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TEMP + args[0])));
    
    String S, pS;
    while ((S = in.readLine ()) != null )
    {
      log.info ("Reading a line : " + S);
      pS = processString (S);
      out.write (pS);
      out.write (LINE_SEPARATOR);
    }  
    
    out.flush ();
    out.close ();
    in.close ();
    
    log.info ("Re-copying");
    
    in = new BufferedReader(new FileReader(TEMP + args[0] ));
    out = new PrintWriter(new BufferedWriter(new FileWriter(args[0])));
    
    while ((S = in.readLine ()) != null )
    {     
      out.write (S);
      out.write (LINE_SEPARATOR);
    }
        
    out.flush ();
    out.close ();
    in.close ();
    
    log.info ("Copied ...");
  }
  
  public static String processString (String s)
  {
    String processedS;        
    return processedS = s + s;
  }
  
  public static void printUsage ()
  {
    System.out.println (USAGE);
  }
}

Flat View: This topic has 1 reply on 1 page
Topic: Help please Previous Topic   Next Topic Topic: Error in Running the class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use