-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileHandlingPalindrome.java
82 lines (76 loc) · 2.7 KB
/
FileHandlingPalindrome.java
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class FileHandlingPalindrome {
// Function to check if a given string is a palindrome
public static Boolean palindrome(String c) {
int l = c.length();
for (int i = 0; i < l; i++) {
if (c.charAt(i) != c.charAt(l - i - 1)) {
return false;
}
}
return true;
}
public static void main(String args[]) {
Scanner P = new Scanner(System.in);
System.out.println("Enter Filename:");
String filename = P.nextLine();
File f = new File(filename);
try {
// Create a new file or check if it already exists
if (f.createNewFile()) {
System.out.println("File Created at:" + f.getAbsolutePath());
} else {
System.out.println("File already exists/created");
}
} catch (IOException e) {
System.out.println(e);
}
try {
String str;
// Open a FileOutputStream to write to the file
FileOutputStream fos = new FileOutputStream(filename);
System.out.println("Enter String to input into file:");
str = P.nextLine();
// Write the user-input string to the file
for (int i = 0; i < str.length(); i++) {
fos.write(str.charAt(i));
}
fos.close(); // Close the FileOutputStream after writing the content
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
try {
// Open a FileInputStream to read from the file
FileInputStream fis = new FileInputStream(filename);
StringBuilder s = new StringBuilder();
while (true) {
int c = fis.read();
if (c == -1) {
break;
} else {
char c1 = (char) c;
s.append(c1);
}
}
// Close the FileInputStream after reading the content
fis.close();
// Check if the content in the file is a palindrome
if (palindrome(s.toString())) {
System.out.println("File content is palindrome");
} else {
System.out.println("File content is not palindrome");
}
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
}