-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filewrite.java
44 lines (42 loc) · 1.01 KB
/
Filewrite.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
import java.util.*;
import java.lang.*;
import java.io.*;
public class Filewrite
{
public static void main(String[] args)
{
File file = new File("/Users/priya-8673/Desktop/java/samples/writefile.txt");
boolean result;
try
{
result = file.createNewFile();
if(result)
{
System.out.println("File successfully created in :" + file.getCanonicalPath());
try
{
FileOutputStream fos = new FileOutputStream("writefile.txt", true);
System.out.println("Enter the contents of the file : ");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
byte[] b = str.getBytes();
fos.write(b);
fos.close();
System.out.println("File written successfully.");
}
catch(Exception e)
{
System.out.println("Error in file writing : " + e);
}
}
else
{
System.out.println("File is already located in the location : " + file.getCanonicalPath());
}
}
catch(IOException e)
{
System.out.println("Error in file createion : " + e);
}
}
}