We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

substring()

kousis
kousis asked
on
Medium Priority
1,160 Views
Last Modified: 2008-02-20
I have the file in rtf format want to get the output in wordpad with the same format. but my output changes when viewd in wordpad. how it can be done?
my original file contains., From ATOM till N is single line and next line starts from ATOM.
ATOM      1  N   ASN    35      -6.009  -4.045  21.189  1.00 42.58           N  
ATOM      2  CA  ASN    35      -6.184  -2.781  21.960  1.00 40.82           C  
my copy file contains
ATOM      1  N   ASN    35      -6.009  -4.045  21.189  1.00 42.58          
N ATOM      2  CA  ASN    35      -6.184  -2.781  21.960  1.00 40.82          
C  
I use substring()
how it can be rectified?

Comment
Watch Question

Top Expert 2006

Commented:
post the method that writes to the new file.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>but my output changes when viewd in wordpad.

Are you sure you're not just experiencing line-wrap?

Author

Commented:
import java.io.*;
import java.lang.*;
import java.lang.String;
import java.util.*;
import java.text.*;
public class trans1
{
public static void main(String args[])throws IOException
{
File inputfile=new File("ptn.rtf");
File outputfile=new File("ptncopy.rtf");

FileReader in= new FileReader(inputfile);
FileWriter out=new FileWriter(outputfile);
BufferedReader br=new BufferedReader(in);
BufferedWriter bw=new BufferedWriter(out);
String s;
int n=0;

char a='A';

while((s=br.readLine())!=null)
{
if(s.startsWith("ATOM"))
{
s=s.substring(0,n+21)+a+s.substring(n+22);
}
out.write(s);

}
in.close();
out.close();
}
}
here, I am writing one letter and copying it in another file. only in this case , i am facing the error as mentioned earlier.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Top Expert 2006

Commented:
>  You can't read rtf like that
AKAIK, you can.  You should be able to read/write RTF as plain text... even for RTF with images.
Top Expert 2006

Commented:
Did you create your original RTF file with Wordpad?
I know that you might get problems with RTF docs created with Word.
CERTIFIED EXPERT
Top Expert 2016

Commented:
If that 'rtf' is working then it probably isn't rtf - it's probably a text file with an rtf extension.

Author

Commented:
Thanx for giving me the above link.I'll work it out this on monday and get back to you.Hope my output will be the same as the parent file if I include the above mentioned package. isn't it?
Top Expert 2006

Commented:
Note that there are several sub-versions for the RTF standard.
That's probably why RTF files created with Word don't work.

>  You can't read rtf like that.
CEHJ, i guess i need to agree with your comment for RTF created with MS Word.
CERTIFIED EXPERT
Top Expert 2016

Commented:
As i mentioned earlier, you're probably looking at it with line wrap on - which is why the lines are wrapping

Author

Commented:
yes it's text file with an rtf extension

Author

Commented:
ok. let me explain to you. I have pdb file. which is protein structure file.this file has the  x y z  coordinates. the alignment should not be changed. if it changes structure will not be seen. which extension is suitable in this case.
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Hope my output will be the same as the parent file if I include the above mentioned package. isn't it?

Firstly, we need to find out if this really is an rtf file - i suspect not. For instance, i've just saved the output you printed above as rtf and this is what you see when it's opened in a text editor (logically equivalent to reading it with Java as you show above):

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fcharset0 Times New Roman;}{\f1\fswiss\fcharset0 Arial;}}
\viewkind4\uc1\pard\sb100\sa100\lang2057\f0\fs24 ATOM 1 N ASN 35 -6.009 -4.045 21.189 1.00 42.58 N \par
ATOM 2 CA ASN 35 -6.184 -2.781 21.960 1.00 40.82 C\lang1033\f1\fs20\par
}

(and i couldn't even copy/paste the last character - it wasn't allowed as it was a null char)
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>CEHJ, i guess i need to agree with your comment for RTF created with MS Word

How could it read any kind of rtf unless it contained no metadata and no 'illegal' characters? It were devoid of these then it would be a plain text file would it not?

>>(note that you can flush outside the while loop if you want)

Why flush at all?

If you're printing lines, it's much easier to do not

>>BufferedWriter bw=new BufferedWriter(out);

but

PrintWriter pw = new PrintWriter(out);
pw.println(.....);
Top Expert 2006

Commented:
using Wordpad, i get this:
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\lang1033\f0\fs20\par
ATOM      1  N   ASN    35      -6.009  -4.045  21.189  1.00 42.58           N  \par
ATOM      2  CA  ASN    35      -6.184  -2.781  21.960  1.00 40.82           C  \par
\par
}
which is a perfecly valid text file.
with indeed a NULL character at the end, but java does not care about that.
note i have a blank line as first line, that's why i get my first "ATOM" line at the beginning of the line.

Author

Commented:
thanx for your comment. I'll get back to  you on monday
bye
Top Expert 2006

Commented:
>  it would be a plain text file
just like an html file is a plain text file... no?
the nice thing about RTF is that you can insert text without messing with the formatting.  Just like you do with html files... if you insert text at a point where the text is red and bold, the inserted text will be red and bold.
In word documents, you can replace text by some other text that has exactly the same length, otherwise the file gets completely messed up...
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>which is a perfecly valid text file

Well it depends on how you look at it - we obviously look at it differently ;-) It's valid if you happen to know what metadata it contains and are allowing for them in your parsing routine, which is probably not what kousis is doing. But we're now in academic territory, as he's since said:

>>yes it's text file with an rtf extension
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sounds good rtf Grand - i don't know much about it. What i have noticed, which seems rather counter-intuitive, is that Word files get larger if you save them as rtf. Comments?
Top Expert 2006

Commented:
> Word files get larger if you save them as rtf.
You can try this funny thing:
1- Create an RTF file in Wordpad, look at its size.
2- Open that file in Word and save it (still as RTF), look at its size (it's usually trippled).
3- Open that file in Wordpad and save it, look at its size (it's back to the original small size).
Conclusion: don't use Word.  :°)

> >>which is a perfecly valid text file
It's fine as long as you use it as template, to replace/insert text.
It's not fine indeed if you need to parse it, which you don't need to do in this case.
You can just copy all the gibberish formatting characters (including encoded images) straight from input to output.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
BTW, you've already been told to use the BufferedWriter's write (), newLine () and flush () methods in the answer you accepted at:

https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20939669.html

Accepted answer - https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20939669.html#10738518

Why did you revert back to out.write () ??
Top Expert 2006

Commented:
:°)
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>You can try this funny thing...

Knowing M$ i wouldn't be at all surprised if they haven't made no attempt to eliminate redundant metadata from the Word rtf in order to discourage people from using the portable, platform-independent rtf. i.e. people might think - 'well if it's larger as rtf than Word, i may as well save as Word' ;-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.