Skip to content

Commit c13b4f0

Browse files
committed
Serialisation added.
Signed-off-by: Someshdiwan <[email protected]>
1 parent 2f47959 commit c13b4f0

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
InputStreamReader is commonly used to specify character encoding when reading text files.
2+
3+
It acts as a bridge between byte streams and character streams, allowing you to read characters from a byte stream
4+
(like FileInputStream) using a specified character encoding.
5+
6+
Key points about character encoding in Java I/O:
7+
1. FileReader uses the default character encoding of the system.
8+
2. InputStreamReader allows specifying a custom character encoding.
9+
3. It's important to use the correct encoding to avoid data corruption.
10+
4. Common encodings include UTF-8, UTF-16, ISO-8859-1, etc.
11+
12+
Example usage:
13+
```java
14+
try (FileInputStream fis = new FileInputStream("file.txt");
15+
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
16+
BufferedReader br = new BufferedReader(isr)) {
17+
18+
String line;
19+
while ((line = br.readLine()) != null) {
20+
System.out.println(line);
21+
}
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
```
26+
27+
In this example, InputStreamReader is used to read the file with UTF-8 encoding.
28+
This approach is more flexible than using FileReader, as it allows you to specify the encoding explicitly,
29+
ensuring that the file is read correctly regardless of the system's default encoding.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Key points about serialization:
2+
3+
1. Serialization is the process of converting an object into a byte stream.
4+
2. Deserialization is the reverse process of creating an object from a byte stream.
5+
3. The Serializable interface is in the java.io package.
6+
4. Fields marked as transient are not serialized.
7+
5. Static fields are not serialized as they belong to the class, not the object.
8+
9+
The method canRead() of the File class is used to check if a file is readable.
10+
11+
This method returns a boolean value indicating whether the application can read the file denoted by the File object.
12+
13+
Key points about file permissions in Java:
14+
1. canRead(): Checks if the file is readable.
15+
2. canWrite(): Checks if the file is writable.
16+
3. canExecute(): Checks if the file is executable.
17+
4. setReadable(), setWritable(), setExecutable(): Methods to change file permissions.
18+
19+
Example usage:
20+
```java
21+
File file = new File("example.txt");
22+
23+
if (file.canRead()) {
24+
System.out.println("File is readable");
25+
} else {
26+
System.out.println("File is not readable");
27+
}
28+
29+
if (file.canWrite()) {
30+
System.out.println("File is writable");
31+
} else {
32+
System.out.println("File is not writable");
33+
}
34+
35+
// Change permissions
36+
file.setWritable(true);
37+
file.setReadable(true, false); // true for all users, false for owner only
38+
```
39+
40+
It's important to note that these methods work within the constraints of the underlying operating system
41+
and the Java Security Manager.
42+
43+
The actual ability to read, write, or execute a file may depend on factors beyond just the file permissions,
44+
such as overall system security policies.

0 commit comments

Comments
 (0)