-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisplayBook.java
More file actions
85 lines (61 loc) · 2.11 KB
/
Copy pathDisplayBook.java
File metadata and controls
85 lines (61 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Programmer: Edgar Colin
// Student ID: EDG2140344
// Class: CIS263AA JAVA II Class # 12303
// Instructor: Michael Parmeley
// Chapter: 13
// Problem: 3
// Part:
// Filename: FileStatistics1.java
import java.nio.file.*; import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class DisplayBook
{
public static void main(String[] args)
{
String bookFilename;
String bookTitle;
Scanner Filename = new Scanner(System.in);
System.out.print("Enter the title of the book you want to read: >> ");
bookTitle = Filename.nextLine();
bookFilename = bookTitle + ".txt";
//Reading
Path file =
Paths.get(bookFilename);
String s = "";
//Extra for creating and writing to the file
try
{
InputStream input = new
BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new
BufferedReader(new InputStreamReader(input)); s = reader.readLine();
while(s != null)
{
System.out.println(s);
s = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
System.out.println("Message: The book title entered does not match"
+ " any records, a file " + bookFilename + " has been created.\n" +
"Location PATH: " + file); // Full path diplayed if not saved in program dir
String title = bookTitle;
try
{
OutputStream output = new
BufferedOutputStream(Files.newOutputStream(file, CREATE));
BufferedWriter writer = new
BufferedWriter(new OutputStreamWriter(output));
writer.write(title,0,title.length());
writer.close();
}
catch(Exception e2)
{
System.out.println("Message: " + e);
}
}
}
}