๋ฐ์ํ
package view;
import java.io.*;
public class FileWrite {
public static void main(String[] args) {
try (
/* ์ด๊ณณ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด try ์ข
๋ฃ ํ ์๋์ผ๋ก close ์ฒ๋ฆฌ๊ฐ ๋ฉ๋๋ค.
* true : ๊ธฐ์กด ํ์ผ์ ์ด์ด์ ์์ฑ (default๋ false ์
๋๋ค.) - ๋ฎ์ด์๊ธฐ๊ฐ ๋จ
*/
FileWriter f_writer = new FileWriter("test.txt");
BufferedWriter b_writer = new BufferedWriter( f_writer );
)
{
b_writer.newLine(); // ๋ฒํผ์ ๊ฐํ ์ฝ์
Enter
b_writer.write(" โโโโ* I Love U โโโโ"); //๋ฒํฐ์ ์
๋ ฅ
b_writer.newLine(); // ๋ฒํผ์ ๊ฐํ ์ฝ์
b_writer.write(" โ โ");
b_writer.newLine();
b_writer.write(" โ Lovely yuna * โ");
b_writer.newLine();
b_writer.write(" โ โ");
b_writer.newLine();
b_writer.write(" โโโโโโโโโโโโโโโโโโโ");
b_writer.newLine();
b_writer.newLine();
b_writer.flush(); //๋ฒํผ์ ๋ด์ฉ์ ํ์ผ์ ์ฐ๊ธฐ
} catch ( IOException e) {
System.out.println(e);
}
File file = new File("test.txt");
// ํ์ผ ์ /๋ฌด ํ๋จ
if (file.isFile()) {
System.out.println("test.txt ํ์ผ์ด ์์ต๋๋ค.");
}
}
}
package view;
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class FileRead {
public static void main(String[] args) throws IOException {
System.out.println("1. FileReader");
// 1. FileReader
FileReader reader = new FileReader("test.txt");
int check;
while((check = reader.read()) != -1) {
System.out.print((char)check);
}
System.out.println("2. BufferedReader");
// 2. BufferedReader
BufferedReader bufferedReader = new BufferedReader(
new FileReader("test.txt"),
16*1024
);
String str;
while((str = bufferedReader.readLine()) != null){
System.out.println(str);
}
bufferedReader.close();
System.out.println("3. Scanner");
// 3. Scanner
Scanner scan = new Scanner(new File("test.txt"));
while(scan.hasNext()) {
str = scan.next();
System.out.println(str);
}
System.out.println("4. Files");
// 4. Files
List<String> lines = Files.readAllLines(Paths.get("test.txt"));
System.out.println(lines);
}
}
List ๋ก ๊ฐ์ ๋ถ๋ฌ์ค๋ Files.read ๋ ๋ฐ์ดํฐ๋ฅผ ์๋ผ์ ๋ถ๋ฌ์ฌ๋ ์ ์ฉํ๊ฒ ์ ์ฉํ ์์๊ฒ ๋ค. ์ฌ๋ฐ๋น๐
300x250
'๊ฐ๋ฐ์ธ์ด > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Map - getOrDefault ์ด๋? (0) | 2022.04.06 |
---|---|
[Java] ์ฒ ๋จ์ ์ฝค๋ง ์ฐ๊ธฐ (Format ํด๋์ค , ์ฝค๋ง ์ ๊ท์) (0) | 2022.03.15 |
[JAVA] ์บก์ํ - Encapsulation (0) | 2022.02.23 |
[JAVA] repeat ๋ฉ์๋ (0) | 2022.02.22 |
[JAVA] ํจํค์ง ๋ช ๋ช ๊ท์น (0) | 2022.02.22 |