Here's the simplest running example, basically a crappy implementation of the the Unix "cat" command (read a file and spit it out to the console, line by line). To get it to do something useful, replace the System.out.println with something more useful, like putting to a queue, or running the line through a CSV parser, or running a SQL query, etc.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class UnixCat
{
public void cat(String filename) throws IOException
{
BufferedReader is = new BufferedReader(new FileReader(filename));
String line = "";
while (line != null)
{
line = is.readLine();
System.out.println(line);
}
is.close();
}
public static void main(String[] args)
{
UnixCat uc = new UnixCat();
// Assume args is a list of filenames
for (int ii = 0; ii < args.length; ii++)
{
try
{
uc.cat(args[ii]);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
No comments:
Post a Comment