Skip to content

Commit 8f8d525

Browse files
Exception handling
even i added with class files of create a file using java and read file using java
1 parent cebf731 commit 8f8d525

File tree

9 files changed

+116
-0
lines changed

9 files changed

+116
-0
lines changed

tryCatch/bin/module-info.class

147 Bytes
Binary file not shown.
1.29 KB
Binary file not shown.

tryCatch/bin/tryCatch/Finally.class

962 Bytes
Binary file not shown.
1.32 KB
Binary file not shown.

tryCatch/myfile.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sushma
2+
100
3+
you caan read this
4+
Only you can read this girl
5+
hey how are you
6+
this will be last line
7+
-----------------------------------

tryCatch/src/module-info.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*
3+
*/
4+
/**
5+
*
6+
*/
7+
module tryCatch {
8+
}

tryCatch/src/tryCatch/CreateFile.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tryCatch;
2+
import java.io.File;
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
6+
public class CreateFile {
7+
8+
public static void main(String[] args) throws IOException {
9+
// TODO Auto-generated method stub
10+
11+
try {
12+
File f = new File("myfile.txt");
13+
System.out.println("This is the text Flie");
14+
15+
16+
if(!f.exists()) {
17+
f.createNewFile();
18+
}
19+
20+
PrintWriter pw = new PrintWriter(f);
21+
pw.println("sushma");
22+
pw.println(100);
23+
pw.println("you caan read this");
24+
pw.println("Only you can read this girl");
25+
pw.println("hey how are you");
26+
pw.println("this will be last line");
27+
pw.println("-----------------------------------");
28+
pw.close();
29+
}
30+
catch(IOException e) {
31+
e.printStackTrace();
32+
33+
}
34+
System.out.println("done");
35+
}
36+
37+
38+
}

tryCatch/src/tryCatch/Finally.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tryCatch;
2+
3+
public class Finally {
4+
5+
public static int funInt() {
6+
int a = 1000;
7+
try {
8+
a = a/00;
9+
return a;
10+
11+
} catch (ArithmeticException e) {
12+
System.out.println(e);
13+
System.out.println("this is catch method");
14+
return a;
15+
16+
} finally {
17+
a = 5000;
18+
System.out.println("This is finally");
19+
return a;
20+
}
21+
}
22+
23+
24+
public static void main (String[] args) {
25+
System.out.println(funInt());
26+
}
27+
}

tryCatch/src/tryCatch/fliereader.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tryCatch;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
public class fliereader {
8+
9+
public static void main(String[] args) {
10+
// TODO Auto-generated method stub
11+
BufferedReader br = null;
12+
13+
try {
14+
br = new BufferedReader(new FileReader("myfile.txt"));
15+
String line;
16+
17+
while ((line = br.readLine()) != null) {
18+
System.out.println(line);
19+
}
20+
21+
}catch (Exception e) {
22+
e.printStackTrace();
23+
24+
} finally {
25+
try {
26+
br.close();
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
31+
System.out.println("The End");
32+
}
33+
34+
}
35+
36+
}

0 commit comments

Comments
 (0)