SlideShare une entreprise Scribd logo
1  sur  3
JAVA
Q2: Write a program that reads strings from the user and writes them to an output file called
userStrings.txt. Stop processing when the user enters the string "DONE". Do not write the
sentinel string ("DONE") to the output file. [15 points] Hint: For a complete example of file IO,
please see the attached TestData program.
Sample Output:
A string.
Another string.
Yet more text...
DONE
Solution
Answer
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
class WriteStringFile {
public static void main(String[] args) {
File file = new File("D:/userFile.txt");
String data;
Scanner input = new Scanner(System.in);
data = input.readLine();
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
if (data != "DONE") {
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} else {
System.out.println("User Enter the value Done.. So unable to write into file");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
JAVA Q2- Write a program that reads strings from the user and writes t.docx

Contenu connexe

Similaire à JAVA Q2- Write a program that reads strings from the user and writes t.docx

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
 
Write a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdfWrite a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdf
atulkapoor33
 
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
 
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
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
Alok Kumar
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
f3apparelsonline
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Don Bosco BSIT
 
The java program that reads an input file input.txt an.pdf
   The java program that reads an input file input.txt  an.pdf   The java program that reads an input file input.txt  an.pdf
The java program that reads an input file input.txt an.pdf
vichu19891
 

Similaire à JAVA Q2- Write a program that reads strings from the user and writes t.docx (20)

Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
 
Code red SUM
Code red SUMCode red SUM
Code red SUM
 
Bhaloo
BhalooBhaloo
Bhaloo
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
 
• GUI design using drag and drop feature of IDE(Net beans), • File IO
•	GUI design using drag and drop feature of IDE(Net beans), •	File IO•	GUI design using drag and drop feature of IDE(Net beans), •	File IO
• GUI design using drag and drop feature of IDE(Net beans), • File IO
 
Write a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdfWrite a program in java that asks a user for a file name and prints .pdf
Write a program in java that asks a user for a file name and prints .pdf
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
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
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
In JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdfIn JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdf
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
 
2. Write a program which will open an input file and write to an out.pdf
2. Write a program which will open an input file and write to an out.pdf2. Write a program which will open an input file and write to an out.pdf
2. Write a program which will open an input file and write to an out.pdf
 
The java program that reads an input file input.txt an.pdf
   The java program that reads an input file input.txt  an.pdf   The java program that reads an input file input.txt  an.pdf
The java program that reads an input file input.txt an.pdf
 

Plus de michael1810

John Corporation has three investments that are classified as noncontr.docx
John Corporation has three investments that are classified as noncontr.docxJohn Corporation has three investments that are classified as noncontr.docx
John Corporation has three investments that are classified as noncontr.docx
michael1810
 
Joe Frecaso was recently hired as the controller for Larson Cement Com.docx
Joe Frecaso was recently hired as the controller for Larson Cement Com.docxJoe Frecaso was recently hired as the controller for Larson Cement Com.docx
Joe Frecaso was recently hired as the controller for Larson Cement Com.docx
michael1810
 
For each of the abstract data types (ADTs) that we have analyzed in th.docx
For each of the abstract data types (ADTs) that we have analyzed in th.docxFor each of the abstract data types (ADTs) that we have analyzed in th.docx
For each of the abstract data types (ADTs) that we have analyzed in th.docx
michael1810
 
For each form of transport plot the value of overall benefits against.docx
For each form of transport plot the value of overall benefits against.docxFor each form of transport plot the value of overall benefits against.docx
For each form of transport plot the value of overall benefits against.docx
michael1810
 

Plus de michael1810 (17)

Judging from their relative positions in the Periodic Table- which ato.docx
Judging from their relative positions in the Periodic Table- which ato.docxJudging from their relative positions in the Periodic Table- which ato.docx
Judging from their relative positions in the Periodic Table- which ato.docx
 
John Corporation has three investments that are classified as noncontr.docx
John Corporation has three investments that are classified as noncontr.docxJohn Corporation has three investments that are classified as noncontr.docx
John Corporation has three investments that are classified as noncontr.docx
 
John Smith worked in the stockyard of a large building supply company-.docx
John Smith worked in the stockyard of a large building supply company-.docxJohn Smith worked in the stockyard of a large building supply company-.docx
John Smith worked in the stockyard of a large building supply company-.docx
 
John has tremendous technical skills and frequently comes up with valu.docx
John has tremendous technical skills and frequently comes up with valu.docxJohn has tremendous technical skills and frequently comes up with valu.docx
John has tremendous technical skills and frequently comes up with valu.docx
 
Joe Frecaso was recently hired as the controller for Larson Cement Com.docx
Joe Frecaso was recently hired as the controller for Larson Cement Com.docxJoe Frecaso was recently hired as the controller for Larson Cement Com.docx
Joe Frecaso was recently hired as the controller for Larson Cement Com.docx
 
Java Write a method that takes as a parameter a one dimensional array.docx
Java Write a method that takes as a parameter a one dimensional array.docxJava Write a method that takes as a parameter a one dimensional array.docx
Java Write a method that takes as a parameter a one dimensional array.docx
 
Java Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxJava Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docx
 
Java Language Which of the following statements is true- Select all th.docx
Java Language Which of the following statements is true- Select all th.docxJava Language Which of the following statements is true- Select all th.docx
Java Language Which of the following statements is true- Select all th.docx
 
It-'s virtualization topic Would anyone please expalin what is the di.docx
It-'s virtualization topic  Would anyone please expalin what is the di.docxIt-'s virtualization topic  Would anyone please expalin what is the di.docx
It-'s virtualization topic Would anyone please expalin what is the di.docx
 
For each pair of substance identify the substance that is likely to ha.docx
For each pair of substance identify the substance that is likely to ha.docxFor each pair of substance identify the substance that is likely to ha.docx
For each pair of substance identify the substance that is likely to ha.docx
 
For each of the requirements listed in the table below- 1- Please iden.docx
For each of the requirements listed in the table below- 1- Please iden.docxFor each of the requirements listed in the table below- 1- Please iden.docx
For each of the requirements listed in the table below- 1- Please iden.docx
 
For Contingent Liability- When do you disclose it- When do you record.docx
For Contingent Liability- When do you disclose it- When do you record.docxFor Contingent Liability- When do you disclose it- When do you record.docx
For Contingent Liability- When do you disclose it- When do you record.docx
 
For each of the abstract data types (ADTs) that we have analyzed in th.docx
For each of the abstract data types (ADTs) that we have analyzed in th.docxFor each of the abstract data types (ADTs) that we have analyzed in th.docx
For each of the abstract data types (ADTs) that we have analyzed in th.docx
 
For each form of transport plot the value of overall benefits against.docx
For each form of transport plot the value of overall benefits against.docxFor each form of transport plot the value of overall benefits against.docx
For each form of transport plot the value of overall benefits against.docx
 
Fluid Mechanics The water surface is at the position shown at t - 0 se.docx
Fluid Mechanics The water surface is at the position shown at t - 0 se.docxFluid Mechanics The water surface is at the position shown at t - 0 se.docx
Fluid Mechanics The water surface is at the position shown at t - 0 se.docx
 
Fluctuation in inflation can occur in the short or long term- For exam.docx
Fluctuation in inflation can occur in the short or long term- For exam.docxFluctuation in inflation can occur in the short or long term- For exam.docx
Fluctuation in inflation can occur in the short or long term- For exam.docx
 
First National Bank charges 11-2 percent compounded monthly on its bus (1).docx
First National Bank charges 11-2 percent compounded monthly on its bus (1).docxFirst National Bank charges 11-2 percent compounded monthly on its bus (1).docx
First National Bank charges 11-2 percent compounded monthly on its bus (1).docx
 

Dernier

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
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Ữ Â...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

JAVA Q2- Write a program that reads strings from the user and writes t.docx

  • 1. JAVA Q2: Write a program that reads strings from the user and writes them to an output file called userStrings.txt. Stop processing when the user enters the string "DONE". Do not write the sentinel string ("DONE") to the output file. [15 points] Hint: For a complete example of file IO, please see the attached TestData program. Sample Output: A string. Another string. Yet more text... DONE Solution Answer import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; class WriteStringFile { public static void main(String[] args) { File file = new File("D:/userFile.txt"); String data;
  • 2. Scanner input = new Scanner(System.in); data = input.readLine(); try (FileOutputStream fop = new FileOutputStream(file)) { // if file doesn't exists, then create it if (!file.exists()) { file.createNewFile(); } if (data != "DONE") { // get the content in bytes byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); } else { System.out.println("User Enter the value Done.. So unable to write into file"); } } catch (IOException e) { e.printStackTrace(); } } }