Thomas SMETS
Posts: 307
Nickname: tsmets
Registered: Apr, 2002
|
|
Re: adding lines in a file
|
Posted: May 1, 2002 2:52 PM
|
|
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);
}
}
|
|