SlideShare a Scribd company logo
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

More Related Content

DOCX
PAGE 1Input output for a file tutorialStreams and File IOI.docx
PPTX
File Handling.pptx
PPTX
Understanding java streams
PDF
IN JAVA Write a program to create a binary data file named RandomInt.pdf
DOCX
Write a java program that allows the user to input the name of a fil.docx
DOCX
Write a java program that allows the user to input the name of a file.docx
PDF
The Java Program for the above given isimport java.io.File;impo.pdf
DOCX
Write a program that will count the number of characters- words- and l.docx
PAGE 1Input output for a file tutorialStreams and File IOI.docx
File Handling.pptx
Understanding java streams
IN JAVA Write a program to create a binary data file named RandomInt.pdf
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
The Java Program for the above given isimport java.io.File;impo.pdf
Write a program that will count the number of characters- words- and l.docx

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

PPTX
Input output files in java
PPT
M251_Meeting 7 (Exception Handling and Text IO).ppt
PDF
Hi, I need help with a java programming project. specifically practi.pdf
DOC
Object oriented programming la bmanual jntu
DOCX
DOCX
Code red SUM
PPTX
ExtraFileIO.pptx
DOCX
• GUI design using drag and drop feature of IDE(Net beans), • File IO
PDF
Write a program in java that asks a user for a file name and prints .pdf
PPT
Java API, Exceptions and IO
DOCX
I can not get my code to comply it gets mad that I have run declare mo.docx
PPT
Java căn bản - Chapter12
PPT
Chapter 12 - File Input and Output
PDF
Important java programs(collection+file)
PDF
Use Java to program the following.1. Create public java class name.pdf
PPT
Jedi Slides Intro2 Chapter12 Advanced Io Streams
PDF
In JavaWrite a program that reads a file and counts how many line.pdf
PPT
Basic input-output-v.1.1
PDF
2. Write a program which will open an input file and write to an out.pdf
Input output files in java
M251_Meeting 7 (Exception Handling and Text IO).ppt
Hi, I need help with a java programming project. specifically practi.pdf
Object oriented programming la bmanual jntu
Code red SUM
ExtraFileIO.pptx
• 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
Java API, Exceptions and IO
I can not get my code to comply it gets mad that I have run declare mo.docx
Java căn bản - Chapter12
Chapter 12 - File Input and Output
Important java programs(collection+file)
Use Java to program the following.1. Create public java class name.pdf
Jedi Slides Intro2 Chapter12 Advanced Io Streams
In JavaWrite a program that reads a file and counts how many line.pdf
Basic input-output-v.1.1
2. Write a program which will open an input file and write to an out.pdf
Ad

More from michael1810 (17)

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

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Basic Mud Logging Guide for educational purpose
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Lesson notes of climatology university.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
GDM (1) (1).pptx small presentation for students
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
master seminar digital applications in india
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Basic Mud Logging Guide for educational purpose
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Lesson notes of climatology university.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
GDM (1) (1).pptx small presentation for students
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Supply Chain Operations Speaking Notes -ICLT Program
TR - Agricultural Crops Production NC III.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Computing-Curriculum for Schools in Ghana
master seminar digital applications in india
STATICS OF THE RIGID BODIES Hibbelers.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(); } } }