-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexp9.java
47 lines (40 loc) · 1.12 KB
/
exp9.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
//file handling program in Java with reader/writer.
// file sample.txt and store the inputs given from the keyboard.
//after execution of program a new file is generated with name "new-sample.txt" with the contents from sample.txt.
import java.util.*;
import java.io.*;
public class exp9 {
public static void main(String args[])throws IOException
{
Scanner in=new Scanner(System.in);
File f=new File("sample.txt");
f.createNewFile();
FileWriter W = new FileWriter("sample.txt");
System.out.println("Enter the data");
String s=in.nextLine();
W.write(s);
W.close();
//new file
File n=new File("new-sample.txt");
n.createNewFile();
FileReader fr = new FileReader("sample.txt");
FileWriter fw=new FileWriter("new-sample.txt",true);
int c;
while ((c = fr.read()) != -1)
{
fw.write(c);
}
System.out.println("Copy finish...");
fr.close();
fw.close();
//new content
FileReader fq = new FileReader("new-sample.txt");
BufferedReader b=new BufferedReader(fq);
String str;
while((str=b.readLine()) != null)
{
System.out.print(str);
}
fq.close();
}
}