-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileHandling.java
193 lines (175 loc) · 6.69 KB
/
fileHandling.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package fileHandling;
import java.io.*;
import java.util.*;
public class fileHandling {
public static void main(String args[]) {
/* Create a File */
File f = new File("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
try {
if (f.createNewFile()) {
System.out.println("File Successfully created.");
} else {
System.out.println("File already Exists...!");
}
} catch (IOException e) {
System.out.println("An error occured : " + e.getMessage());
}
/* Write on File --> using FileWriter */
// try
// {
// FileWriter f = new
// FileWriter("D:\\Future\\JavaTutorialbyApnaCollege\\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\\newFile.txt", true);
// BufferedWriter bw = new BufferedWriter(f)){
// bw.write("I 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\\newFile.txt"))
// {
// 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());
// }
/* Read Data from a file */
// try
// {
// FileReader f = new FileReader("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// try
// {
// int i;
// while((i = f.read()) != -1)
// {
// System.out.print((char)i);
// }
// System.out.println();
// }
// finally
// {
// f.close();
// System.out.println("File closed");
// }
// }
// catch(IOException e)
// {
// System.out.println("An error occurred :" + e.getMessage());
// }
/* Read Data from a file --> using BufferedReader*/
// try (FileReader f = new FileReader("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// BufferedReader br = new BufferedReader(f))
// {
// String line = br.readLine();
// if (line != null){
// System.out.println("Data in file : "+line);
// }
// else {
// System.out.println("End of file reached.");
// }
// }
// catch (IOException e)
// {
// System.out.println("An error occurred : "+e.getMessage());
// }
/* Read Data from a file --> using FileInputStream*/
// try {
// FileInputStream f = new FileInputStream("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// try
// {
// int i;
// while((i = f.read()) != -1)
// {
// System.out.print((char)i);
// }
// System.out.println();
// }
// finally
// {
// f.close();
// System.out.println("File closed");
// }
// }
// catch (IOException e) {
// System.out.println("An error occurred : "+e.getMessage());
// }
/* Read Data from a file --> using scanner class */
// File file = new File("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// try {
// Scanner sc = new Scanner(file);
// String line = sc.nextLine();
// System.out.println(line);
// }
// catch (IOException e){
// System.out.println("An error occurred : "+e.getMessage());
// }
/* File Information */
// if(f.exists()){
// System.out.println("File Name : "+f.getName());
// System.out.println("File Location : "+f.getAbsolutePath());
// System.out.println("File Writable : "+f.canWrite());
// System.out.println("File Readable : "+f.canRead());
// System.out.println("File Size : "+f.length());
// // System.out.println("File Removes : "+f.delete());
// } else{
// System.out.println("File doesn't Exists");
// }
/* Rename a file */
// File f = new File("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// File r = new File("D:\\Future\\JavaTutorialbyApnaCollege\\rename.txt");
// if (r.exists()) {
// System.out.println(r.renameTo(f));
// } else {
// System.out.println("File doesn't exists..!");
// }
/* Copy one file data to another */
// try {
// FileInputStream r = new FileInputStream("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// FileOutputStream w = new FileOutputStream("D:\\Future\\JavaTutorialbyApnaCollege\\Copy.txt");
// int i;
// while((i = r.read()) != -1){
// w.write((char) i);
// }
// System.out.println("Data copied successfully...!");
// } catch (IOException e) {
// System.out.println("An error occurred : " + e.getMessage());
// }
/* Copy one file data to another */
// try {
// File f = new File("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// // check whether file exist or not
// if(f.exists()){
// FileInputStream r = new FileInputStream("D:\\Future\\JavaTutorialbyApnaCollege\\newFile.txt");
// FileOutputStream w = new FileOutputStream("D:\\Future\\JavaTutorialbyApnaCollege\\rename.txt");
// int i;
// while ((i = r.read()) != -1) {
// w.write((char) i);
// }
// System.out.println("Data copied Successfully :)");
// } else {
// System.out.println("File doesn't exists...!");
// }
// } catch (IOException e){
// System.out.println("An error occurred : "+e.getMessage());
// }
}
}