-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileHandling_ReaderWriter.java
More file actions
32 lines (32 loc) · 1007 Bytes
/
Copy pathFileHandling_ReaderWriter.java
File metadata and controls
32 lines (32 loc) · 1007 Bytes
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
import java.io.*;
public class FileHandling_ReaderWriter
{
public static void main(String[] args)throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
String path = "D:\\Don\\Lab Programs\\Files\\";
System.out.print("Enter the filename: ");
String name = in.readLine();
path +=name;
File file = new File(path);
if(file.createNewFile())
System.out.println("New File Created: "+file.getName());
else
System.out.println("File already exists: "+file.getName());
FileWriter fw = new FileWriter(name);
System.out.println("Enter a string to add into file");
String s = in.readLine();
fw.write(s);
fw.flush();
fw.close();
System.out.println("File Written...");
System.out.println("Reading From File:");
FileReader fr = new FileReader(name);
char[] ch = new char[50];
fr.read(ch);
System.out.println(ch);
System.out.println("File Read...");
fr.close();
}
}