Skip to content

Commit 95b3f47

Browse files
committed
File Handling programs
1 parent 75e7ad3 commit 95b3f47

15 files changed

+301
-0
lines changed

StringCombining.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class StringCombining {
2+
public static void main(String[] args) {
3+
String str1 = new String("Wiley");
4+
StringBuffer str2 = new StringBuffer("Google");
5+
6+
System.out.println(str1.hashCode());
7+
str1 = str1.concat(" Pvt. Ltd.");
8+
System.out.println(str1.hashCode());
9+
10+
str2.append(" Ltd.");
11+
12+
System.out.println(str1);
13+
System.out.println(str2);
14+
}
15+
}

StringInternNitish.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class StringInternNitish {
2+
public static void main(String args[]) {
3+
4+
String str0 = new String("hlo");
5+
String str1 = "hlo";
6+
String str2 = "hlo";
7+
String str4 = str0.intern();
8+
System.out.println(str0 == str4); // false
9+
System.out.println(Integer.toHexString(str4.hashCode()));
10+
System.out.println(Integer.toHexString(str0.hashCode()));
11+
}
12+
}

StringTokenizerExample.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import java.util.StringTokenizer;
2+
3+
public class StringTokenizerExample {
4+
public static void main(String[] args) {
5+
StringTokenizer tokenizer = new StringTokenizer("Akash,Kumar Das", " ");
6+
while (tokenizer.hasMoreTokens()) {
7+
System.out.println(tokenizer.nextToken());
8+
}
9+
}
10+
}

fileHandling/CreateFile.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package fileHandling;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public class CreateFile {
7+
public static void main(String[] args) throws IOException {
8+
File f = new File("fileHandling/Test2.txt");
9+
if (f.createNewFile()) {
10+
System.out.println("File created successfully");
11+
} else {
12+
System.out.println("File already exists");
13+
}
14+
}
15+
}

fileHandling/DeleteFile.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fileHandling;
2+
3+
import java.nio.file.DirectoryNotEmptyException;
4+
import java.nio.file.Files;
5+
import java.nio.file.NoSuchFileException;
6+
import java.nio.file.Paths;
7+
import java.io.File;
8+
import java.io.FileNotFoundException;
9+
import java.io.IOException;
10+
11+
public class DeleteFile {
12+
public static void main(String[] args) {
13+
File f = new File("Test.txt");
14+
if (f.delete()) {
15+
System.out.println(f.getName() + " deleted.");
16+
} else {
17+
System.out.println("Unexpected error encountered while trying to delete " + f.getName());
18+
}
19+
20+
try {
21+
Files.deleteIfExists(Paths.get("Empty"));
22+
System.out.println("Deleted successfully");
23+
} catch (FileNotFoundException e) {
24+
System.out.println("File was not found");
25+
} catch (NoSuchFileException e) {
26+
System.out.println("Sorry, file not found!");
27+
} catch (DirectoryNotEmptyException e) {
28+
System.out.println("Directory not empty");
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
}

fileHandling/GetFileInfo.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fileHandling;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public class GetFileInfo {
7+
public static void main(String[] args) {
8+
File f = new File("fileHandling/Test.txt");
9+
10+
if (f.exists()) {
11+
System.out.println(f.getName());
12+
13+
System.out.println(f.getAbsolutePath());
14+
15+
System.out.println(f.canRead());
16+
17+
System.out.println(f.canWrite());
18+
19+
System.out.println(f.canExecute());
20+
21+
System.out.println(f.length());
22+
}
23+
}
24+
}

fileHandling/ReadFromFile.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package fileHandling;
2+
3+
import java.util.Scanner;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.io.BufferedReader;
7+
import java.io.File;
8+
import java.io.FileReader;
9+
import java.io.FileNotFoundException;
10+
import java.io.IOException;
11+
12+
public class ReadFromFile {
13+
public static void main(String[] args) {
14+
// the general way
15+
File f = new File("fileHandling/Test.txt");
16+
17+
try (Scanner fileReader = new Scanner(f);) { // try-with-resources
18+
while (fileReader.hasNextLine()) {
19+
System.out.println(fileReader.nextLine());
20+
}
21+
} catch (FileNotFoundException e) {
22+
e.printStackTrace();
23+
}
24+
25+
// nio way of reading from file
26+
try (BufferedReader reader = Files.newBufferedReader(Paths.get("fileHandling/Test2.txt"))) {
27+
String currentLine = null;
28+
while ((currentLine = reader.readLine()) != null) {// while there is content on the current line
29+
System.out.println(currentLine); // print the current line
30+
}
31+
} catch (IOException ex) {
32+
ex.printStackTrace(); // handle an exception here
33+
}
34+
35+
// using FileReader (reads character by character)
36+
try (FileReader fileReader = new FileReader("fileHandling/Test.txt");) {
37+
int ch;
38+
int counter = 0;
39+
while ((ch = fileReader.read()) != -1) {
40+
System.out.print((char) ch);
41+
counter++;
42+
}
43+
System.out.println(counter);
44+
} catch (IOException ex) {
45+
ex.printStackTrace(); // handle an exception here
46+
}
47+
}
48+
}

fileHandling/Test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is a test file for file handling.
2+
Second line.

fileHandling/Test2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File Handling test file. Dummy text. Appended via FileWriter. Appended via nio Files.

fileHandling/Test3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Appended via FileWriter

fileHandling/Test4.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Appended via nio.Files

fileHandling/WriteFile.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package fileHandling;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
import java.nio.file.StandardOpenOption;
8+
9+
public class WriteFile {
10+
public static void main(String[] args) throws IOException {
11+
// Create or Overwrite a file
12+
FileWriter writer = new FileWriter("fileHandling/Test2.txt");
13+
writer.write("This is my 2nd file handling test file.");
14+
writer.write("Dummy text.");
15+
writer.close();
16+
System.out.println("File written successfully");
17+
18+
// Append to a file
19+
FileWriter writer2 = new FileWriter("fileHandling/Test2.txt", true);
20+
writer2.write("Appended via FileWriter");
21+
writer2.close();
22+
System.out.println("File written successfully");
23+
24+
// Java 7 onwards
25+
Files.write(Paths.get("fileHandling/Test2.txt"), "Appended via nio.Files".getBytes(), StandardOpenOption.CREATE,
26+
StandardOpenOption.APPEND);
27+
}
28+
}

fileHandling/delete

Whitespace-only changes.

myPackage/Test.txt

Whitespace-only changes.

quiz/Quiz1.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package quiz;
2+
3+
public class Quiz1 {
4+
public static void main(String[] args) {
5+
System.out.println(Integer.parseInt("ab10"));
6+
}
7+
}
8+
9+
// 1. public class LineUp {
10+
// 2. public static void main(String[] args) {
11+
// 3. double d = 12.345;
12+
// 4. // insert code here
13+
// 5. }
14+
// 6. }
15+
16+
// Expected Output: | 12.345|
17+
18+
// Options
19+
// A. System.out.printf("|%7d| \n", d);
20+
// B. System.out.printf("|%7f| \n", d);
21+
// C. System.out.printf("|%3.7d| \n", d);
22+
// D.System.out.printf("|%3.7f| \n",d);
23+
// E. System.out.printf("|%7.3d| \n", d);
24+
// F. System.out.printf("|%7.3f| \n", d);
25+
26+
// public class Test {
27+
// public static void main(String [] args) {
28+
// int x = 5;
29+
// boolean b1 = true;
30+
// boolean b2 = false;
31+
32+
// if ((x == 4) && !b2 )
33+
// System.out.print("1 ");
34+
35+
// System.out.print("2 ");
36+
37+
// if ((b2 = true) && b1 )
38+
// System.out.print("3 ");
39+
// }
40+
// }
41+
42+
// A. 2
43+
// B. 3
44+
// C. 1 2
45+
// D. 2 3
46+
// E. 1 2 3
47+
// F. Compilation fails.
48+
// G. An exception is thrown at runtime.
49+
50+
// interface Foo {}
51+
// class Alpha implements Foo {}
52+
// class Beta extends Alpha {}
53+
54+
// class Delta extends Beta {
55+
// public static void main(String[] args) {
56+
// Beta x = new Beta();
57+
// //insert code here 16
58+
// }
59+
// }
60+
61+
// Which code, inserted at line 16, will cause a java.lang.ClassCastException?
62+
63+
// A.Alpha a = x;
64+
// B.Foo f = (Delta) x;
65+
// C.Foo f = (Alpha) x;
66+
// D.Beta b = (Beta)(Alpha)x;
67+
68+
// public void go() {
69+
// String o = "";
70+
// z: for (int x = 0; x < 3; x++) {
71+
// for (int y = 0; y < 2; y++) {
72+
// if (x == 1)
73+
// break;
74+
// if (x == 2 && y == 1)
75+
// break z;
76+
// o = o + x + y;
77+
// }
78+
// }
79+
// System.out.println(o);
80+
// }
81+
82+
// What is
83+
// the result
84+
// when the
85+
86+
// go() method is invoked?
87+
88+
// A. 00
89+
// B. 0001
90+
// C. 000120
91+
// D. 00012021
92+
// E. Compilation fails.
93+
// F. An exception is thrown at runtime.
94+
95+
// try {
96+
// //some code here line 108
97+
// } catch (NullPointerException e1) {
98+
// System.out.print("a");
99+
// } catch (Exception e2) {
100+
// System.out.print("b");
101+
// }finally {
102+
// System.out.print("c");
103+
// }
104+
105+
// If some sort of exception is thrown at line 108, which output is possible?
106+
107+
// A. a
108+
// B. b
109+
// C. c
110+
// D. ac
111+
// E. abc

0 commit comments

Comments
 (0)