Skip to content

Commit 24237ae

Browse files
authored
📚 create project
1 parent 29f8f58 commit 24237ae

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Java Reader and Write Files
2+
3+
Example Application: Reader_(FileReader)_ and Write_(FileWriter)_ files.
4+
5+
## Read File
6+
7+
Imports
8+
9+
```java
10+
import java.io.BufferedReader;
11+
import java.io.File;
12+
import java.io.FileReader;
13+
import java.io.IOException;
14+
```
15+
16+
Check file exists.
17+
18+
:warning: Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.
19+
20+
```java
21+
File file = new File(FILE);
22+
23+
if (file.exists()) {
24+
//TODO
25+
} else {
26+
//TODO
27+
}
28+
```
29+
30+
Read file
31+
32+
```java
33+
String FILE = "YOUR_LOG.log";
34+
35+
FileReader reader = new FileReader(FILE);
36+
BufferedReader buffer = new BufferedReader(reader);
37+
38+
String line = null;
39+
40+
while ((line = buffer.readLine()) != null) {
41+
//TODO
42+
System.out.printf("%s\n", line);
43+
}
44+
45+
```
46+
47+
:warning:
48+
49+
```java
50+
buffer.close();
51+
```
52+
53+
54+
## Write File
55+
56+
Imports
57+
58+
```java
59+
import java.io.BufferedWriter;
60+
import java.io.File;
61+
import java.io.FileWriter;
62+
import java.io.IOException;
63+
```
64+
65+
Check file exists
66+
67+
:warning: Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.
68+
69+
```java
70+
String FILE = "YOUR_LOG.log";
71+
72+
File file = new File(FILE);
73+
74+
if (file.exists()) {
75+
//TODO
76+
} else {
77+
//TODO
78+
}
79+
```
80+
81+
Write File
82+
83+
:warning: _Need add **throws declaration** or surround with **try/catch**;_
84+
85+
```java
86+
String content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
87+
88+
String FILE = "YOUR_LOG.log";
89+
FileWriter writer = new FileWriter(FILE);
90+
BufferedWriter buffer = new BufferedWriter(writer);
91+
92+
buffer.write(content);
93+
94+
System.out.println("Success! You file is saved!");
95+
buffer.close();
96+
```
97+
98+
## Exceptions
99+
100+
* _IOException_
101+
102+
103+
## Some links for more in depth learning
104+
105+
* [JAVA](https://github.com/search?q=fefong%2Fjava)
106+
* [JAVA PRINT](https://github.com/fefong/java_print);
107+
* [JAVA SWITCH CASE](https://github.com/fefong/java_switch);
108+
* [JAVA IF/ELSE](https://github.com/fefong/java_ifElse);
109+
* [JAVA ARITHMETIC](https://github.com/fefong/java_calculator);

bin/Main.class

2.76 KB
Binary file not shown.

src/Main.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.io.BufferedReader;
2+
import java.io.BufferedWriter;
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
8+
public class Main {
9+
10+
private final static String FILE = "YOUR_LOG.log";
11+
12+
public static void main(String[] args) {
13+
createFile();
14+
readFile();
15+
}
16+
17+
private static void readFile() {
18+
19+
File file = new File(FILE);
20+
21+
if (file.exists()) {
22+
System.out.println("- File exists: File Name: " + file + " -");
23+
} else {
24+
System.out.println("- File not exists -");
25+
return;
26+
}
27+
try {
28+
FileReader reader = new FileReader(FILE);
29+
BufferedReader buffer = new BufferedReader(reader);
30+
31+
String line = null;
32+
while ((line = buffer.readLine()) != null) {
33+
System.out.printf("%s\n", line);
34+
}
35+
36+
System.out.println("- End file -");
37+
buffer.close();
38+
39+
} catch (IOException e) {
40+
System.err.format("Error! Exception %s%n", e);
41+
42+
}
43+
44+
}
45+
46+
47+
private static void createFile() {
48+
File file = new File(FILE);
49+
50+
if (file.exists()) {
51+
System.out.println("- Update File -");
52+
} else {
53+
System.out.println("- Create File-");
54+
}
55+
56+
String content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\nMaecenas quis rutrum enim.\nAliquam fringilla tincidunt diam in condimentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vehicula commodo ornare. Praesent a tortor ac elit vulputate interdum et eget lectus. Fusce enim nunc, elementum a turpis et, finibus dapibus sem. Nulla iaculis elementum ipsum ac dictum. Curabitur at ullamcorper risus. Aenean commodo leo leo, vitae fringilla velit iaculis eu. Praesent at gravida tortor.\r\n";
57+
try {
58+
FileWriter writer = new FileWriter(FILE);
59+
BufferedWriter buffer = new BufferedWriter(writer);
60+
61+
buffer.write(content);
62+
63+
System.out.println("Success! You file is saved!");
64+
buffer.close();
65+
} catch (IOException e) {
66+
System.err.format("Error! Exception %s%n", e);
67+
68+
}
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)