Cat.java Source code

Java tutorial

Introduction

Here is the source code for Cat.java

Source

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Cat {
    public static void concatenate(String fileName) {
        RandomAccessFile file = null;
        String line = null;

        try {
            file = new RandomAccessFile(fileName, "r");
            while ((line = file.readLine()) != null) {
                System.out.println(line);
            }
            return;
        } catch (FileNotFoundException fnf) {
            System.err.println("File: " + fileName + " not found.");
        } catch (Exception e) {
            System.err.println(e.toString());
        } finally {
            if (file != null) {
                try {
                    file.close();
                } catch (IOException io) {
                }
            }
        }

    }

    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++)
            Cat.concatenate(args[i]);
    }
}