-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileWriter.java
47 lines (42 loc) · 1.42 KB
/
fileWriter.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
package fileHandling;
import java.io.*;
public class fileWriter {
public static void main (String args[]){
/* Write on File --> using FileWriter */
try
{
FileWriter f = new FileWriter("D:\\Future\\JavaTutorialbyApnaCollege\\fileHandling\\newfile.txt");
try
{
f.write("Java is the best programming language.");
}
finally
{
f.close();
}
System.out.println("Successfully data wrote in file.");
}
catch(IOException i)
{
System.out.println(i);
}
/* Write on File --> using BufferedWriter */
try(FileWriter f= new FileWriter("D:\\Future\\JavaTutorialbyApnaCollege\\fileHandling\\newfile.txt", true);
BufferedWriter bw = new BufferedWriter(f)){
bw.write("\nI love java");
System.out.println("Written successfully.");
}
catch(IOException e){
System.out.println(e.getMessage());
}
/* Write on File --> using FileOutputStream */
try (FileOutputStream stream = new FileOutputStream("D:\\Future\\JavaTutorialbyApnaCollege\\fileHandling\\newfile.txt", true))
{
String data = String.valueOf(12);
stream.write(data.getBytes());
System.out.println("Written successfully");
} catch(IOException e){
System.out.println("An error occurred : "+e.getMessage());
}
}
}