-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathPrintThyself.java
More file actions
61 lines (42 loc) · 1.43 KB
/
PrintThyself.java
File metadata and controls
61 lines (42 loc) · 1.43 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
/* File: PrintThyself.java
* ---------------------------------
* დაწერეთ პროგრამა რომელიც წაიკითხავს თავის თავს
* ფაილიდან და დაბეჭდავს კონსოლში
*/
import java.io.BufferedReader;
import java.io.FileReader;
import acm.graphics.GOval;
import acm.program.*;
public class ConsoleProgrammSample extends ConsoleProgram {
private static final String FILE_NOT_FOUND_MESSAGE = "- ERROR - Can't Locate Recevied File - ERROR -";
private String readFile(String fileName) {
//Trying To Open File
try {
String result = "";
BufferedReader br = new BufferedReader(new FileReader(fileName));
while(true) {
//Reading Every Line Of File
String newLine = br.readLine();
//If newLine == null, It Means That There Are No More Line To Read, So While Loop Breaks
if(newLine == null) break;
//Add newLine To "result"
result += newLine + '\n';
}
br.close();
//Return Result
return result;
}catch(Exception e) {
System.out.print(e);
}
//Return Error Message, If Code Catches Errors
return FILE_NOT_FOUND_MESSAGE;
}
public void run(){
//Read File Content
String fileName = readLine("Enter FileName: ");
String result = readFile(fileName);
//Print File Content
println("Received File Contains Text: ");
println(result);
}
}