-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.java
More file actions
101 lines (93 loc) · 2.66 KB
/
File.java
File metadata and controls
101 lines (93 loc) · 2.66 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Scanner;
public class Files {
public static void main(String args[]){
System.out.print("Enter Text: ");
Scanner scan = new Scanner(System.in);
String text = scan.nextLine();
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("newfile.txt");
writer = new BufferedWriter(fWriter);
writer.write(text);
writer.newLine();
writer.close();
System.err.println("Your input of " + text.length() + " characters was saved.");
} catch (Exception e) {
System.out.println("Error!");
}
Scanner in= new Scanner(System.in);
str pass= in.nextInt(); //input password
// Specify the path of the file here
File file = new File("D:\\JAVA\\newfile.txt");
BufferedInputStream bis = null;
FileInputStream fis = null;
try {
// FileInputStream to read the file
fis = new FileInputStream(file);
/*
* Passed the FileInputStream to BufferedInputStream For Fast read using the
* buffer array.
*/
bis = new BufferedInputStream(fis);
/*
* available() method of BufferedInputStream returns 0 when there are no more
* bytes present in the file to be read
*/
while (bis.available() > 0) {
System.out.print((char) bis.read());
}
} catch (FileNotFoundException fnfe) {
System.out.println("The specified file not found" + fnfe);
} catch (IOException ioe) {
System.out.println("I/O Exception: " + ioe);
} finally {
try {
if (bis != null && fis != null) {
fis.close();
bis.close();
}
} catch (IOException ioe) {
System.out.println("Error in InputStream close(): " + ioe);
}
}
str Pass2= in.nextInt();
if(pass==Pass2)
{
try {
String mycontent = in.nextLine();
// Specify the file name and path here
File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt");
/*
* This logic will make sure that the file gets created if it is not present at
* the specified location
*/
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(mycontent);
System.out.println("File written Successfully");
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (Exception ex) {
System.out.println("Error in closing the BufferedWriter" + ex);
}
}
}
else
{ System.out.println("Wrong Password");
}
}
}