|
| 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); |
0 commit comments