Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

how to ignore separator??

Thread view: 
nice_arabian@yahoo.com - 09 May 2006 22:26 GMT
Hi plz if i have a text file file1.txt contains for example:
Hi Bob ?
How are You?
I need to meet u.
See u Bye

and i need to copy this file contents to another file but without line
separator i need to ignore it so i want for example this file to
contain:
Hi Bob ?How are You?I need to meet u.See u Bye
how can i do that???
mike - 09 May 2006 22:39 GMT
You could do string=string.replaceAll("\n","");
James McGill - 10 May 2006 04:41 GMT
> You could do string=string.replaceAll("\n","");

Of course that only works if the file's newline and the runtime
platform's newline happen to be the same.  The very first thing I'd
do to test this routine would be to feed it files with unix-style
newlines and dos-style newlines.
Chris Smith - 10 May 2006 05:37 GMT
> > You could do string=string.replaceAll("\n","");
>
> Of course that only works if the file's newline and the runtime
> platform's newline happen to be the same.  The very first thing I'd
> do to test this routine would be to feed it files with unix-style
> newlines and dos-style newlines.

Not even that.  If you check the API docs for Pattern, you'll see that
it only matches \u000A, the newline character.  It doesn't match other
newline sequences at all, on any platform.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

VisionSet - 09 May 2006 22:42 GMT
> Hi plz if i have a text file file1.txt contains for example:
> Hi Bob ?
[quoted text clipped - 7 lines]
> Hi Bob ?How are You?I need to meet u.See u Bye
> how can i do that???

Easy.
Read it in and write it out character by character with FileReader &
FileWriter classes.  Wrap them both in BufferedReader/Writers.

int myChar = reader.read();
if (myChar != '\r' &&
   myChar != '\n' &&
   myChar != '\f') writer.write(myChar);

--
Mike W
Snyke - 09 May 2006 23:30 GMT
Quick and dirty hack: use trim(), which removes all leading and
trailing whitespaces from a string.
Chris Smith - 10 May 2006 05:38 GMT
> Quick and dirty hack: use trim(), which removes all leading and
> trailing whitespaces from a string.

Hack to do what?  It certainly doesn't do what the OP asked for.  I can
present all sorts of even quicker hacks, if we're dropping the
requirement that it works properly.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Mark Thomas - 10 May 2006 10:27 GMT
>> Quick and dirty hack: use trim(), which removes all leading and
>> trailing whitespaces from a string.
>
> Hack to do what?  It certainly doesn't do what the OP asked for.  I can
> present all sorts of even quicker hacks, if we're dropping the
> requirement that it works properly.

How about something like this:

import java.io.*;

public class Copier {
  public static void main(String[] args) {
    try {
      BufferedReader in = new BufferedReader
                              (new FileReader("file1.txt"));
      PrintWriter out = new PrintWriter(new FileWriter("file2.txt"));
      String line = null;
      while ((line = in.readLine()) != null) {
        out.write(line);
      }
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

as readLine() strips the line terminators as it goes, and write()
doesn't add them.

Mark
Robert Klemme - 10 May 2006 10:33 GMT
> Hi plz if i have a text file file1.txt contains for example:
> Hi Bob ?
[quoted text clipped - 7 lines]
> Hi Bob ?How are You?I need to meet u.See u Bye
> how can i do that???

A simple approach is to use BufferedReader.readLine() for reading and
write all lines with a standard Writer.

    robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.