Skip to content

Commit 9cdbec7

Browse files
Add files via upload
1 parent b1629e3 commit 9cdbec7

40 files changed

+521
-0
lines changed
1017 Bytes
Binary file not shown.
1.47 KB
Binary file not shown.
1.13 KB
Binary file not shown.
1.68 KB
Binary file not shown.
1.26 KB
Binary file not shown.
1.46 KB
Binary file not shown.
1.13 KB
Binary file not shown.
736 Bytes
Binary file not shown.
Binary file not shown.
1.3 KB
Binary file not shown.
358 Bytes
Binary file not shown.
3.29 KB
Binary file not shown.
3.73 KB
Binary file not shown.
1.57 KB
Binary file not shown.
1.57 KB
Binary file not shown.
2.23 KB
Binary file not shown.

Class15/bin/module-info.class

191 Bytes
Binary file not shown.

Class15/in/Company.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<company>
3+
<employee>
4+
<id>32</id>
5+
<name>David</name>
6+
<age>33</age>
7+
</employee>
8+
<employee>
9+
<id>23</id>
10+
<name>Paul</name>
11+
<age>23</age>
12+
<employee>
13+
<id>12</id>
14+
<name>Josh</name>
15+
<age>13</age>
16+
</employee>
17+
<employee>
18+
<id>41</id>
19+
<name>Tom</name>
20+
<age>33</age>
21+
</employee>
22+
<employee>
23+
<id>37</id>
24+
<name>Adam</name>
25+
<age>25</age>
26+
</employee>
27+
</employee>
28+
</company>

Class15/in/Customers.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<customers>
3+
<customer>
4+
<id>12</id>
5+
<name>David</name>
6+
<address>Address 1</address>
7+
</customer>
8+
<customer>
9+
<id>52</id>
10+
<name>Paul</name>
11+
<address>Address 2</address>
12+
</customer>
13+
<customer>
14+
<id>23</id>
15+
<name>Adam</name>
16+
<address>Address 3</address>
17+
</customer>
18+
<customer>
19+
<id>2</id>
20+
<name>Ben</name>
21+
<address>Address 4</address>
22+
</customer>
23+
</customers>

Class15/in/Settings.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#set name
2+
#Tue Dec 14 13:34:03 IST 2021
3+
name=paul
4+
course=java
5+
version=17

Class15/out/Example.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello
2+
World

Class15/out/Sample.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello World
2+
i am David

Class15/out/SampleCopy.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello World
2+
i am David
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.david.class151;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
7+
public class Example1 {
8+
public static void main(String[] args) {
9+
try {
10+
FileWriter fw = new FileWriter("./out/Sample.txt");
11+
BufferedWriter bw =new BufferedWriter(fw);
12+
bw.write("Hello World \n i am David");
13+
bw.close();
14+
System.out.println("File created");
15+
16+
} catch (IOException e) {
17+
e.printStackTrace();
18+
}
19+
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.david.class151;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
8+
public class Example2 {
9+
public static void main(String[] args) {
10+
try {
11+
FileReader fr = new FileReader("./out/Sample.txt");
12+
BufferedReader br = new BufferedReader(fr);
13+
String op ="";
14+
while(true) {
15+
String s = br.readLine();
16+
if(s == null) {
17+
break;
18+
} else {
19+
op += s + "\n";
20+
}
21+
}
22+
br.close();
23+
System.out.println("File content : \n"+op);
24+
} catch (FileNotFoundException e) {
25+
e.printStackTrace();
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.david.class151;
2+
3+
import java.io.File;
4+
5+
public class Example3 {
6+
public static void main(String[] args) {
7+
File f = new File("./out/Sample.txt");
8+
System.out.println("name : "+f.getName());
9+
System.out.println("path : "+f.getAbsolutePath());
10+
System.out.println("Exists : "+f.exists());
11+
System.out.println("can read : "+f.canRead());
12+
System.out.println("can write : "+f.canWrite());
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.david.class151;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.util.Properties;
9+
10+
public class Example4 {
11+
public static void main(String[] args) {
12+
Properties p = new Properties();
13+
try {
14+
p.load(new FileReader("./in/settings.properties"));
15+
System.out.println(p);
16+
System.out.println("check key : "+p.containsKey("name"));
17+
System.out.println("version = "+p.get("version"));
18+
p.setProperty("name", "paul");
19+
p.store(new FileWriter("./in/settings.properties"), "set name");
20+
System.out.println("file stored");
21+
} catch (FileNotFoundException e) {
22+
e.printStackTrace();
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.david.class152;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.nio.file.StandardCopyOption;
8+
9+
public class Example1 {
10+
public static void main(String[] args) {
11+
Path p1 = Paths.get("out", "Sample.txt");
12+
Path p2 = Paths.get("out", "SampleCopy.txt");
13+
try {
14+
Files.copy(p1, p2, StandardCopyOption.REPLACE_EXISTING);
15+
System.out.println("Files copied");
16+
} catch (IOException e) {
17+
e.printStackTrace();
18+
}
19+
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.david.class152;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.nio.file.StandardCopyOption;
8+
import java.nio.file.attribute.FileAttribute;
9+
10+
public class Example2 {
11+
public static void main(String[] args) {
12+
Path p1 = Paths.get("out", "Example.txt");
13+
System.out.println("file name : "+p1.getFileName());
14+
System.out.println("file system"+p1.getFileSystem());
15+
try {
16+
Files.createFile(p1);
17+
System.out.println("File created");
18+
19+
} catch (IOException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.david.class152;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
public class Example3 {
9+
public static void main(String[] args) {
10+
Path p1 = Paths.get("out", "Example.txt");
11+
try {
12+
Files.writeString(p1,"""
13+
Hello
14+
World""");
15+
System.out.println("File written");
16+
} catch (IOException e) {
17+
e.printStackTrace();
18+
}
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.david.class153;
2+
3+
public class Employee {
4+
int id;
5+
String name;
6+
int age;
7+
@Override
8+
public String toString() {
9+
return "id = "+this.id+", name ="+this.name+", age="+this.age;
10+
11+
}
12+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.david.class153;
2+
3+
import java.util.ArrayList;
4+
5+
import org.xml.sax.Attributes;
6+
import org.xml.sax.SAXException;
7+
import org.xml.sax.helpers.DefaultHandler;
8+
9+
public class EmployeeHandler extends DefaultHandler{
10+
ArrayList<Employee> eList;
11+
Employee e;
12+
boolean employee,id,name,age;
13+
@Override
14+
public void startDocument() throws SAXException {
15+
eList = new ArrayList<>();
16+
System.out.println("Document Started");
17+
}
18+
19+
@Override
20+
public void endDocument() throws SAXException {
21+
System.out.println(eList.size());
22+
System.out.println(eList);
23+
System.out.println("Document ended");
24+
}
25+
26+
@Override
27+
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
28+
System.out.println(qName);
29+
switch(qName) {
30+
case "employee": e = new Employee();
31+
employee = true;
32+
break;
33+
case "id" : id = true;
34+
break;
35+
case "name" : name = true;
36+
break;
37+
case "age" : age = true;
38+
break;
39+
}
40+
}
41+
@Override
42+
public void characters(char[] ch, int start, int length) throws SAXException {
43+
44+
System.out.println("character event");
45+
String val = new String(ch, start, length);
46+
if(id){
47+
e.id = Integer.parseInt(val);
48+
id =false;
49+
} else if(name) {
50+
e.name= val;
51+
name = false;
52+
} else if(age) {
53+
e.age = Integer.parseInt(val);
54+
age =false;
55+
}
56+
}
57+
@Override
58+
public void endElement(String uri, String localName, String qName) throws SAXException {
59+
switch(qName) {
60+
case "employee": eList.add(e);
61+
employee =false;
62+
break;
63+
}
64+
System.out.println(qName+"element ended");
65+
}
66+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.david.class153;
2+
3+
import java.io.IOException;
4+
5+
import javax.xml.parsers.ParserConfigurationException;
6+
import javax.xml.parsers.SAXParser;
7+
import javax.xml.parsers.SAXParserFactory;
8+
9+
import org.xml.sax.SAXException;
10+
11+
public class Example1 {
12+
public static void main(String[] args) {
13+
EmployeeHandler handler = new EmployeeHandler();
14+
try {
15+
SAXParserFactory factory = SAXParserFactory.newInstance();
16+
SAXParser saxParser = factory.newSAXParser();
17+
saxParser.parse("./in/Company.xml", handler);
18+
} catch (SAXException | IOException e) {
19+
e.printStackTrace();
20+
} catch (ParserConfigurationException e) {
21+
e.printStackTrace();
22+
}
23+
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.david.class154;
2+
3+
public class Customer {
4+
int id;
5+
String name;
6+
String address;
7+
8+
}

0 commit comments

Comments
 (0)