The Artima Developer Community
Sponsored Link

Java Answers Forum
Formatter and Appendable ?

4 replies on 1 page. Most recent reply: Jul 14, 2005 8:07 PM by Rob Roy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Rob Roy

Posts: 3
Nickname: robbie
Registered: Jul, 2005

Formatter and Appendable ? Posted: Jul 10, 2005 8:31 AM
Reply to this message Reply
Advertisement
I want to open a text file using the new Formatter class to be able to append to the file:

Something like the following:

Formatter outFile = new Formatter( "filename.txt", true );

This does not do it. The constructor does not accept the "true" argument.

My question: How would you "open" such file in append mode?


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Formatter and Appendable ? Posted: Jul 10, 2005 11:59 PM
Reply to this message Reply
> I want to open a text file using the new Formatter class
> to be able to append to the file:
>
> Something like the following:
>
> Formatter outFile = new Formatter( "filename.txt",
> xt", true );
>
> This does not do it. The constructor does not accept the
> "true" argument.
>
> My question: How would you "open" such file in append mode?

An alternative approach:

Define your own class with this capability


public class MyFormatter{
private String buf;
private Formatter outFile;
private boolean append;
private StringBuffer myBuf;

public MyFormatter(Formatter outFile, boolean append){
this.outFile = outFile;
this.append = append;
buf = use BufferedReader and InputStreamReader
to read The String in the file and store
it in buf
myBuf = new StringBuffer(buf);
}

public boolean doAppend(String str){
// You are able to append
if(append == true){
myBuf.append(str);
return true;
}else{
// We are not in append mode
return false;
}
}

public String toString(){
return myBuf.toString();
}
}



Just one of many ways to go about it.

Good luck,

Spike

Rob Roy

Posts: 3
Nickname: robbie
Registered: Jul, 2005

Re: Formatter and Appendable ? Posted: Jul 11, 2005 5:56 PM
Reply to this message Reply
Thank you, Spike. It seems reasonable. My only concern is that if the file is too big, I may have a problem. Plus, it could take a while to read in the entire file.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Formatter and Appendable ? Posted: Jul 11, 2005 11:22 PM
Reply to this message Reply
Hi there:

How large a file are we talking of?

There maybe alternatives in the Java API, but as we
don't know their implementations, they might also
be using StringBuffers.

Using a StringBuffer in the algorithm is the fastest
way *I can* think of - at least just off the top of my
head. Its definitely quicker than using constantly
reading and appending it via a String.

Actually, I think the algorithm should be fairly
efficient with respect to speed (you can always test
it against other implementations via timestamps).

Only thing is only spit it back out as a String
(toString()) when you are either finally reading to
or from the file.

Check out this:

String globalString;// initiated in Constructor

// Expensive method
public String app(String str){
globalString = globalString+str;
}

// supposing you want to append a few hundred
for(int i = 0; i < myStrings.length; i++){
app(myStrings[i]);
}

// In expensive method
public String app(String str){
myStringBuf.append(str);
}
// using buffer algorithm
for(int i = 0; i < myStrings.length; i++){
app(myStrings[i]);
}


From an algorithm stand point the former recopies
the String globalString each time before appending,
whilst the latter simply keeps a String buffer and
continuously appends a String to the String attribute
in the buffer.

Hence I'm quite confident that it should be fairly
efficient...

I hope this makes sense...

As I said this was the most efficient algorithm
I could think of at the time (there are obviously
others), but this should do the trick even on fairly
large files (I think) - how large are the files you
are dealing with?

Or I think you can try opening a File and get to
EOF (without copying), then stick the String there.

Various options to explore.

Hopefully, if the files are extremely big you won't
run into any "OutOfMemoryError" issues (in which
case, research the EOF option I suggested), I'll
post a snippet if I can think of one - right now
some multithreading stuff is driving me nutts.

Good luck again, and I hope it helps...

Rob Roy

Posts: 3
Nickname: robbie
Registered: Jul, 2005

Re: Formatter and Appendable ? Posted: Jul 14, 2005 8:07 PM
Reply to this message Reply
Spike,

The files I'm dealing with are not that big – at least not now. I was just thinking ahead in case these files begin to get larger and larger.

Generally, I do not need to read in the file. I just open the file and move the "file pointer" to end before adding some new data to the file.

Again, thanks for the follow up.

Rob

Flat View: This topic has 4 replies on 1 page
Topic: reader Previous Topic   Next Topic Topic: Polymorphism vs Methods Signatures Best Fitness

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use