Developer's Daily Java Education - Test Projects
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
   
 


package com.devdaily.apps.unix2dos;

import java.io.*;

/**
 * Convert a Unix file to a DOS file, i.e., convert \r\n to \r only.
 */
public class Unix2Dos
{
  private File unixFile;
  private File dosFile;

  public static void main (String[] args)
  {
    Unix2Dos u2d = new Unix2Dos(args);
  }

  public Unix2Dos(String[] args)
  {
    getProperArgCountOrExit(args);
    createUnixFileOrExit(args[0]);
    //openDosFileOrExit(args[1]);
    // read from the unix file, write to the dos file
  }

  private void getProperArgCountOrExit(String[] args)
  {
    if ( args.length != 2 )
    {
      displayUsageStatement();
      System.exit(1);
    }
  }

  private void createUnixFileOrExit(String fileName)
  {
    unixFile = new File(fileName);
    if (! unixFile.exists() )
    {
      System.err.println( "\nCannot find Unix file, " + fileName );
      displayUsageStatement();
      System.exit(2);
    }
    if (! unixFile.isFile() )
    {
      System.err.println( "\n" + fileName + " is not a file, quitting.");
      displayUsageStatement();
      System.exit(4);
    }
    if (! unixFile.canRead() )
    {
      System.err.println( "\nCannot read Unix file, " + fileName );
      displayUsageStatement();
      System.exit(3);
    }
  }

  private void openDosFileOrExit(String fileName)
  {
    dosFile = new File(fileName);
    try
    {
      if (! dosFile.createNewFile() )
      {
        System.err.println( "\nCould not create the DOS file, " + fileName );
        System.exit(5);
      }
    }
    catch (IOException ioe)
    {
      System.err.println( "\nThrew an IOException trying to create the file " + fileName + ", message follows." );
      System.err.println( ioe.getMessage() );
      System.exit(5);
    }
  }

  private void displayUsageStatement()
  {
    System.err.println( "\nUsage: java com.devdaily.apps.unix2dos.Unix2Dos unixfile dosfile" );
  }

}
Copyright 1998-2003 DevDaily Interactive, Inc.
All Rights Reserved.