?
Solved

Simple End-of-line marker help!!!

Posted on 2003-03-10
3
Medium Priority
?
406 Views
Last Modified: 2010-07-27
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
Comment
Question by:xillu5ionzx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8108512
If you are just doing a direct copy then use a stream class directly instead of a reader and just copy the bytes.
0
 
LVL 92

Expert Comment

by:objects
ID: 8108518
akaik it is not possible to do what you require using readLine().
0
 

Accepted Solution

by:
honey_bear earned 525 total points
ID: 8108790
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question