๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] ๊ทธ ์™ธ ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 23.
๋ฐ˜์‘ํ˜•

โ–ถ ๊ทธ ์™ธ ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค

โ€‹

*File ํด๋ž˜์Šค

File ํด๋ž˜์Šค๋Š” ๋ง๊ทธ๋Œ€๋กœ ํŒŒ์ผ์ด๋ผ๋Š” ๊ฐœ๋…์„ ์ถ”์ƒํ™”ํ•œ ํด๋ž˜์Šค์ด๋‹ค. File ํด๋ž˜์Šค์— ๋ณ„๋„์˜ ์ž…์ถœ๋ ฅ ๊ธฐ๋Šฅ์€ ์—†์ง€๋งŒ ํŒŒ์ผ ์ž์ฒด์˜ ๊ฒฝ๋กœ๋‚˜ ์ •๋ณด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ณ  ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

์ƒ์„ฑ์ž

์„ค๋ช…

File(String pathname)

pathname์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

โ€‹

โ€‹

FileTest.java

package stream.others;

import java.io.File;
import java.io.IOException;

public class FileTest {
	public static void main(String[] args) throws IOException {
		File file = new File("c:\\java\\newFile.txt"); // ํ•ด๋‹น ๊ฒฝ๋กœ์— File ํด๋ž˜์Šค ์ƒ์„ฑ. ์•„์ง ์‹ค์ œ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์€ ์•„๋‹˜
		file.createNewFile(); // ์‹ค์ œ ํŒŒ์ผ ์ƒ์„ฑ

		System.out.println(file.isFile());
		System.out.println(file.isDirectory());
		System.out.println(file.getName());
		System.out.println(file.getAbsolutePath());
		System.out.println(file.getPath());
		System.out.println(file.canRead());
		System.out.println(file.canWrite()); // ํŒŒ์ผ์˜ ์†์„ฑ์„ ์‚ดํŽด๋ณด๋Š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•˜์—ฌ ์ถœ๋ ฅ

		file.delete(); // ํŒŒ์ผ ์‚ญ์ œ
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ€‹

*RandomAccessFile ํด๋ž˜์Šค

RandomAccessFile์€ ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค ์ค‘ ์œ ์ผํ•˜๊ฒŒ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์„ ๋™์‹œ์— ํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค. ๋˜ํ•œ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ์ŠคํŠธ๋ฆผ์€ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ž๋ฃŒ๋ฅผ ์ฝ์—ˆ์ง€๋งŒ RandomAccessFile์€ ์ž„์˜์˜ ์œ„์น˜๋กœ ์ด๋™ํ•˜์—ฌ ์ž๋ฃŒ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. RandomAccessFile์—๋Š” ํŒŒ์ผ ํฌ์ธํ„ฐ๊ฐ€ ์žˆ๋Š”๋ฐ, ํ˜„์žฌ ์ด ํŒŒ์ผ์˜ ์–ด๋Š ์œ„์น˜์—์„œ ์ฝ๊ณ  ์“ฐ๋Š”์ง€ ๊ทธ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์†์„ฑ์ด๋‹ค. ์ŠคํŠธ๋ฆผ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ํŒŒ์ผ์— ์ž๋ฃŒ๋ฅผ ์“ฐ๊ฑฐ๋‚˜ ์ฝ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค. ํŒŒ์ผ ํฌ์ธํ„ฐ๊ฐ€ ์ด๋™ํ•˜๋Š” ์œ„์น˜๊ฐ€ ํŒŒ์ผ ์ž๋ฃŒ๋ฅผ ์ฝ๊ฑฐ๋‚˜ ์“ฐ์ด๋Š” ์œ„์น˜์ด๋ฏ€๋กœ ํŒŒ์ผ ํฌ์ธํ„ฐ์˜ ์œ„์น˜๋ฅผ ์ž˜ ์ƒ๊ฐํ•˜๋ฉฐ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

์ƒ์„ฑ์ž

์„ค๋ช…

RandomAccessFile(File file, String mode)

์ž…์ถœ๋ ฅ์„ ํ•  File๊ณผ ์ž…์ถœ๋ ฅ mode๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. mode์—๋Š” ์ฝ๊ธฐ ์ „์šฉ "r"๊ณผ ์ฝ๊ณ  ์“ฐ๊ธฐ ๊ธฐ๋Šฅ์ธ "rw"๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

RandomAccessFile(String file, String mode)

์ž…์ถœ๋ ฅ์„ ํ•  ํŒŒ์ผ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ›๊ณ  ์ž…์ถœ๋ ฅ mode๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. mode์—๋Š” ์ฝ๊ธฐ ์ „์šฉ "r"๊ณผ ์ฝ๊ณ  ์“ฐ๊ธฐ ๊ธฐ๋Šฅ์ธ "rw"๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โ€‹

โ€‹

*RandomAccessFile์„ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ž๋ฃŒํ˜• ๊ฐ’์„ ์ฝ๊ณ  ์“ฐ๊ธฐ

โ€‹

RandomAccessFileTest.java

package stream.others;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
	public static void main(String[] args) throws IOException {
		RandomAccessFile rf = new RandomAccessFile("random.txt", "rw");
		rf.writeInt(100); // int 4 ๋ฐ”์ดํŠธ
		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer()); // ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
		rf.writeDouble(3.14); // double 8 ๋ฐ”์ดํŠธ
		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer());
		rf.writeUTF("์•ˆ๋…•ํ•˜์„ธ์š”"); // ์ˆ˜์ •๋œ UTF-8 ์‚ฌ์šฉ ํ•œ๊ธ€(3๋ฐ”์ดํŠธ) * 5 + null ๋ฌธ์ž(2๋ฐ”์ดํŠธ) = 17
		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer());

		int i = rf.readInt();
		double d = rf.readDouble();
		String str = rf.readUTF();

		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer());
		System.out.println(i);
		System.out.println(d);
		System.out.println(str);
	}
}

โ€‹<์‹คํ–‰ ๊ฒฐ๊ณผ>

ํŒŒ์ผ ํฌ์ธํ„ฐ์˜ ์œ„์น˜๊ฐ€ ๋งจ ์ฒ˜์Œ์œผ๋กœ ์˜ฎ๊ฒจ์ง€์ง€ ์•Š์•„์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

โ€‹

ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜๋ฅผ ์ด๋™ํ•ด ์ฃผ๋Š” seek( ) ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋งจ ์ฒ˜์Œ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

package stream.others;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
	public static void main(String[] args) throws IOException {
		RandomAccessFile rf = new RandomAccessFile("random.txt", "rw");
		rf.writeInt(100); // int 4 ๋ฐ”์ดํŠธ
		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer());
		rf.writeDouble(3.14); // double 8 ๋ฐ”์ดํŠธ
		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer());
		rf.writeUTF("์•ˆ๋…•ํ•˜์„ธ์š”"); // ์ˆ˜์ •๋œ UTF-8 ์‚ฌ์šฉ ํ•œ๊ธ€(3๋ฐ”์ดํŠธ) * 5 + null ๋ฌธ์ž(2๋ฐ”์ดํŠธ) = 17
		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer());

		rf.seek(0); // ํŒŒ์ผ ํฌ์ธํ„ฐ์˜ ์œ„์น˜๋ฅผ ๋งจ ์ฒ˜์Œ์œผ๋กœ ์˜ฎ๊น€
		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer());

		int i = rf.readInt();
		double d = rf.readDouble();
		String str = rf.readUTF();

		System.out.println("ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ : " + rf.getFilePointer()); // ์ฝ๊ธฐ๊ฐ€ ๋๋‚œ ํ›„ ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜๋ฅผ ์ถœ๋ ฅํ•จ

		System.out.println(i);
		System.out.println(d);
		System.out.println(str);
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

์—ฐ์Šต๋ฌธ์ œ Q3) FileOutputStream๊ณผ OutputStreamWriter๋ฅผ ํ™œ์šฉํ•˜์—ฌ a.txt ํŒŒ์ผ์— ๋‹ค์Œ์ฒ˜๋Ÿผ ์ถœ๋ ฅํ•ด๋ณด๋ผ.

 

โ€‹

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

package test;

import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.IOException;

public class test1 {
	public static void main(String[] args) throws IOException {
		try (FileOutputStream fos = new FileOutputStream("a.txt"); // ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ
		        OutputStreamWriter osw = new OutputStreamWriter(fos)) { // ๋ณด์กฐ ์ŠคํŠธ๋ฆผ
            osw.write("์ง€๊ธˆ๊นŒ์ง€ ์ž๋ฐ” ์ •๋ง ์žฌ๋ฏธ์žˆ๊ฒŒ ๊ณต๋ถ€ํ–ˆ์–ด์š”^^");
        } catch (IOException e) {
            System.out.println(e);
        }
		System.out.println("์ถœ๋ ฅ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

๋”๋ณด๊ธฐ

<์„ ์ƒ๋‹˜์ด ํ•œ ๊ฒƒ>

package test;

import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.IOException;

public class test1 {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("a.txt"); // ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ
		OutputStreamWriter osw = new OutputStreamWriter(fos); // ๋ณด์กฐ ์ŠคํŠธ๋ฆผ

		osw.write("์ง€๊ธˆ๊นŒ์ง€ ์ž๋ฐ” ์ •๋ง ์žฌ๋ฏธ์žˆ๊ฒŒ ๊ณต๋ถ€ํ–ˆ์–ด์š”^^");
		osw.flush(); // ์ž”๋ฅ˜ํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋˜๋„๋ก ํ•˜๋Š”๊ฒƒ
	}
}

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€