Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove HEX 0D and HEX 0A from a file.

Status
Not open for further replies.

abc73

Programmer
Apr 28, 2004
89
US
Hi,
I have a text-file that is wrapped. Can any-one help me how to un-wrap that file using java. Basically I am trying to delete any occurence of \r\n i.e hex 0D hex0A from a file. So that my resulting file is un-wrapped. Currently each line ends with hex 0D 0A and I want to delete these 0D 0A from the end of each line. Any help is really appreciated. Thanks

file looks like this:
asas asasas saa
dsdsd sdsd sdsd
sdsd sds dsdd sd

now I want to look like as below:
asas asasas saadsdsd sdsd sdsdsdsd sds dsdd sd

i.e. all the 0D0A are removed and the file is un-wrapped.
 
Maybe something like

while not eof {
readln()
write() //instead of writeln()
}

That should avoid line breaks

Cheers.

Dian
 
I need to check if file contains hex 0D 0A then remove it, if it doesn't contain any then don't process the file as it's already un-wrapped. Is there a way to check if file does contain 0D 0A then process removing it if not then don't process.
 
In order to check that, you need to read the file. If you read if wothout processing, you may have to read it again if it has the line breaks. The best approach depends on the average file size and the percentage ot files with those line breaks.

Answering your question, I'm not sure, but I think sthg line this should work:

int lines = 0;
while (!eof) {
readln()
lines ++;
}
if (lines > 1)
unwrap()

 
read the file in line by line and do a string replace :

Code:
BufferedReader br ...
FileOutputStream fos ...
String line = "";
while ((line = br.readLine()) != null) {
   line = line.replaceAll("\\r\\n", "");
    fos.write(line.getBytes());
}

// close IO streams

--------------------------------------------------
Free Database Connection Pooling Software
 
br.readLine () will remove the EOL afaik, sedj.

But do we have more information, to know if the file contains a \r\n, without reading the whole file?

Does a uncorrected file perhaps contain a EOL after a maximum of 80 characters?

Else we need to read the whole file.
Could save it to file.tmp
If the size doesn't differ, delete it.
Else replace the original file with the corrected file.tmp.

The timestamp of uncorrected files would keep intact.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top