SlideShare a Scribd company logo
DisplayBook.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class DisplayBook {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
File file = new File("D:books.txt");
if(file.exists()){
System.out.println("File exists");
FileInputStream fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
System.out.print((char) content);
}
fis.close();
}
else{
System.out.println("File does not exist");
String titles = "";
FileOutputStream fop = new FileOutputStream(file);
while(true){
System.out.println("Enter Book Title (none to exit):");
String title = scan.nextLine();
if(title.equalsIgnoreCase("none")){
break;
}
else{
titles = titles + title +" ";
}
}
byte[] contentInBytes = titles.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
}
}
}
Output:
File does not exist
Enter Book Title (none to exit):
Physics
Enter Book Title (none to exit):
Chemistry
Enter Book Title (none to exit):
Science
Enter Book Title (none to exit):
Maths
Enter Book Title (none to exit):
none
--------------------------------------
File exists
Physics
Chemistry
Science
Maths
Solution
DisplayBook.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class DisplayBook {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
File file = new File("D:books.txt");
if(file.exists()){
System.out.println("File exists");
FileInputStream fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
System.out.print((char) content);
}
fis.close();
}
else{
System.out.println("File does not exist");
String titles = "";
FileOutputStream fop = new FileOutputStream(file);
while(true){
System.out.println("Enter Book Title (none to exit):");
String title = scan.nextLine();
if(title.equalsIgnoreCase("none")){
break;
}
else{
titles = titles + title +" ";
}
}
byte[] contentInBytes = titles.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
}
}
}
Output:
File does not exist
Enter Book Title (none to exit):
Physics
Enter Book Title (none to exit):
Chemistry
Enter Book Title (none to exit):
Science
Enter Book Title (none to exit):
Maths
Enter Book Title (none to exit):
none
--------------------------------------
File exists
Physics
Chemistry
Science
Maths

More Related Content

DOCX
DOCX
Code red SUM
PDF
Create a text file named lnfo.txt. Enter the following informatio.pdf
PPTX
Session 23 - JDBC
PPSX
DOC
Inheritance
PPTX
Understanding java streams
PDF
import required package file import java.io.File; The Filed.pdf
Code red SUM
Create a text file named lnfo.txt. Enter the following informatio.pdf
Session 23 - JDBC
Inheritance
Understanding java streams
import required package file import java.io.File; The Filed.pdf

Similar to DisplayBook.java import java.io.File; import java.io.FileInput.pdf (20)

PDF
PDF
The Java Program for the above given isimport java.io.File;impo.pdf
PDF
2. Write a program which will open an input file and write to an out.pdf
PPT
Java Input Output and File Handling
DOCX
I can not get my code to comply it gets mad that I have run declare mo.docx
PDF
5. Ввод-вывод, доступ к файловой системе
PDF
26 io -ii file handling
PPT
Java căn bản - Chapter12
PPT
Chapter 12 - File Input and Output
PPTX
Input output files in java
PPT
File Input and Output in Java Programing language
DOCX
Know how to redirect input and output- and know how to append to an ex.docx
PDF
Change the code in Writer.java only to get it working. Must contain .pdf
DOCX
FileHandling.docx
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
File Handling in Java.pdf
PPTX
File Handling.pptx
PPT
04_1.Exceptionssssssssssssssssssss..pptt
PDF
Java Programming - 06 java file io
The Java Program for the above given isimport java.io.File;impo.pdf
2. Write a program which will open an input file and write to an out.pdf
Java Input Output and File Handling
I can not get my code to comply it gets mad that I have run declare mo.docx
5. Ввод-вывод, доступ к файловой системе
26 io -ii file handling
Java căn bản - Chapter12
Chapter 12 - File Input and Output
Input output files in java
File Input and Output in Java Programing language
Know how to redirect input and output- and know how to append to an ex.docx
Change the code in Writer.java only to get it working. Must contain .pdf
FileHandling.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
File Handling in Java.pdf
File Handling.pptx
04_1.Exceptionssssssssssssssssssss..pptt
Java Programming - 06 java file io

More from sudheerforce (20)

PDF
the bond between Si and O .pdf
PDF
Step1 Equivalents of NaOH= .1146 x[19.25-.55]x10^.pdf
PDF
step 1 Br2 under UV light step 2 NMe3 Solu.pdf
PDF
Since column chromatography substances are separa.pdf
PDF
Option III .pdf
PDF
Oh group is removed by Br yes ther can be two ty.pdf
PDF
Moles of Na2O2 = 2578=0.32 Moles of H2O = 12.11.pdf
PDF
Is this a reduction oxidation reaction If so, wh.pdf
PDF
HNO3========= H+ (aq) + NO3-(aq) [H+] = 0.0578 .pdf
PDF
Valley fever is the initial form of coccidioidomycosis infection. Th.pdf
PDF
Yes,The interthalamic adhesion (also known as the mass intermedia or.pdf
PDF
Think of the X and Y as totally different events. The expected value.pdf
PDF
There are two source terms in Maxwell’s equations, the static charge.pdf
PDF
The xenate ion, [HXeO4]-, slowly decomposes in alkaline solution to .pdf
PDF
The strengths of the Indian Parliamentary System of governanace1..pdf
PDF
Pulse code modulationPulse code modulation is a digital sign of a.pdf
PDF
PFA JAVA programimport java.io.; import java.util.; import .pdf
PDF
D) four s bonds Pi bonds are double bonds as carb.pdf
PDF
out of Africa hypothesis out of Africa hypothesis is about .pdf
PDF
Leukocytes or WBC They are of two typesGranulocytes Neutrophils.pdf
the bond between Si and O .pdf
Step1 Equivalents of NaOH= .1146 x[19.25-.55]x10^.pdf
step 1 Br2 under UV light step 2 NMe3 Solu.pdf
Since column chromatography substances are separa.pdf
Option III .pdf
Oh group is removed by Br yes ther can be two ty.pdf
Moles of Na2O2 = 2578=0.32 Moles of H2O = 12.11.pdf
Is this a reduction oxidation reaction If so, wh.pdf
HNO3========= H+ (aq) + NO3-(aq) [H+] = 0.0578 .pdf
Valley fever is the initial form of coccidioidomycosis infection. Th.pdf
Yes,The interthalamic adhesion (also known as the mass intermedia or.pdf
Think of the X and Y as totally different events. The expected value.pdf
There are two source terms in Maxwell’s equations, the static charge.pdf
The xenate ion, [HXeO4]-, slowly decomposes in alkaline solution to .pdf
The strengths of the Indian Parliamentary System of governanace1..pdf
Pulse code modulationPulse code modulation is a digital sign of a.pdf
PFA JAVA programimport java.io.; import java.util.; import .pdf
D) four s bonds Pi bonds are double bonds as carb.pdf
out of Africa hypothesis out of Africa hypothesis is about .pdf
Leukocytes or WBC They are of two typesGranulocytes Neutrophils.pdf

Recently uploaded (20)

PPTX
master seminar digital applications in india
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
RMMM.pdf make it easy to upload and study
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 Đ...
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
VCE English Exam - Section C Student Revision Booklet
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pre independence Education in Inndia.pdf
Microbial diseases, their pathogenesis and prophylaxis
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O7-L3 Supply Chain Operations - ICLT Program
Pharma ospi slides which help in ospi learning
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

DisplayBook.java import java.io.File; import java.io.FileInput.pdf

  • 1. DisplayBook.java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class DisplayBook { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); File file = new File("D:books.txt"); if(file.exists()){ System.out.println("File exists"); FileInputStream fis = new FileInputStream(file); int content; while ((content = fis.read()) != -1) { System.out.print((char) content); } fis.close(); } else{ System.out.println("File does not exist"); String titles = ""; FileOutputStream fop = new FileOutputStream(file); while(true){ System.out.println("Enter Book Title (none to exit):"); String title = scan.nextLine(); if(title.equalsIgnoreCase("none")){ break; } else{ titles = titles + title +" "; }
  • 2. } byte[] contentInBytes = titles.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); } } } Output: File does not exist Enter Book Title (none to exit): Physics Enter Book Title (none to exit): Chemistry Enter Book Title (none to exit): Science Enter Book Title (none to exit): Maths Enter Book Title (none to exit): none -------------------------------------- File exists Physics Chemistry Science Maths Solution DisplayBook.java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
  • 3. import java.util.Scanner; public class DisplayBook { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); File file = new File("D:books.txt"); if(file.exists()){ System.out.println("File exists"); FileInputStream fis = new FileInputStream(file); int content; while ((content = fis.read()) != -1) { System.out.print((char) content); } fis.close(); } else{ System.out.println("File does not exist"); String titles = ""; FileOutputStream fop = new FileOutputStream(file); while(true){ System.out.println("Enter Book Title (none to exit):"); String title = scan.nextLine(); if(title.equalsIgnoreCase("none")){ break; } else{ titles = titles + title +" "; } } byte[] contentInBytes = titles.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); }
  • 4. } } Output: File does not exist Enter Book Title (none to exit): Physics Enter Book Title (none to exit): Chemistry Enter Book Title (none to exit): Science Enter Book Title (none to exit): Maths Enter Book Title (none to exit): none -------------------------------------- File exists Physics Chemistry Science Maths