[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1134
  • Last Modified:

substring()

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?

0
kousis
Asked:
kousis
  • 9
  • 9
  • 5
  • +1
1 Solution
 
GrandSchtroumpfCommented:
post the method that writes to the new file.
0
 
CEHJCommented:
>>but my output changes when viewd in wordpad.

Are you sure you're not just experiencing line-wrap?
0
 
kousisAuthor 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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
0
 
GrandSchtroumpfCommented:
>  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.
0
 
GrandSchtroumpfCommented:
Did you create your original RTF file with Wordpad?
I know that you might get problems with RTF docs created with Word.
0
 
CEHJCommented:
If that 'rtf' is working then it probably isn't rtf - it's probably a text file with an rtf extension.
0
 
kousisAuthor 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?
0
 
GrandSchtroumpfCommented:
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.
0
 
CEHJCommented:
As i mentioned earlier, you're probably looking at it with line wrap on - which is why the lines are wrapping
0
 
kousisAuthor Commented:
yes it's text file with an rtf extension
0
 
kousisAuthor 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.
0
 
GrandSchtroumpfCommented:
i tested your code using Wordpad, and i get one single line.

if you change this:
out.write(s);

to:
bw.write(s);
bw.newLine();
bw.flush();

then everything looks fine.
(note that you can flush outside the while loop if you want)
0
 
CEHJCommented:
>>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)
0
 
CEHJCommented:
>>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(.....);
0
 
GrandSchtroumpfCommented:
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.
0
 
kousisAuthor Commented:
thanx for your comment. I'll get back to  you on monday
bye
0
 
GrandSchtroumpfCommented:
>  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...
0
 
CEHJCommented:
>>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
0
 
CEHJCommented:
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?
0
 
GrandSchtroumpfCommented:
> 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
BTW, you've already been told to use the BufferedWriter's write (), newLine () and flush () methods in the answer you accepted at:

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

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

Why did you revert back to out.write () ??
0
 
GrandSchtroumpfCommented:
:°)
0
 
CEHJCommented:
>>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' ;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 9
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now