Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Simple End-of-line marker help!!!

Hi. I'm writing a simple java program to basically take take 2 parameters as a source file and a target file. My program will will the Source File and make an exact duplicate of it line for line under a new file (the target file). As of right now, i have it successfully read and copy each line. However, i have one problem: I need to be able to recognize End-of-line markers so that if the very last line of the source file DOES NOT have an End-of-line marker, then it won't print an End-of-line marker. I basically am lost about how to check and recognize the End-of-line marker at all. Here's my code so far:

import java.io.*;

public class Copy
{
    /**
     * Constructor for objects of class Copy
     */
    public static void main(String[]args)
    {
        if(args.length == 2)
        {
            try
            {
                String data;
               
                String inputFile = args[0];
                FileReader inStream = new FileReader(inputFile);
                BufferedReader ins = new BufferedReader(inStream);
               
                String outputFile = args[1];
                FileWriter outStream = new FileWriter(outputFile);
                PrintWriter outs = new PrintWriter(outStream);
               
                do
                {
                    data = ins.readLine();
                    if (data != null)
                    {
                        outs.println(data);
                    }
                }   while(data != null);
               
                ins.close();
                outs.close();
            }
            catch(IOException ex)
            {
                System.out.println("--- Error: " + ex.getMessage());
                ex.printStackTrace();
                System.exit(1);
            }
            catch(NumberFormatException ex)
            {
                System.out.println(ex.getMessage());
                ex.printStackTrace();
            }
        }
        else
        {
            System.out.println("--- Error: No Parameters Entered");
            System.exit(1);            
        }
    }
}

Someone please help!! I want to be able to test for end-of-line markers but i have no clue how! Thanks to all for reading this! =)
0
xillu5ionzx
Asked:
xillu5ionzx
  • 2
1 Solution
 
objectsCommented:
If you are just doing a direct copy then use a stream class directly instead of a reader and just copy the bytes.
0
 
objectsCommented:
akaik it is not possible to do what you require using readLine().
0
 
honey_bearCommented:
use ins.ready() test is end.


import java.io.*;
public class test
{
   /**
    * Constructor for objects of class Copy
    */
   public static void main(String[]args)
   {
       if(args.length == 2)
       {
           try
           {
               String data;
               
               String inputFile = args[0];
               FileReader inStream = new FileReader(inputFile);
               BufferedReader ins = new BufferedReader(inStream);
               
               String outputFile = args[1];
               FileWriter outStream = new FileWriter(outputFile);
               PrintWriter outs = new PrintWriter(outStream);
               
               do
               {
                   data = ins.readLine();
                   if (data != null)
                   {
                            if(ins.ready())
                            outs.println(data);
                            else
                                 outs.print(data);
                   }
               }   while(data != null);
               
               ins.close();
               outs.close();
           }
           catch(IOException ex)
           {
               System.out.println("--- Error: " + ex.getMessage());
               ex.printStackTrace();
               System.exit(1);
           }
           catch(NumberFormatException ex)
           {
               System.out.println(ex.getMessage());
               ex.printStackTrace();
           }
       }
       else
       {
           System.out.println("--- Error: No Parameters Entered");
           System.exit(1);            
       }
   }
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now