how to ignore separator??

N

nice_arabian

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???
 
V

VisionSet

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???

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);
 
S

Snyke

Quick and dirty hack: use trim(), which removes all leading and
trailing whitespaces from a string.
 
J

James McGill

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.
 
C

Chris Smith

James McGill said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Snyke said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mark Thomas

Chris said:
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
 
R

Robert Klemme

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???

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

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top