-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile.java
More file actions
73 lines (58 loc) · 1.86 KB
/
Copy pathfile.java
File metadata and controls
73 lines (58 loc) · 1.86 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
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class file {
public void createFile(){
try {
File myObjNew = new File("createMe.txt");
if(myObjNew.createNewFile()){
System.out.println("File Successfully created"+myObjNew.getAbsolutePath());
}else{
System.out.println("File already exists"+myObjNew.getAbsolutePath());
}
} catch(IOException e){
System.out.println("An error occurred");
e.printStackTrace();
}
}
public void writeFile(){
try{
File myObjNew = new File("C:\\Users\\pperiyas\\Desktop\\createMe.txt");
FileWriter myWriter = new FileWriter("C:\\Users\\pperiyas\\Desktop\\createMe.txt");
myWriter.write("This file is written using Java FileWriter");
myWriter.close();
System.out.println("Successfully wrote to the file");
}catch(IOException e){
System.out.println("An error Occurred");
e.printStackTrace();
}
}
public void readFile(){
try {
File myObjNote = new File("C:\\Users\\pperiyas\\Desktop\\notes.txt");
Scanner myReader = new Scanner(myObjNote);
while(myReader.hasNextLine()){
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
}catch(FileNotFoundException e){
System.out.println("An error occurred");
e.printStackTrace();
}
}
public static void main(String[] args) {
File myObj = new File("C:\\Users\\pperiyas\\Desktop\\notes.txt");
System.out.println(myObj.exists());
System.out.println(myObj.getName());
System.out.println(myObj.length());
System.out.println(myObj.getAbsolutePath());
//System.out.println(myObj.list());
file myFile = new file();
//myFile.createFile();
//myFile.writeFile();
myFile.readFile();
}
}