Solved

substring()

Posted on 2004-04-03
24
1,106 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?

0
Comment
Question by:kousis
  • 9
  • 9
  • 5
  • +1
24 Comments
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
post the method that writes to the new file.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>but my output changes when viewd in wordpad.

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

Author Comment

by:kousis
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
>  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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
Did you create your original RTF file with Wordpad?
I know that you might get problems with RTF docs created with Word.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If that 'rtf' is working then it probably isn't rtf - it's probably a text file with an rtf extension.
0
 

Author Comment

by:kousis
Comment Utility
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
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
 
LVL 86

Expert Comment

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

Author Comment

by:kousis
Comment Utility
yes it's text file with an rtf extension
0
 

Author Comment

by:kousis
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 100 total points
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
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
 

Author Comment

by:kousis
Comment Utility
thanx for your comment. I'll get back to  you on monday
bye
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
>  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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
> 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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
:°)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now