open multiple files at the same time

Dear everyone,

Suppose we have an array of millions of integers, we wanna create 10 files:

0.txt (stores the integers in that array whose last bit is 0.)

1.txt (stores the integers in that array whose last bit is 1.)

2.txt (stores the integers in that array whose last bit is 2.)

3.txt (stores the integers in that array whose last bit is 3.)

......

and so on.

One solution to this is to look at each integer and then decide which file to OPEN and to write to. But this will result in a waste of time spent on opening and closing files repeatedly.

So I am considering if we can OPEN all those files in the beginning, and then look at each integer to decide to which file to write to. Once all integers have been gone through, CLOSE all files.

But I am not so sure if this will lead to more memory use, whether or not all the file content will be loaded into RAM as the file is OPEN.

Also, can anybody give me some idea how I can implement this.

Thanks in advance!

[1042 byte] By [Wenxiaoa] at [2007-11-27 3:30:10]
# 1
Any additional memory use will be marginal.If I were you, I'd open all the files, put the FileWriters into an array, and then use the last digit as an index into that array.
paulcwa at 2007-7-12 8:33:11 > top of Java-index,Java Essentials,Java Programming...
# 2

> Dear everyone,

>

> Suppose we have an array of millions of integers, we

> wanna create 10 files:

>

> 0.txt (stores the integers in that array whose last

> bit is 0.)

> 1.txt (stores the integers in that array whose last

> bit is 1.)

> 2.txt (stores the integers in that array whose last

> bit is 2.)

> 3.txt (stores the integers in that array whose last

> bit is 3.)

> ......

A bit can only be 0 or 1. If you want to interpret the low order as the file number, you'll need to rethink it:

0000 --> 0

0001 --> 1

0010 --> 2

0011 --> 3

...

1001 --> 9

1010 --> ?

1011 --> ?

... etc.

> So I am considering if we can OPEN all those files in

> the beginning, and then look at each integer to

> decide to which file to write to. Once all integers

> have been gone through, CLOSE all files.

Yes, you can do that.

> But I am not so sure if this will lead to more memory

> use, whether or not all the file content will be

> loaded into RAM as the file is OPEN.

The files' contents are only loaded into memory if you explicitly do so by reading them. If you're just opening and writing, the files' contents will no be loaded into memory.

> Also, can anybody give me some idea how I can

> implement this.

What specific questions do you have? It's not clear what you're trying to do or what trouble you're having.

You might want to start here:

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

jverda at 2007-7-12 8:33:11 > top of Java-index,Java Essentials,Java Programming...
# 3
Oh yeah.OP: do you understand the difference between "digit" and "bit"? It sounds like you actually mean "decimal digit", but you say "bit".Getting either the last bit or the last (decimal) digit is quite easy, but you should be clear on what you're actually trying to do.
paulcwa at 2007-7-12 8:33:12 > top of Java-index,Java Essentials,Java Programming...