3 Replies Latest reply: May 6, 2007 3:00 PM by 807606 RSS

    open multiple files at the same time

    807606
      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!
        • 1. Re: open multiple files at the same time
          807606
          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.
          • 2. Re: open multiple files at the same time
            796440
            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
            • 3. Re: open multiple files at the same time
              807606
              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.