writing to a text file, urgent help needed

Hi,

I am trying to write to a text file, but I need certain behavior that I'm not sure how to get. Look at the code below, it is fairly simple and straight forward.

FileOutputStream FOStream = null;

PrintStream POStream = null;

try

{

FOStream = new FileOutputStream("text.txt");

POStream = new PrintStream(FOStream);

for (int i=10; i<20; i++){

POStream.print("hello" + i +"\r\n");

}

}catch(Exception e){

e.printStackTrace();

}

This pretty much just iterates thru the for loop and then displays everything in the text file.

However, what I want is for each iteration to overwrite the previous iteration. So, for each iteration, I want the result to just overwrite what was already there in the text file instead of just appending it. I know I can create the FileOutputStream inside the loop itself to create a text file every iteration but I would like to avoid this, as it causes some memory and cpu usage that I wish to avoid. Please help. I would be ever so grateful.

Thanks

[1087 byte] By [kosh_javaa] at [2007-11-27 10:58:04]
# 1

Please don't flag your question as urgent, even if it is for you. Claiming urgency is very likely to be counter-productive: most forum regulars will simply ignore such messages as rude and selfish attempts to elicit immediate and special attention.

Besides, unless a pack of rabid ninja spider monkeys is about to chase you over the edge of a cliff if you don't get an answer in fifteen seconds, your problem probably isn't as urgent as you think.

~

yawmarka at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 2

> This pretty much just iterates thru the for loop and then displays everything in the text file.

Erm, no. That's not what it does at all.

~

yawmarka at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 3

> Please don't flag your question as urgent, even if

> it is for you. Claiming urgency is very likely to

> be counter-productive: most forum regulars will

> simply ignore such messages as rude and selfish

> attempts to elicit immediate and special attention.

>

> Besides, unless a pack of rabid ninja spider monkeys

> is about to chase you over the edge of a cliff if you

> don't get an answer in fifteen seconds, your problem

> probably isn't as urgent as you think.

>

> ~

He didn't say his question was urgent, he said that any help he receives should be urgent help.

Ergo, all help offered should be bold and underlined.

Message was edited by:

jGardner

jGardnera at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 4

> Ergo, all help offered should be bold and underlined.

Touch.

~

yawmarka at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 5

my apologies if this seemed to be unnessarily made urgent.......however, if you could still help me out, I would be very grateful

kosh_javaa at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 6

> however, if you could still help me out, I would be very grateful

I'd be happy to help. First off, do you understand why your code does not do what you say it does (i.e., display everything in the text file)?

~

yawmarka at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 7

ok, what I get displaying in the text file after running the program is....

hello10

hello11

hello12

hello13

hello14

hello15

hello16

hello17

hello18

hello19

and this is what I kind of meant by "This pretty much just iterates thru the for loop and then displays everything in the text file". It basically appends each iteration in the next line, and prints everything out in the text file. Are we good?

kosh_javaa at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 8

> (i.e., display everything in the text file)?

display as in "if you open the text file, that stuff is displayed"

@OP: People usually call this "writing to a (text) file".

thomas.behra at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 9

You realize that your requirement does not make much sense?

If you only want the last line written to the text file, well then write only the last line.

thomas.behra at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 10

apologies again...i meant writing to a text file.....

and the reason I want it this way is pretty complicated. I have something else reading from the same text file. Ill try to sum it up for you.....

I have one software package (java side) that has an infinite while loop, and polls for a user input. the moment this input is received, it writes that input into a text file.

Now, another software package (python side) is reading this input from the text file at the same time and doing stuff with it.

Therefore, its not that, I just want the last line written to a text file......I need to write each input to the text file so that the python side can read it. And everytime, I get a new user input, I wish to overwrite whats already there, so that the python side can read the new input. Let me know if your still with me.

kosh_javaa at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 11

and I know one option, is to make the changes on the python side, so as to make it read the last line of the text file.

However, I am not involved with this aspect of the project, so I was wondering if anything could be done from my side (java side).

kosh_javaa at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 12

> And everytime, I get a new user input, I wish to overwrite whats already there, so that the

> python side can read the new input.

No problem. Just create a new FileWriter and overwrite the file.

~

yawmarka at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 13

Yes, I did think about that. THing is, with respect to our environment, doing this would consume a lot of memory and cpu usage as we have to constantly create a new text file.

I was wondering if there is any other efficient way of doing this, such as a java class that has this kind of functionality of something.

I was also thinking about another solution. What if, right before I write to the text file, check to see if there is anything already in the text file. If there is, erase it entirely so that the text file is blank right before I write to it. Is this even possible?

kosh_javaa at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 14

> Yes, I did think about that. THing is, with respect

> to our environment, doing this would consume a lot of

> memory and cpu usage as we have to constantly create

> a new text file.

Have you verified this is a problem by using a profiler?

> I was wondering if there is any other efficient way

> of doing this, such as a java class that has this

> kind of functionality of something.

You could do something similar with a RandomAccessFile, but I'm not sure it's worth the trouble.

> I was also thinking about another solution. What if,

> right before I write to the text file, check to see

> if there is anything already in the text file. If

> there is, erase it entirely so that the text file is

> blank right before I write to it. Is this even

> possible?

That's more "efficient" than just overwriting what's there?

Read this:

Writing Better Code: A Conversation With Sun Microsystems Technology Evangelist Brian Goetz

http://java.sun.com/developer/technicalArticles/Interviews/goetz_qa.html

Q: So how can developers write Java code that performs well?

A: The answer may seem counterintuitive. Often, the way to write fast code in Java applications is to write dumb code -- code that is straightforward, clean, and follows the most obvious object-oriented principles. This has to do with the nature of dynamic compilers, which are big pattern-matching engines. Because compilers are written by humans who have schedules and time budgets, the compiler developers focus their efforts on the most common code patterns, because that's where they get the most leverage. So if you write code using straightforward object-oriented principles, you'll get better compiler optimization than if you write gnarly, hacked-up, bit-banging code that looks really clever but that the compiler can't optimize effectively.

So clean, dumb code often runs faster than really clever code, contrary to what developing in C might have taught us. In C, clever source code turns into the expected idiom at the machine-code level, but it doesn't work that way in Java applications. I'm not saying that the Java compiler is too dumb to translate clever code into the appropriate machine code. It actually optimizes Java code more effectively than does C.

My advice is this: Write simple straightforward code and then, if the performance is still not "good enough", optimize. But implicit in the concept of "good enough" is that you need to have clear performance metrics. Without them, you'll never know when you're done optimizing. You'll also need a realistic, repeatable, testing program in place to determine if you're meeting your metrics. Once you can test the performance of your program under actual operating conditions, then it's OK to start tweaking, because you'll know if your tweaks are helping or not. But assuming "Oh, gee, I think if I change this, it will go faster" is usually counterproductive in Java programming.

Because Java code is dynamically compiled, realistic testing conditions are crucial. If you take a class out of context, it will be compiled differently than it will in your application, which means performance must be measured under realistic conditions. So performance metrics should be tied to indices that have business value -- transactions per second, mean service time, worst-case latency -- factors that your customers will perceive. Focusing on performance characteristics at the micro level is often misleading and difficult to test, because it's hard to make a realistic test case for some small bit of code that you've taken out of context.

~

yawmarka at 2007-7-29 12:13:11 > top of Java-index,Java Essentials,Java Programming...
# 15

just to keep options open, how could I use RandomAccessFile to do this?

kosh_javaa at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 16

and I figured erasing the file is not efficient, but thought it would atleast consume less resources

kosh_javaa at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 17

> and I figured erasing the file is not efficient, but

> thought it would atleast consume less resources

Before you decide to do something complex and time-consuming, it'd be worthwhile to validate those assumptions with actual data. You may well find that the things you're worried about aren't worth worrying about at all.

~

yawmarka at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 18

> just to keep options open, how could I use

> RandomAccessFile to do this?

Are you familiar with the class?

http://java.sun.com/javase/6/docs/api/java/io/RandomAccessFile.html

~

yawmarka at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 19

I have never used this class before. Could you give me an idea as to how I would use it? thanks again for your help.

kosh_javaa at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 20

http://java.sun.com/docs/books/tutorial/essential/io/rafs.html

~

yawmarka at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 21

Im sorry, i still dont get it. I understand that using this class will allow me to read and write to the textfile.....but how will I get rid of what is already there?

kosh_javaa at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 22

is this what im supposed to do............after receiving the next input, am I reading it and writing it into the same location as the previous entry......thereby kinda replacing it?

kosh_javaa at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 23

> is this what im supposed to do............after

> receiving the next input, am I reading it and writing

> it into the same location as the previous

> entry......thereby kinda replacing it?

I think I just saw the light bulb go on! ;o)

Just to reiterate, RandomAccessFile allows the developer to specify where in the file s/he wants to start writing.

~

yawmarka at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 24

If you're going to use RandomAccessFile, you need to make sure that you call setLength(0L) before each write, otherwise you'll potentially leave garbage at the end of the file.

I will echo yawmark's point that you shouldn't be concerned about CPU time and memory without actually profiling. The simple approach of re-creating the file for each new line is preferable from a maintenance standpoint, since you don't have to worry about leaving partial data in the file, or the atomicity of reads and writes.

Incidentally, your original code didn't close the file after writing to it. This could be the source of any CPU and memory issues that you're seeing ... or at least errors due to exhausting file handles.

And finally: do you really want to overwrite the file? In other words, is it OK if the python process misses some messages? Usually that's not OK. And if it's not OK, then you're going to be much better off using pipes (named pipes, stdio pipelines, sockets, whatever) to communicate between the processes.

Edit: also, your original code doesn't follow standard Java coding conventions: variables normally start with a lower-case letter, classes with uppercase.

kdgregorya at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 25

I have to look into RandomAccessFile and it will probably take a while to figure out how to use it. I am still struggling at the moment, and am probably going to try for some code samples online.

THe major reason for not recreating the file everytime is because it crashes the python system that is trying to read the file. We have tested this and seen that it crashes because the text file is constantly recreated while we have this other system constantly trying to read from it. As a result, we had to resort to other tactics.

finally, the code I had written is not the final and used copy. I just quickly wrote up some dummy code. I do close the connection.

I really appreciate the quick response time on this forum.

kosh_javaa at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 26

> THe major reason for not recreating the file

> everytime is because it crashes the python system

> that is trying to read the file.

Sounds like you have bigger problems then. And you're not going to solve those problems with a RandomAccessFile, just add to them.

I suggest looking into redesigning your application using a standard pipeline design.

kdgregorya at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 27

I might suggest using a different approach. Use something that was built for two programs to communicate: Sockets.

- Listen on a TCP socket on the Python side (shouldn't be much more difficult than polling the file)

- Open a connections to that socket in Java and send your stuff whenever it arrives.

This way you'll have only one Socket, don't need to re-create any files, make sure that you don't miss any lines (for example if the Java-side writes the file faster/more often than the Python side reads it) and can claim to have learned something new (probably ;-)).

A possible other solution would be a pipe in the filesystem, I haven't used such a thing in Java, 'though.

Edit: out of curiosity I've just tried writing to a fifo (created using mkfifo in UNIX/Linux) and it works just fine, just handle it as you would handle a file.

JoachimSauera at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 28

I managed to get it working using RandomAccessFIle......so for anyone who might need help with this in the future, heres the code I have....

RandomAccessFile raf = null;

try {

raf = new RandomAccessFile ( "test.txt", "rw" ) ;

Writer out = new OutputStreamWriter ( new FileOutputStream (raf.getFD ()));// , "UTF-8" ) ;

for (int i=0; i<11; i++) {

raf.seek ( 0 ) ;

out.write ( "ouput number = " + i ) ;

raf.setLength(0L);

out.close();

}

} catch(Exception e){

e.printStackTrace();

}

PS:Thanks for all ure help, kdgregory - the setLength(0L) pulled through

and yawmark - "unless a pack of rabid ninja spider monkeys is about to chase you over the edge of a cliff if you don't get an answer in fifteen seconds, your problem probably isn't as urgent as you think. ".........that was just hilarious.....I died reading it

kosh_javaa at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 29

> I managed to get it working using

> RandomAccessFIle......so for anyone who might need

> help with this in the future, heres the code I

> have....

> RandomAccessFile raf = null;

> try {

> raf = new RandomAccessFile ( "test.txt", "rw" ) ;

> Writer out = new OutputStreamWriter ( new FileOutputStream (raf.getFD ()));// , "UTF-8" ) ;

>

> for (int i=0; i<11; i++) {

>raf.seek ( 0 ) ;

> out.write ( "ouput number = " + i ) ;

>raf.setLength(0L);

>out.close();

>

> } catch(Exception e){

> e.printStackTrace();

> }

Whoa. This code is ... questionable.

First off, you're closing the OutputStreamWriter in the first iteration of the loop, then trying to write to it in subsequent iterations. So you'd never get more than the first line of output.

Then, immediately after writing that text -- but perhaps before it gets passed through the various buffers to the file -- you're setting the length of the random access file to 0. So if this actually worked, you'd have content in the file for perhaps a fraction of a second, before it gets wiped out.

I'll say it again: you have bigger, system-level problems. Trying to use a disk file as a pipe isn't going to solve those problems. And writing buggy code to simulate that pipe certainly isn't going to help.

kdgregorya at 2007-7-29 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 30

definatly interesting... have you ever considered looking at itext? pretty cool download that lets you output stuff to pdf and word files

mark07a at 2007-7-29 12:13:21 > top of Java-index,Java Essentials,Java Programming...
# 31

hey, um i was wondering, does it have any append options? 'cause if it does, you could just mark false instead of true, and it would overwrite on it own everytime its pulled out... But as i said, i was only wondering... i havent read carefully the code, so feel free to ignore me =]

Good luck

efainsoda at 2007-7-29 12:13:21 > top of Java-index,Java Essentials,Java Programming...