
๐ฅค ๋นจ๋ - FileInputStream / FileOutputStream ์ฌ์ฉ
๋ฐ์ดํธ ๋จ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฐ๋ ๋ฐ์ด๋๋ฆฌ ์คํธ๋ฆผ
์ด๋ฏธ์ง, ๋์์, ์คํํ์ผ ๋ฑ ๋ชจ๋ ํ์ผ ํํ ์ฒ๋ฆฌ ๊ฐ๋ฅ
FileInputStream: ์
๋ ฅ ์คํธ๋ฆผ (๋ฐ์ด๋๋ฆฌ ์ฝ๊ธฐ์ฉ)
FileOutputStream: ์ถ๋ ฅ ์คํธ๋ฆผ (๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ์ฉ)
์คํธ๋ฆผ(Stream)
ํ์ค์์ stream์ "๋ฌผ์ค๊ธฐ", "ํ๋ฆ"์ ์๋ฏธํจ
์๋ฐ์์์ ์คํธ๋ฆผ์ "๋ฐ์ดํฐ์ ํ๋ฆ"์ ๋ปํจ
โ ํ์ผ, ํค๋ณด๋, ๋คํธ์ํฌ ๋ฑ ๋ค์ํ ์ ์ถ๋ ฅ ๋์๊ณผ ์คํ ์ค์ธ ํ๋ก๊ทธ๋จ ์ฌ์ด์์ ๋ฐ์ดํฐ๋ฅผ ํ ๋ฐฉํฅ์ผ๋ก ์ฃผ๊ณ ๋ฐ๊ธฐ ์ํ ํต๋ก
โ ์ฃผ์ ๋ฉ์๋
read() : 1๋ฐ์ดํธ์ฉ ์ฝ์ (๋ฆฌํด๊ฐ -1์ด๋ฉด ํ์ผ ๋)
ํธ์ถ ์๋ง๋ค ์ค์ค๋ก ๋ค์ ๋ฐ์ดํฐ ์ ๊ทผ
write(int b) : 1๋ฐ์ดํธ์ฉ ์ถ๋ ฅ
close() : ์ฌ์ฉํ ์คํธ๋ฆผ ์์ ํด์
โ
try-catch-finally ๋ฌธ ์ฌ์ฉ
try : ์์ธ๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ฝ๋(=์ํ ๊ตฌ๊ฐ)๋ฅผ ๊ฐ์ธ์ ์คํํ๋ ์์ญ
catch : ์์ธ๊ฐ ๋น์ ์ ์ข
๋ฃ๋์ง ์๊ฒ, ์ ์ ์ํ์ผ๋ก ์ ๋
finally : try์์ ์์ธ๊ฐ ๋ฐ์ํ๋ , catch๋ก ๋์ด๊ฐ๋ ์๊ด์์ด ๋ฌด์กฐ๊ฑด ์คํ๋๋ ์์ญ
ํ์ผ ์ ์ถ๋ ฅ ์ค ์ค๋ฅ ๋ฐ์ ๊ฐ๋ฅ์ฑ์ด ๋๊ธฐ ๋๋ฌธ์ ํ์
โ ์์ธ ๊ตฌ๋ถ ์ฒ๋ฆฌ
FileNotFoundException : ํ์ผ์ด ์กด์ฌํ์ง ์๊ฑฐ๋ ๊ฒฝ๋ก ์ค๋ฅIOException : ์ผ๋ฐ์ ์ธ ์
์ถ๋ ฅ ์ค๋ฅ (์ฝ๊ธฐ/์ฐ๊ธฐ ์คํจ ๋ฑ)โ ์์ ์ ๋ฆฌ
finally ๋ธ๋ก์์ close() ํธ์ถ
์
๋ ฅ์คํธ๋ฆผ์ด null์ด ์๋ ๊ฒฝ์ฐ์๋ง(=์กด์ฌํ ๋๋ง)
close() ์คํํ์ฌ ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ฐฉ์ง
์์ธ vs ์๋ฌ
Exception์ ๊ฐ๋ฐ์๊ฐ ์ฒ๋ฆฌํด์ผ ํ๋ ๋ฌธ์
Error๋ ๊ฐ๋ฐ์๊ฐ ์๋ ์ ์๋ ์ฌ๊ฐํ ๋ฌธ์ (JVM ์ฐจ์)
์ฒดํฌ์์ธ vs ์ธ์ฒดํฌ ์์ธ
๊ตฌ๋ถ โ Checked ์์ธ โ ๏ธ Unchecked ์์ธ ์ ์ ์ปดํ์ผ ์์ ์ ๋ฐ๋์ ์์ธ ์ฒ๋ฆฌํด์ผ ํ๋ ์์ธ ์ปดํ์ผ๋ฌ๊ฐ ์์ธ ์ฒ๋ฆฌ ๊ฐ์ ํ์ง ์์ ์์ธ ์ฒ๋ฆฌ ํ์? โญ try-catch
๋๋throws๋ก ๋ช ์ํด์ผ ํจโ ๊ฐ๋ฐ์์๊ฒ ๋งก๊น ์์ ๊ตฌ์กฐ Exceptionโ ์ง์ ์์RuntimeExceptionโ ํ์ ํด๋์ค๋ํ ์์ IOException,SQLException,FileNotFoundExceptionNullPointerException,ArithmeticException,ArrayIndexOutOfBoundsException์ฉ๋ ์ธ๋ถ ์์: ํ์ผ, DB, ๋คํธ์ํฌ ๋ฑ ๋ ผ๋ฆฌ ์ค๋ฅ: ์ฝ๋ ๋ด๋ถ ์ค์, ์๋ชป๋ ์ฌ์ฉ ๋ฑ ๊ฐ๋ฐ์ ์ค์? ์๋ (์ธ๋ถ ์์ธ) ๋ง์ (ํ๋ก๊ทธ๋๋จธ ์ค์)
public void copy() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(t_ori.getText());
fos = new FileOutputStream(t_dest.getText());
int data;
while ((data = fis.read()) != -1) {
fos.write(data);
}
// ๋ณต์ฌ ์๋ฃ ํ ์๋ฆผ
JOptionPane.showMessageDialog(this, "๋ณต์ฌ ์๋ฃ");
} catch (IOException e) {
// ๋ณต์ฌ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
e.printStackTrace();
JOptionPane.showMessageDialog(this, "๋ณต์ฌ ์ค ์ค๋ฅ ๋ฐ์");
} finally {
// ์คํธ๋ฆผ ์์ ์ ๋ฆฌ (๋ฌด์กฐ๊ฑด ์คํ)
try {
if (fis != null) fis.close();
if (fos != null) fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
JOptionPane.showMessageDialog(this, "๋ณต์ฌ์๋ฃ");
JOptionPane
์๋ฐ์์ ๊ฐ๋จํ ํ์
๋ฉ์์ง ์ฐฝ์ ๋์ฐ๊ธฐ ์ํ ํด๋์ค
showMessageDialog(๋ฉ์์ง์ฐฝ ๋์ธ ๋ถ๋ชจ ์ปดํฌ๋ํธ, ์
๋ ฅํ๊ณ ์ถ์ ํ
์คํธ)
ํ์
๋ฉ์์ง ์ถ๋ ฅ์ฉ ๋ฉ์๋
Js์ alert()๊ณผ ๊ฐ์ ์ญํ
e.printStackTrace() : ์๋ฌ์ ์์ธ์ ์ ์ ์๋ ๋ฉ์๋![]() | ![]() |
|---|
ํ์ผ ๊ฒฝ๋ก ์ ๋ ฅ์ ํตํ ๋ฐ์ด๋๋ฆฌ ํ์ผ ๋ณต์ฌํ๊ธฐ
![]() | ![]() |
|---|
ํ์ผ ๊ฒฝ๋ก๋ฅผ ํ์๊ธฐ๋ฅผ ํตํด ์ ํํ ์ ์๋๋ก ๊ฐ์
package com.sinse.ioproject;
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class GuiCopy extends JFrame implements ActionListener {
JLabel la_ori, la_dest;
JTextField t_ori, t_dest;
JButton bt_ori, bt_dest, bt_copy;
JFileChooser fileChooser;
public GuiCopy() {
la_ori = new JLabel("์๋ณธ");
la_dest = new JLabel("๋ณต์ฌ๋ณธ");
t_ori = new JTextField();
t_dest = new JTextField();
bt_ori = new JButton("์ด๊ธฐ");
bt_dest = new JButton("์ ์ฅ");
bt_copy = new JButton("๋ณต์ฌ");
fileChooser = new JFileChooser("C:/.../res");
Dimension d = new Dimension(100, 24);
Dimension d2 = new Dimension(330, 24);
la_ori.setPreferredSize(d);
la_dest.setPreferredSize(d);
t_ori.setPreferredSize(d2);
t_dest.setPreferredSize(d2);
bt_ori.setPreferredSize(d);
bt_dest.setPreferredSize(d);
bt_copy.setPreferredSize(d);
setLayout(new FlowLayout());
bt_ori.addActionListener(this);
bt_dest.addActionListener(this);
bt_copy.addActionListener(this);
add(la_ori);
add(t_ori);
add(bt_ori);
add(la_dest);
add(t_dest);
add(bt_dest);
add(bt_copy);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(580, 130);
setVisible(true);
}
public void copy() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(t_ori.getText());
fos = new FileOutputStream(t_dest.getText());
int data;
while ((data = fis.read()) != -1) {
fos.write(data);
}
JOptionPane.showMessageDialog(this, "๋ณต์ฌ ์๋ฃ");
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "๋ณต์ฌ ์ค ์ค๋ฅ ๋ฐ์");
} finally {
try {
if (fis != null) fis.close();
if (fos != null) fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == bt_ori) {
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
//์ ์ ๊ฐ ์ ํํ ํ์ผ ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ๊ฒฝ๋ก ์ถ์ถ
//๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ
์คํธ ํ๋์ ์ถ๋ ฅ
File file = fileChooser.getSelectedFile();
t_ori.setText(file.getAbsolutePath());
}
} else if (obj == bt_dest) {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
t_dest.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
} else if (obj == bt_copy) {
copy();
}
}
public static void main(String[] args) {
new GuiCopy();
}
}
์ก์ ์ด๋ฒคํธ๋ฆฌ์ค๋ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ ๋ถ๋ถ์์...
if (obj == bt_ori) {
int result = fileChooser.showOpenDialog(this);
// showOpenDialog : ์ฌ์ฉ์๊ฐ ์ด๋ค ๋ฒํผ์ ๋๋ ๋์ง์ ๋ฐ๋ผ ์ ์๊ฐ์ผ๋ก ๋ฐํ๋จ
if (result == JFileChooser.APPROVE_OPTION) {
//์ ์ ๊ฐ ์ ํํ ํ์ผ ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ๊ฒฝ๋ก ์ถ์ถ
//๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ
์คํธ ํ๋์ ์ถ๋ ฅ
File file = fileChooser.getSelectedFile();
t_ori.setText(file.getAbsolutePath());
}
} else if (obj == bt_dest) {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
t_dest.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
1๏ธโฃ
if(result == JFileChooser.APPROVE_OPTION){...}
fileChooser.showOpenDialog(this)(์ ์๊ฐ ๋ฐํ)์ด๋
JFileChooser.APPROVE_OPTION(์์ ๋ฐํ) ๊ฐ์ผ๋ฉด ์กฐ๊ฑด ์ํ
2๏ธโฃ
showOpenDialog()โ ํ์ผ์ "์ฝ๊ธฐ ์ํด" ์ ํ
showSaveDialog()โ ํ์ผ์ "์ฐ๊ธฐ ์ํด" ์ ํ๋ ๋ค JFileChooser์์ ์ ๊ณตํ๋ ๋ค์ด์ผ๋ก๊ทธ,
๋ ๋ค0= ์๋ฝ,1= ์ทจ์,-1= ์ค๋ฅ ์ฒด๊ณ
โ์ด๊ธฐ๋, ์ ์ฅ์ด๋โ ๋ชฉ์ ์ ๋ฐ๋ผ ํธ์ถํ๋ ๋ฉ์๋๊ฐ ๋ค๋ฅธ ๊ฒ๋ฟ
3๏ธโฃ
File file = fileChooser.getSelectedFile()
ํ์ผ ์ ํ ์ฐฝ(JFileChooser) ์์ ์ฌ์ฉ์๊ฐ ์ ํํ ํ์ผ ์ ๋ณด๋ฅผ
File ๊ฐ์ฒด ํํ๋ก ๋ฐ์์ค๋ ๊ฒ
getSelectedFile()์ ๋ฐํํ์ ์ด File ๊ฐ์ฒด์
๋ด๋ถ์์ ์๋์ผ๋ก new File( ) ํด์ ๋๊ฒจ์ฃผ๊ธฐ ๋๋ฌธ์ ๋ณ๋๋ก new File( ) ํ ํ์ ์์
File ๊ฐ์ฒด๋ ์ ํํ ํ์ผ์ ๊ฒฝ๋ก, ์ด๋ฆ, ํฌ๊ธฐ ๋ฑ์ ๋ค๋ฃฐ ์ ์์