Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java File IO question :confused:

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java File IO question :confused:

    I was looking to know if anyone could help me out..

    What I'm trying to do is write (multiple) file(s) into a single file, but in a seemingly random order(Where a single file is in more than one spot), which ofcourse is not actually random as it still needs to be readable by the same program.

    I don't want to encrypt the files though, just more or less "scramble" them..
    I've been working on this for a bit, but I can't find a logical way to do it where a person would be unable to unscramble any file(s) modified by this in under 5 minutes

    If this isn't explained well enough, or is worded poorly, please let me know and I will see if I can explain better or choose different wording.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java File IO question :confused:

    I'm trying to do is write (multiple) file(s) into a single file, but in a seemingly random order
    Is that like shuffling a deck of cards: mixing lines from one file with lines from other files.
    The text for each line is unchanged.
    can't find a logical way to do it where a person would be unable to unscramble any file(s) modified by this
    Is the program supposed to be able to unscramble the file back to the original files?

    What is the purpose of this exercise?

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java File IO question :confused:

    The point of this would be for it to

    • Read the contents of a file
    • Place the file in a char/byte array array(array[file id][byte/char blocks])
    • After loading all files in this mannor, open an output file
    • place the byte/char arrays into the file in a random, but reconstructible, order


    This is just for educational purposes as I got bored, The list above is color coded, green is that which I've already determined how to do, red is that which I haven't.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java File IO question :confused:

    So the mixing is at the byte level not the line level. What about padding if the files are of different length?
    Are there a fixed number of files or can you write a control field into the output file with info on how it was constructed.
    If you can add a control field, that expands the possibilities.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java File IO question :confused:

    There's no fixed amount of files, and a control field strikes my interest, mind explaining it a little?
    Sorry for my lack of understanding, there's just some things that I'm not accustomed to

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java File IO question :confused:

    The control field would be the first n bytes of the output file. n to be fixed at a later point in the design.
    The data to go into the control field:
    number of files (a byte allows 255)
    random number seed (a long)
    original file names (pascal format: leading byte has string length)


    Is the mixing at the byte level? One or more bytes from file 1, one or more bytes from file 2. etc
    Random number generators generate the same sequence of numbers for a given seed number.
    That repeatable sequence can be used to chose the number of bytes and the next file source for copying to the output file.

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java File IO question :confused:

    yes, the random number would be at the byte level, but also the file level I was thinking..
    Ex: x bytes from file y, repeating this as a pattern until there are no more bytes that remain.

    By the random number generators, do you mean the math.Random() function, or is there another way to get a random number?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java File IO question :confused:

    Here's the code I just used to verify my post:
          Random rand1 = new Random(12345);
          int[] rands1 = new int[30];
          for(int i=0; i < rands1.length; i++)
            rands1[i] = rand1.nextInt(50);
     
          System.out.println("rand1=" + Arrays.toString(rands1));
     
          Random rand2 = new Random(12345);
          int[] rands2 = new int[30];
          for(int i=0; i < rands2.length; i++)
            rands2[i] = rand2.nextInt(50);
     
          System.out.println("rand2=" + Arrays.toString(rands2));

    until there are no more bytes that remain.
    There could be a problem there. If files run out of bytes, when the random numbers says to get x bytes from file y....???

    Will take some more thought. But you were bored, so I'll leave it to you.

  9. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Java File IO question :confused:

    Quote Originally Posted by Norm View Post
    There could be a problem there. If files run out of bytes, when the random numbers says to get x bytes from file y....???

    Will take some more thought. But you were bored, so I'll leave it to you.
    If I'm not mistaken, something like
       Random rand(){
          Random rand1 = new Random(12345);
          int[] rands1 = new int[30];
          for(int i=0; i < rands1.length; i++)
            rands1[i] = rand1.nextInt(50);
          return rand1;
       }
       System.out.println("random 1=" + Arrays.toString(rand())+"\t random 2=" + Arrays.toString(rand()));
    Would be sufficient, am I correct in assuming this?
    Then have it check if random 2 > ToWrite.length with ToWrite being a byte array and if so, then adjust byte 2 so that it = ToWrite.length?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java File IO question :confused:

    The problem is what if the algorithm says to get x bytes from file y but file y has less than x bytes. How would you know when reconstructing the original files that there were less than x bytes from file y?

Similar Threads

  1. Ayudame! :confused:
    By wasaki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 22nd, 2010, 10:37 AM
  2. Confusion about Java development IDES
    By neo_2010 in forum The Cafe
    Replies: 4
    Last Post: July 7th, 2009, 03:14 PM
  3. [SOLVED] Prime number generator program in Java
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 27th, 2009, 12:08 PM
  4. Replies: 3
    Last Post: February 26th, 2009, 03:04 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM