Skip to content

Commit ff963db

Browse files
committed
File IO Streams, Reader, Object Serialization
1 parent 759b536 commit ff963db

File tree

11 files changed

+216
-0
lines changed

11 files changed

+216
-0
lines changed

FileIO/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

FileIO/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>FileIO</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.cdac.fileio.bytestream;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
8+
public class ByteStream {
9+
10+
public static void main(String[] args) throws IOException {
11+
String content = "Welcome to DAC Course!";
12+
writeToFile(content);
13+
readFromFile();
14+
}
15+
16+
public static void writeToFile(String content){
17+
String filePath = "D:\\byteStream.txt";
18+
FileOutputStream fos = null;
19+
try {
20+
fos = new FileOutputStream(filePath);
21+
fos.write(content.getBytes());
22+
} catch (FileNotFoundException e) {
23+
e.printStackTrace();
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
}
27+
finally{
28+
try {
29+
fos.close();
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
}
35+
36+
public static void readFromFile(){
37+
String filePath = "D:\\byteStream.txt";
38+
FileInputStream fis = null;
39+
try {
40+
fis = new FileInputStream(filePath);
41+
byte[] buffer = new byte[fis.available()];
42+
fis.read(buffer);
43+
String fileContent = new String(buffer);
44+
System.out.println("File Content is : "+fileContent);
45+
} catch (FileNotFoundException e) {
46+
e.printStackTrace();
47+
} catch (IOException e) {
48+
e.printStackTrace();
49+
} finally {
50+
try {
51+
fis.close();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
}
57+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.cdac.fileio.bytestream;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
8+
public class CharacterStream {
9+
10+
public static void main(String[] args) throws IOException {
11+
String content = "Hi Good morning!";
12+
writeToFile(content);
13+
readFromFile();
14+
}
15+
16+
public static void writeToFile(String content){
17+
String filePath = "D:\\char-based-stream.txt";
18+
FileWriter fileWriter = null;
19+
try {
20+
fileWriter = new FileWriter(filePath);
21+
fileWriter.write(content);
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}finally{
25+
try {
26+
fileWriter.close();
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
}
32+
33+
public static void readFromFile(){
34+
String filePath = "D:\\char-based-stream.txt";
35+
FileReader fileReader = null;
36+
try {
37+
fileReader = new FileReader(filePath);
38+
int i;
39+
while ( (i = fileReader.read()) != -1){
40+
char ch = (char) i;
41+
System.out.print(ch);
42+
}
43+
} catch (FileNotFoundException e) {
44+
e.printStackTrace();
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}finally{
48+
try {
49+
fileReader.close();
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.cdac.fileio.bytestream;
2+
3+
import java.io.Serializable;
4+
5+
public class Employee implements Serializable{
6+
7+
private int empId;
8+
private String name;
9+
transient private int salary;
10+
public int getEmpId() {
11+
return empId;
12+
}
13+
public void setEmpId(int empId) {
14+
this.empId = empId;
15+
}
16+
public String getName() {
17+
return name;
18+
}
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
public int getSalary() {
23+
return salary;
24+
}
25+
public void setSalary(int salary) {
26+
this.salary = salary;
27+
}
28+
@Override
29+
public String toString() {
30+
return empId+" "+name+" "+salary;
31+
}
32+
}

0 commit comments

Comments
 (0)