Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2013
    Posts
    70
    Rep Power
    0

    Default write a file same as first, but make in binary!?! whatttt??

    hello! so the full prompt is: "extend the previous example to write a second file at the same time as the first, but containing the month, day, year values as binary data. I DONT KNOW WHAT THIS IS ASKING OF ME!!!!!!!! anyways, this is what i have so far. any help will be helpful!
    Java Code:
    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateFileWriter {
    
        public static void main(String[] args) {
            // TODO code application logic here
            int[] dataValue = {28, 3, 1997, 20, 2, 2012, 16, 12, 2011, 9, 10, 1867, 1, 1, 5045};
            //String[] month = {"January, February, March, April, May, June, July, Augest,Septemeber, October, November, December"};
            //String[] singleDate = {"1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st"};
            for (int i = 0; i < dataValue.length; i = i + 3) {
                int day = dataValue[i];
                int month = (dataValue[i + 1]) - 1;
                int year = (dataValue[i + 2]) - 1900;
                Date date = new Date(year, month, day);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM dd YYYY");
                String formattedDate = simpleDateFormat.format(date);
                System.out.println(formattedDate);
                boolean myDir = (new File("C:/Jason/Test")).mkdirs();
                try {
                    File file = new File("C:/Jason/Test", "example.txt");
                    BufferedWriter output = new BufferedWriter(new FileWriter(file, true));
                    output.write(formattedDate);
                    output.newLine();
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //System.out.println("hello");
            }
        }
    }

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: write a file same as first, but make in binary!?! whatttt??

    you can use DatatStreams to write primitive types in a binary file . Here is the complete description how you can do it .
    Data Streams (The Java™ Tutorials > Essential Classes > Basic I/O)

  3. #3
    Join Date
    Jun 2013
    Posts
    70
    Rep Power
    0

    Default Re: write a file same as first, but make in binary!?! whatttt??

    ok im confused. anyone else explain this to me a little more precise????

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: write a file same as first, but make in binary!?! whatttt??

    Say you have an int value of 1234567. If you write that out to the terminal or to a text file (like one that notepad can read), you are actually writing out 56 bits of information (assuming 8 bits for each character). If you write it out in binary, you are writing out only 32 bits of information for each integer value. So you need to write out your types using one of the DataStream methods as previously described.

    Here is what seems to be a reasonable article and a must read if you are serious learning about computer science in general.
    Ascii vs. Binary Files

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Jun 2013
    Posts
    70
    Rep Power
    0

    Default Re: write a file same as first, but make in binary!?! whatttt??

    Quote Originally Posted by jim829 View Post
    Say you have an int value of 1234567. If you write that out to the terminal or to a text file (like one that notepad can read), you are actually writing out 56 bits of information (assuming 8 bits for each character). If you write it out in binary, you are writing out only 32 bits of information for each integer value. So you need to write out your types using one of the DataStream methods as previously described.

    Here is what seems to be a reasonable article and a must read if you are serious learning about computer science in general.
    Ascii vs. Binary Files

    Regards,
    Jim
    hello Jim, i have read both of the articles, but out of the three methods that data streams offers in the first article, none of them actually prints out strings in binary data, as in 0s and 1s. So, im not sure what i need to do now. Is there like a method that auto converts strings to 0s and 1s??
    -HelpingIsCaring

  6. #6
    Join Date
    Jun 2013
    Posts
    70
    Rep Power
    0

    Default Re: write a file same as first, but make in binary!?! whatttt??

    Anyone else who reads this forum can help too!!!!!!!! Plzzzzzzzzzzzz. Its been like three dayssssss.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default Re: write a file same as first, but make in binary!?! whatttt??

    Quote Originally Posted by HelpingIsCaring View Post
    Anyone else who reads this forum can help too!!!!!!!! Plzzzzzzzzzzzz. Its been like three dayssssss.
    I couldn't explain it any better than Jim did (see above); what is it you still don't understand? b.t.w. your 'z' and 's' keys are stuck.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: write a file same as first, but make in binary!?! whatttt??

    Check out the DataOutputStream API. All the methods such as writeInt, writeLong, etc. write out binary information. Here is the difference:

    If you simply wrote out 123 as printable, 8 bit ASCII, characters to a file, it would look like the following in hex.

    313233 (31 = '1', 32 = '2', 33 = '3') total number of bits = 24

    If you wrote it out in binary it would look like this.

    7B (i.e 7 * 16 = 112 + 11 = 123 or 8 bits (assuming you use the method writeByte()).

    Note that writing something out in binary doesn't mean explicitly writing it out in 1's and 0's. All data is composed of 1's and 0's and is inherently binary. When instructed to write out something in binary vs text that is more related to presentation and interpretation of the data.

    If I have confused you I apologize but this is just one of those topics you need to read about and try some stuff until you get the feel for it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Join Date
    Jun 2013
    Posts
    70
    Rep Power
    0

    Default Re: write a file same as first, but make in binary!?! whatttt??

    OOOOOOOOOOOOOOOOHHHHHHHHHHHHHH. I thought writing String dates in binary meant 0s and 1s. I guess not. THAT CLEARED THINGS UP!

  10. #10
    Join Date
    Jun 2013
    Posts
    70
    Rep Power
    0

    Default Re: write a file same as first, but make in binary!?! whatttt??

    well its already done now. thanks you guys for helping. You are all very caring because "HelpingIsCaring"

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default Re: write a file same as first, but make in binary!?! whatttt??

    Quote Originally Posted by HelpingIsCaring View Post
    OOOOOOOOOOOOOOOOHHHHHHHHHHHHHH. I thought writing String dates in binary meant 0s and 1s. I guess not. THAT CLEARED THINGS UP!
    Well, writing binary stuff does mean writing 0s and 1s; everything is, even if you write characters. The 0s and 1s written are not the characters '0' and '1' but that can't be communicated over a text based forum like this very easily.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2012, 03:01 AM
  2. Make the TextField unable to write on
    By Josep_16 in forum Java Applets
    Replies: 4
    Last Post: 08-21-2011, 08:06 AM
  3. Replies: 9
    Last Post: 06-17-2011, 05:10 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. [SOLVED] write&amp;read a binary file
    By tOpach in forum New To Java
    Replies: 9
    Last Post: 05-01-2009, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •