Results 1 to 11 of 11
- 08-21-2013, 06:45 AM #1
Member
- Join Date
- Jun 2013
- Posts
- 71
- Rep Power
- 0
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"); } } }
- 08-21-2013, 08:10 AM #2
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)
- 08-21-2013, 11:17 PM #3
Member
- Join Date
- Jun 2013
- Posts
- 71
- Rep Power
- 0
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????
- 08-21-2013, 11:47 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-24-2013, 06:30 AM #5
Member
- Join Date
- Jun 2013
- Posts
- 71
- Rep Power
- 0
Re: write a file same as first, but make in binary!?! whatttt??
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
- 08-24-2013, 06:41 AM #6
Member
- Join Date
- Jun 2013
- Posts
- 71
- Rep Power
- 0
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.
- 08-24-2013, 08:24 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 08-24-2013, 05:19 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-25-2013, 02:42 AM #9
Member
- Join Date
- Jun 2013
- Posts
- 71
- Rep Power
- 0
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!
- 08-25-2013, 03:55 AM #10
Member
- Join Date
- Jun 2013
- Posts
- 71
- Rep Power
- 0
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"
- 08-25-2013, 08:23 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: write a file same as first, but make in binary!?! whatttt??
Build a wall around Donald Trump; I'll pay for it.
Similar Threads
-
Problem with File I/O, writing information to a binary file.
By Trithan in forum New To JavaReplies: 1Last Post: 08-02-2012, 04:01 AM -
Make the TextField unable to write on
By Josep_16 in forum Java AppletsReplies: 4Last Post: 08-21-2011, 09:06 AM -
Help!! Trying to write a program that make a square move with keyboard and mouse.
By ohkelvin in forum New To JavaReplies: 9Last Post: 06-17-2011, 06:10 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-10-2009, 12:31 AM -
[SOLVED] write&read a binary file
By tOpach in forum New To JavaReplies: 9Last Post: 05-01-2009, 01:28 AM
Bookmarks