Solved

substring()

Posted on 2004-04-03
24
1,111 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
ID: 10747872
post the method that writes to the new file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10747890
>>but my output changes when viewd in wordpad.

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

Author Comment

by:kousis
ID: 10747893
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10747902
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10747922
>  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
ID: 10747931
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
ID: 10747940
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
ID: 10747943
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
ID: 10747945
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
ID: 10747948
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
ID: 10747950
yes it's text file with an rtf extension
0
 

Author Comment

by:kousis
ID: 10747976
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
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 100 total points
ID: 10747977
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
ID: 10747979
>>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
ID: 10748010
>>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
ID: 10748028
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
ID: 10748048
thanx for your comment. I'll get back to  you on monday
bye
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10748049
>  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
ID: 10748050
>>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
ID: 10748103
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
ID: 10748203
> 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:Mayank S
ID: 10754913
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
ID: 10757166
:°)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10757307
>>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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 79
configure dependency in POM for new database 3 25
by zero exception 10 39
Java string replace 11 43
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

786 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