SlideShare a Scribd company logo
1 of 2
Download to read offline
IN JAVA Write a program to create a binary data file named RandomInt.dat that consists of 10
random integer numbers using DataOutputStream. Use DataInputStream to read the integers
from RandomInt.dat just created and print them on the console in the same program.
Solution
DataInputStreamTest.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
public class DataInputStreamTest {
public static void main(String[] args) {
String strFilePath = "D:RandomInt.dat";
Random r = new Random();
try
{
FileOutputStream outputStr = new FileOutputStream(strFilePath);
DataOutputStream outputStrData = new DataOutputStream(outputStr);
for(int i=0; i<10; i++){
outputStrData.writeInt(r.nextInt(100)+1);
outputStrData.writeChar('t');
}
outputStrData.flush();
outputStrData.close();
outputStr.close();
FileInputStream inputStr = new FileInputStream(strFilePath);
DataInputStream inputStrData = new DataInputStream(inputStr);
int count = 0;
while(count<10){
int i = inputStrData.readInt();
inputStrData.readChar();
count++;
System.out.println( i);
}
outputStrData.flush();
inputStrData.close();
outputStrData.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
}
Output:
65
8
95
69
1
77
16
97
85
1

More Related Content

Similar to IN JAVA Write a program to create a binary data file named RandomInt.pdf

Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
Vince Vo
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docx
noreendchesterton753
 
The Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdfThe Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdf
anjanacottonmills
 
Android Studio Assignment HelpCan someone who is familiar with And.pdf
Android Studio Assignment HelpCan someone who is familiar with And.pdfAndroid Studio Assignment HelpCan someone who is familiar with And.pdf
Android Studio Assignment HelpCan someone who is familiar with And.pdf
feelinggift
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
Alok Kumar
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
Khurshid Asghar
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
Greg.Helton
 
I can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docxI can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docx
hamblymarta
 
Soundreader.classpathSoundreader.project Soundre.docx
Soundreader.classpathSoundreader.project  Soundre.docxSoundreader.classpathSoundreader.project  Soundre.docx
Soundreader.classpathSoundreader.project Soundre.docx
whitneyleman54422
 

Similar to IN JAVA Write a program to create a binary data file named RandomInt.pdf (20)

Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Write a java program that allows the user to input the name of a fil.docx
 Write a java program that allows the user to input  the name of a fil.docx Write a java program that allows the user to input  the name of a fil.docx
Write a java program that allows the user to input the name of a fil.docx
 
Write a java program that allows the user to input the name of a file.docx
Write a java program that allows the user to input  the name of a file.docxWrite a java program that allows the user to input  the name of a file.docx
Write a java program that allows the user to input the name of a file.docx
 
The Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdfThe Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdf
 
Android Studio Assignment HelpCan someone who is familiar with And.pdf
Android Studio Assignment HelpCan someone who is familiar with And.pdfAndroid Studio Assignment HelpCan someone who is familiar with And.pdf
Android Studio Assignment HelpCan someone who is familiar with And.pdf
 
Scalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedInScalable and Flexible Machine Learning With Scala @ LinkedIn
Scalable and Flexible Machine Learning With Scala @ LinkedIn
 
Lab4
Lab4Lab4
Lab4
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
54240326 (1)
54240326 (1)54240326 (1)
54240326 (1)
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
 
Android studio plugin 作ってみた 〜 create intent method generator 〜
Android studio plugin 作ってみた 〜 create intent method generator 〜Android studio plugin 作ってみた 〜 create intent method generator 〜
Android studio plugin 作ってみた 〜 create intent method generator 〜
 
Write a java program that reads a number N as input and displays the.pdf
Write a java program that reads a number N as input and displays the.pdfWrite a java program that reads a number N as input and displays the.pdf
Write a java program that reads a number N as input and displays the.pdf
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
 
I can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docxI can not get my code to comply it gets mad that I have run declare mo.docx
I can not get my code to comply it gets mad that I have run declare mo.docx
 
Soundreader.classpathSoundreader.project Soundre.docx
Soundreader.classpathSoundreader.project  Soundre.docxSoundreader.classpathSoundreader.project  Soundre.docx
Soundreader.classpathSoundreader.project Soundre.docx
 
Complete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docxComplete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docx
 
java
javajava
java
 

More from herminaherman

Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdfWild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
herminaherman
 
What data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdfWhat data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdf
herminaherman
 
Use provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdfUse provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdf
herminaherman
 
Private and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdfPrivate and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdf
herminaherman
 
I wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfI wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdf
herminaherman
 
I keep on get a redefinition error and an undefined error.Customer.pdf
I keep on get a redefinition error and an undefined error.Customer.pdfI keep on get a redefinition error and an undefined error.Customer.pdf
I keep on get a redefinition error and an undefined error.Customer.pdf
herminaherman
 

More from herminaherman (20)

Write an equation for the height of the point P above the ground as .pdf
Write an equation for the height of the point P above the ground as .pdfWrite an equation for the height of the point P above the ground as .pdf
Write an equation for the height of the point P above the ground as .pdf
 
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdfWild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
 
Which of the following is considered to be a distributed denial of s.pdf
Which of the following is considered to be a distributed denial of s.pdfWhich of the following is considered to be a distributed denial of s.pdf
Which of the following is considered to be a distributed denial of s.pdf
 
Which of the folllowing statements about the solubility of 1- propan.pdf
Which of the folllowing statements about the solubility of 1- propan.pdfWhich of the folllowing statements about the solubility of 1- propan.pdf
Which of the folllowing statements about the solubility of 1- propan.pdf
 
What type of mutation that impacts splicing is a significant contrib.pdf
What type of mutation that impacts splicing is a significant contrib.pdfWhat type of mutation that impacts splicing is a significant contrib.pdf
What type of mutation that impacts splicing is a significant contrib.pdf
 
What data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdfWhat data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdf
 
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdfUnlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
 
Use provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdfUse provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdf
 
Two four-sided dice are rolled. One die has the letters A through D..pdf
Two four-sided dice are rolled. One die has the letters A through D..pdfTwo four-sided dice are rolled. One die has the letters A through D..pdf
Two four-sided dice are rolled. One die has the letters A through D..pdf
 
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdf
The job of a CODEC (CoderDecoder) is to  Convert an analog voice si.pdfThe job of a CODEC (CoderDecoder) is to  Convert an analog voice si.pdf
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdf
 
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdf
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdfQuestion 4 Constructive retirement is when A. P buys Ss common stoc.pdf
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdf
 
Private and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdfPrivate and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdf
 
Name the two standards that are supported by major DBMSs for distrib.pdf
Name the two standards that are supported by major DBMSs for distrib.pdfName the two standards that are supported by major DBMSs for distrib.pdf
Name the two standards that are supported by major DBMSs for distrib.pdf
 
Material Manufacturing Name 3 ways that the properties of thermosett.pdf
Material Manufacturing Name 3 ways that the properties of thermosett.pdfMaterial Manufacturing Name 3 ways that the properties of thermosett.pdf
Material Manufacturing Name 3 ways that the properties of thermosett.pdf
 
Mean can be computed for variables with ordinal-level measurements..pdf
Mean can be computed for variables with ordinal-level measurements..pdfMean can be computed for variables with ordinal-level measurements..pdf
Mean can be computed for variables with ordinal-level measurements..pdf
 
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdfLet u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
 
IV. Which compound would have the higher water solubility, tetrahydro.pdf
IV. Which compound would have the higher water solubility, tetrahydro.pdfIV. Which compound would have the higher water solubility, tetrahydro.pdf
IV. Which compound would have the higher water solubility, tetrahydro.pdf
 
Interseting discussion topic,want to chime inDescribe a symbol .pdf
Interseting discussion topic,want to chime inDescribe a symbol .pdfInterseting discussion topic,want to chime inDescribe a symbol .pdf
Interseting discussion topic,want to chime inDescribe a symbol .pdf
 
I wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfI wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdf
 
I keep on get a redefinition error and an undefined error.Customer.pdf
I keep on get a redefinition error and an undefined error.Customer.pdfI keep on get a redefinition error and an undefined error.Customer.pdf
I keep on get a redefinition error and an undefined error.Customer.pdf
 

Recently uploaded

Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

IN JAVA Write a program to create a binary data file named RandomInt.pdf

  • 1. IN JAVA Write a program to create a binary data file named RandomInt.dat that consists of 10 random integer numbers using DataOutputStream. Use DataInputStream to read the integers from RandomInt.dat just created and print them on the console in the same program. Solution DataInputStreamTest.java import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; public class DataInputStreamTest { public static void main(String[] args) { String strFilePath = "D:RandomInt.dat"; Random r = new Random(); try { FileOutputStream outputStr = new FileOutputStream(strFilePath); DataOutputStream outputStrData = new DataOutputStream(outputStr); for(int i=0; i<10; i++){ outputStrData.writeInt(r.nextInt(100)+1); outputStrData.writeChar('t'); } outputStrData.flush(); outputStrData.close(); outputStr.close(); FileInputStream inputStr = new FileInputStream(strFilePath);
  • 2. DataInputStream inputStrData = new DataInputStream(inputStr); int count = 0; while(count<10){ int i = inputStrData.readInt(); inputStrData.readChar(); count++; System.out.println( i); } outputStrData.flush(); inputStrData.close(); outputStrData.close(); } catch(FileNotFoundException fe) { System.out.println("FileNotFoundException : " + fe); } catch(IOException ioe) { System.out.println("IOException : " + ioe); } } } Output: 65 8 95 69 1 77 16 97 85 1