numberlines.java

来自「Data StructuresAnd Algorithm Analysis In」· Java 代码 · 共 47 行

JAVA
47
字号
    import java.io.*;

    public class NumberLines
    {
        public static void main( String [ ] args )
        {
            PrintWriter   fileOut = null;
            BufferedReader fileIn = null;

            try
            {
                if( args.length == 1 )
                    fileOut = new PrintWriter( System.out );
                else if( args.length == 0 || args.length > 2 )
                    throw new Exception( "Usage: java NumberLines sourceFile destFile" );
                else if( args[ 0 ].equals( args[ 1 ] ) )
                    throw new Exception( "Cannot copy to self" );
                else
                    fileOut = new PrintWriter( new BufferedWriter(
                                         new FileWriter( args[ 1 ] ) ) );

                fileIn  = new BufferedReader( new FileReader( args[ 0 ] ) );

                String oneLine;
                int lineNum = 0;

                while( ( oneLine = fileIn.readLine( ) ) != null )
                {
                    fileOut.print( ++lineNum + "\t");
                    fileOut.println( oneLine );
                }
            }
            catch( Exception e )
              { System.out.println( e ); }

            try
            {
                if( fileIn != null )
                    fileIn.close( );
                if( fileOut != null )
                    fileOut.close( );
            }
            catch( IOException e )
              { }
        }
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?