'Java > 레퍼런스 형변환' 카테고리의 다른 글
| 레퍼런스 형변환 오버라이딩 적용시 (0) | 2012.04.13 |
|---|---|
| 레퍼런스 형변환 (0) | 2012.04.13 |
| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
| 레퍼런스 형변환 오버라이딩 적용시 (0) | 2012.04.13 |
|---|---|
| 레퍼런스 형변환 (0) | 2012.04.13 |
| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
| 예비 (0) | 2012.04.13 |
|---|---|
| 레퍼런스 형변환 (0) | 2012.04.13 |
| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
package com.cast3;
public class CastingTest1 {
public static void main(String[] args){
FireEngine fe = new FireEngine();
fe.water();
Car car = null; //객체 선언(객체의 주소가 보관될 변수 선언)
car = fe; //자식 클래스 타입 -> 부모 클래스 타입 (업캐스팅)자동형변환
//car.water();
FireEngine fe2 = null;
fe2 = (FireEngine)car;//부모 클래스 타입->자식 클래스 타입
fe2.water(); //(다운 캐스팅)명시적 형변환
}
}
class Car{
String color;
int door;
void drive(){
System.out.println("Drive brrrrr~");
}
void stop(){
System.out.println("stop!!!");
}
}
class FireEngine extends Car{
void water(){
System.out.println("water!!!");
}
}| 예비 (0) | 2012.04.13 |
|---|---|
| 레퍼런스 형변환 오버라이딩 적용시 (0) | 2012.04.13 |
| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
package com.cast2;//레퍼런스 형변환에 오버라이딩적용
//오버라이딩은 다 같지만 전달되는 내용이 다를때 오버라이딩이라함
class Parent3{
String msg="봄";
public void make(){
System.out.println("부모 클래스");
}
}
class Child3 extends Parent3{
String msg="겨울";
public void make(){
System.out.println("자식 클래스");
}
}
public class CastEx4 {
public static void main(String[] args){
Child3 ch = new Child3();
System.out.println(ch.msg);
ch.make();
Parent3 p = ch; //자식 클래스 타입 -> 부모 클래스 타입
//업 캐스팅, 자동형변환
System.out.println(p.msg);
//오버 라이딩은 가려지지 않아 자식클래스를 호출하게된다
//무조건 자식클래스를 호출하게됨(형변환을 하더라도)
//그래서 출력하려면 super()를 사용해야됨
p.make();
}
}| 레퍼런스 형변환 오버라이딩 적용시 (0) | 2012.04.13 |
|---|---|
| 레퍼런스 형변환 (0) | 2012.04.13 |
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
| 업 캐스팅 예제 (0) | 2012.04.13 |
package com.cast3;//슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출
class Parent{
public void parentPrn(){
System.out.println("슈퍼 클래스 : parentPrn 메서드");
}
}
class Child extends Parent{
//메서드 오버라이딩(재정의)
public void parentPrn(){
System.out.println("서브 클래스 : 오버라이딩된 parentPrn 메서드");
}
public void childPrn(){
System.out.println("서브 클래스 : ChildPrn 메서드");
}
}
public class RefTest06 {
public static void main(String[] args){
Child c = new Child();
c.parentPrn();
Parent p;
p=c; //자식 클래스 타입 -> 부모 클래스 타입
//업 캐스팅, 자동형변환
p.parentPrn();
//오버라이딩 되면은 원래 되로 출력되지 않고 서브 클래스가 출력된다
//형변환이 되었더라도
}
}
| 레퍼런스 형변환 (0) | 2012.04.13 |
|---|---|
| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
| 업 캐스팅 예제 (0) | 2012.04.13 |
| 레퍼런스 형변환 (0) | 2012.04.13 |
package com.cast2;//레퍼런스 형변환 예제3
class Parent2{
String msg = "여름";
public String getMsg(){
return msg;
}
}
class Child2 extends Parent2{
String str = "겨울";
public String getStr(){
return str;
}
}
public class CastEx3 {
public static void main(String[] args){
Child2 ch = new Child2();
System.out.println(ch.msg);
System.out.println(ch.getMsg());
System.out.println(ch.str);
System.out.println(ch.getStr());
Parent2 p = ch; //자식 클래스 타입 -> 부모 클래스 타입
//업 캐스팅, 자동 형변환
System.out.println(p.msg);
System.out.println(p.getMsg());
/*호출 범위를 벗어나 호출 불가 (부모 영역만 호출 가능)
System.out.println(p.str);
System.out.println(p.getStr());*/
Child2 ch2 = (Child2)p;//부모 클래스 타입 -> 자식 클래스 타입
//다운 캐스팅 , 명시적 형변환
System.out.println(ch.msg);
System.out.println(ch.getMsg());
System.out.println(ch.str);
System.out.println(ch.getStr());
}
}
| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
|---|---|
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 업 캐스팅 예제 (0) | 2012.04.13 |
| 레퍼런스 형변환 (0) | 2012.04.13 |
| 레퍼런스 형변환(업 캐스팅과 다운 캐스팅) (0) | 2012.04.13 |
package com.cast2;//업 캐스팅 예제
class Parent{
public void parentPrn(){
System.out.println("슈퍼 클래스 : parentPrn 메서드");
}
}
class Child extends Parent{
public void childPrn(){
System.out.println("서브 클래스 : childPrn 메서드");
}
}
public class RefTest01 {
public static void main(String[] args){
Child c = new Child();
c.parentPrn();
c.childPrn();
Parent p;
p=c; //자식 클래스 타입 -> 부모클래스 타입
// 업캐스팅,자동 형변환 (Parent p = c;)
p.parentPrn();
//호출 범위를 벗어나 호출 불가
//p.chidlPrn();
}
}
| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
|---|---|
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
| 레퍼런스 형변환 (0) | 2012.04.13 |
| 레퍼런스 형변환(업 캐스팅과 다운 캐스팅) (0) | 2012.04.13 |
package com.cast;//레퍼런스 형변환 2
class Parent2{
public void make(){
System.out.println("눈오는 하루");
}
}
class Child2 extends Parent2{
public void fun(){
System.out.println("즐거운 하루");
}
}
public class CastEx2 {
public static void main(String[] args){
Child2 c = new Child2();
c.make();
c.fun();
Parent2 p =c;//c에 있는것을 p로 넘기기(업 캐스팅)
//자식 클래스 타입 -> 부모클래스 타입으로 형변환(자동형변환)
p.make();
//호출 범위를 벗어나 호출 불가
//p.fun();
Child2 c2 = (Child2)p;//부모 클래스 타입 -> 자식클래스 타입
//다운 캐스팅,명시적 형변환
//줄어 들었다 다시 늘려야 하기때문에 앞에 명시적 형변환을 하기 위해(Child2)를 명시
c2.make();
c2.fun();
}
}| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
|---|---|
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
| 업 캐스팅 예제 (0) | 2012.04.13 |
| 레퍼런스 형변환(업 캐스팅과 다운 캐스팅) (0) | 2012.04.13 |
package com.cast;//레퍼런스 형변환 업캐스팅, 다운캐스팅
//레퍼런스 형변환은 부모와 자식간의 관계에서만 가능
class Parent{
int a=100;
}
class Child extends Parent{
int b=200;
}
public class CastEx {
public static void main(String[] args){
Child c = new Child();
System.out.println(c.a);
System.out.println(c.b);
Parent p = c; //자식 클래스 타입 -> 부모 클래스 타입으로
//업 캐스팅, 자동적으로 형변환
System.out.println(p.a);
/*System.out.println(p.b);
호출 범위를 벗어나 호출 불가*/
//child는 전체를 호출할 수 있지만
//parent는 child 영역을 호출 할 수 없기에 오류남
//호출 하기 위해 다시 형변환
Child c2= (Child)p;//부모 클래스 타입 -> 자식클래스 타입
//다운 캐스팅, 명시적으로 형변환
System.out.println(c2.a);
System.out.println(c2.b);
}
}| 레퍼런스 형변환에 오버라이딩 적용 (0) | 2012.04.13 |
|---|---|
| 슈퍼 클래스형 레퍼런스 변수로 오버라이딩된 메서드 호출 (0) | 2012.04.13 |
| 레퍼런스 형변환 예제3(업 캐스팅 , 다운 캐스팅) (0) | 2012.04.13 |
| 업 캐스팅 예제 (0) | 2012.04.13 |
| 레퍼런스 형변환 (0) | 2012.04.13 |
package com.socket;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class MultiClient implements ActionListener {
// Socket 통신을 위한 객체 선언
private Socket socket;
private ObjectInputStream ois;
private ObjectOutputStream oos;
// 화면 UI를 위한 객체 선언
private JFrame jframe;
private JTextField jtf;
private JTextArea jta;
private JLabel jlb1, jlb2;
private JPanel jp1, jp2;
private JButton jbtn;
// ip주소와 id저장
private String ip;
private String id;
public MultiClient(String argIp, String argId) {
ip = argIp;
id = argId;
jframe = new JFrame("Multi Chatting");
jtf = new JTextField(30);
jta = new JTextArea("", 10, 50);
jlb1 = new JLabel("Usage ID : [[ " + id + "]]");
jlb2 = new JLabel("IP : " + ip);
jbtn = new JButton("종료");
jp1 = new JPanel();
jp2 = new JPanel();
jlb1.setBackground(Color.yellow);
jlb2.setBackground(Color.green);
jta.setBackground(Color.pink);
jp1.setLayout(new BorderLayout());
jp2.setLayout(new BorderLayout());
jp1.add(jbtn, BorderLayout.EAST);
jp1.add(jtf, BorderLayout.CENTER);
jp2.add(jlb1, BorderLayout.CENTER);
jp2.add(jlb2, BorderLayout.EAST);
jframe.add(jp1, BorderLayout.SOUTH);
jframe.add(jp2, BorderLayout.NORTH);
JScrollPane jsp = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jframe.add(jsp, BorderLayout.CENTER);
jtf.addActionListener(this);
jbtn.addActionListener(this);
jframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
oos.writeObject(id + "#exit");
} catch (IOException ee) {
ee.printStackTrace();
}
System.exit(0);
}
public void windowOpened(WindowEvent e) {
jtf.requestFocus();
}
});
jta.setEditable(false);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int screenHeight = d.height;
int screenWidth = d.width;
jframe.pack();
jframe.setLocation((screenWidth - jframe.getWidth()) / 2,
(screenHeight - jframe.getHeight()) / 2);
jframe.setResizable(false);
jframe.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
String msg = jtf.getText();
if (obj == jtf) {
if (msg == null || msg.length() == 0) {
JOptionPane.showMessageDialog(jframe, "글을 쓰세요", "경고",
JOptionPane.WARNING_MESSAGE);
} else {
try {
oos.writeObject(id + "#" + msg);
} catch (IOException ee) {
ee.printStackTrace();
}
jtf.setText("");
}
} else if (obj == jbtn) {
try {
oos.writeObject(id + "#exit");
} catch (IOException ee) {
ee.printStackTrace();
}
System.exit(0);
}
}
public void exit(){
System.exit(0);
}
public void init() throws IOException{
socket = new Socket(ip,5000);
System.out.println("connected....");
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
MultiClientThread ct = new MultiClientThread(this);
Thread t = new Thread(ct);
t.start();
}
public static void main(String[] args) throws IOException {
JFrame.setDefaultLookAndFeelDecorated(true);
MultiClient cc = new MultiClient("211.183.2.80", "쌍칼");
cc.init();
}
public ObjectInputStream getOis(){
return ois;
}
public JTextArea getJta(){
return jta;
}
public String getId(){
return id;
}
}
package com.socket;
public class MultiClientThread extends Thread {
private MultiClient mc;
public MultiClientThread(MultiClient mc) {
this.mc = mc;
}
public void run() {
String message = null;
String[] receivedMsg = null;
boolean isStop = false;
while (!isStop) {
try {
message = (String) mc.getOis().readObject();
receivedMsg = message.split("#");
} catch (Exception e) {
e.printStackTrace();
isStop = true;
}
System.out.println(receivedMsg[0] + "," + receivedMsg[1]);
if (receivedMsg[1].equals("exit")) {
if (receivedMsg[0].equals(mc.getId())) {
mc.exit();
} else {
mc.getJta().append(receivedMsg[0]
+"님이 종료 하셨습니다."+System.getProperty("line.separator"));
mc.getJta().setCaretPosition(mc.getJta().getDocument().getLength());
}
}else{
mc.getJta().append(receivedMsg[0]+" : "
+receivedMsg[1]+System.getProperty("line.separator"));
mc.getJta().setCaretPosition(mc.getJta().getDocument().getLength());
}
}
}
}
package com.socket;//채팅방 클라이언트
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class EchoClient {
private String ip;
private int port;
private String str;
BufferedReader file;
public EchoClient(String ip, int port)throws IOException{
this.ip =ip;
this.port = port;
Socket tcpSocket = getSocket();
OutputStream os_socket = tcpSocket.getOutputStream();
InputStream is_socket = tcpSocket.getInputStream();
//서버로 전송할 데이터를 처리하기 위한 객체
BufferedWriter bufferW = new BufferedWriter(new OutputStreamWriter(os_socket));
//서버에서 전송된 데이터 를 처리하기 위한 객체
BufferedReader bufferR = new BufferedReader(new InputStreamReader(is_socket));
System.out.print("입력 : ");
file = new BufferedReader(new InputStreamReader(System.in));
str = file.readLine();
str += System.getProperty("line.separator");
//서버로 데이터 전송 작업 수행
bufferW.write(str);
bufferW.flush();
//서버로 부터 전송된 데이터를 읽는 작업 수행
str = bufferR.readLine();
System.out.println("Echo Result : "+str);
file.close();
bufferW.close();
bufferR.close();
tcpSocket.close();
}
public Socket getSocket(){
Socket tcpSocket = null;
try{
tcpSocket = new Socket(ip,port);
}catch (IOException ioe){
ioe.printStackTrace();
System.exit(0);
}
return tcpSocket;
}
public static void main(String[] args) throws IOException{
new EchoClient("211.183.2.73",3000);
}
}
package com.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer{
private BufferedReader bufferR;
private BufferedWriter bufferW;
private InputStream is;
private OutputStream os;
private ServerSocket serverS;
public EchoServer(int port){
try{
serverS = new ServerSocket(port);
}catch(IOException ioe){
ioe.printStackTrace();
System.exit(0);
}
while(true){
try{
System.out.println("클라이언트를 요청을 기다리는 중");
Socket tcpSocket = serverS.accept();
System.out.println("클라이언트의 IP 주소 : "+
tcpSocket.getInetAddress().getHostAddress());
is = tcpSocket.getInputStream();
os = tcpSocket.getOutputStream();
bufferR = new BufferedReader(
new InputStreamReader(is));
bufferW = new BufferedWriter(
new OutputStreamWriter(os));
String message = bufferR.readLine();
System.out.println("수신메시지 : "+ message);
message = message +
System.getProperty("line.separator");
bufferW.write(message);
bufferW.flush();
bufferR.close();
bufferW.close();
tcpSocket.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
public static void main(String[] args){
new EchoServer(3000);
}
}
package com.basic;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class InetAddressTest02 {
public static void main(String [] args) throws Exception{
BufferedReader reader;
String url = null;
InetAddress addr = null;
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("웹사이트 주소를 입력하세요 ->");
url = reader.readLine();
try{
addr = InetAddress.getByName(url);
}catch(UnknownHostException e){
e.printStackTrace();
}
System.out.println("==========================");
//getHostAddress();
System.out.println(url+"의 IP 번호 = "+addr.getHostAddress());
}
}
package com.basic;
import java.net.URL;
import java.io.IOException;
import java.net.MalformedURLException;
public class URLEx {
public static void main(String[] args) throws MalformedURLException, IOException{
URL url = new URL("http://java.sun.com/index.jsp?name=syh1011#content");
System.out.println("프로토콜 : "+url.getProtocol());
System.out.println("호스트 : "+url.getHost());
System.out.println("포트 : "+url.getDefaultPort());
System.out.println("패스 : "+url.getPath());
System.out.println("Query: "+url.getQuery());
System.out.println("reference: "+url.getRef());
System.out.println("주소: "+url.toExternalForm());
}
}
package com.basic;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class NetworkEx4 {
public static void main(String[] args) {
URL url = null;
BufferedReader input = null;
String address = "http://www.google.co.kr";
String line = "";
try{
//URL : 인터넷 URL 주소를 추상화 하는 클래스
url = new URL(address);
//url.openStream() : URL객체에 저장된 URL주소의 데이터를 InputStream으로 반환
input = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
while((line = input.readLine()) !=null) {
System.out.println(line);
}
input.close();
}catch(Exception e){
e.printStackTrace();
}
}
}| 직렬화 (0) | 2012.04.13 |
|---|---|
| File 생성 및 변경 (0) | 2012.04.13 |
| FileEx4 (0) | 2012.04.13 |
| 디렉토리 정보 (0) | 2012.04.13 |
| 입출력으로 파일 상세정보 (0) | 2012.04.13 |
package com.serial;
import java.io.Serializable;
//Serializable 인터페이스를 구현하는 것은 해당클래스가 직렬화 대상임을 명시하는 역할
public class Customer implements Serializable{
//멤버 변수
private String name;
//생성자
public Customer(String name){
this.name=name;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
//Object to String()재정의
public String toString(){
return "당신의 이름: "+name;
}
public static void main(String[] args) {
}
}
package com.serial;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
public class ObjectInOutputStreamEx {
public static void main(String[] args) {
ObjectOutputStream oos=null;
FileOutputStream fos=null;
ObjectInputStream ois=null;
FileInputStream fis=null;
try{
fos=new FileOutputStream("Object.ser");
//객체 직렬화
oos=new ObjectOutputStream(fos);
oos.writeObject(new Customer("홍길동"));
fis=new FileInputStream("object.ser");
ois=new ObjectInputStream(fis);
Customer m=(Customer)ois.readObject();
System.out.println(m);
}catch(IOException ioe){
ioe.printStackTrace();
}catch (ClassNotFoundException cnfe){
cnfe.printStackTrace();
}finally{
try{
if(oos!=null)oos.close();
if(fos!=null)fos.close();
if(ois!=null)ois.close();
if(fis!=null)fis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
package com.serial;
import java.io.Serializable;
public class UserInfo implements Serializable{
String id;
String password;
int num;
public UserInfo(String id, String password,int num){
this.id= id;
this.password= password;
this.num= num;
}
@Override
public String toString(){
return "UserInfo {id="+id+", password ="+ password+", num="+num+"}";
}
}
package com.serial;
import java.util.ArrayList;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
public class SerialEx1 {
public static void main(String[] args) {
try{
String fileName = "UserInfo.ser";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream out = new ObjectOutputStream(fos);
UserInfo u1 =new UserInfo("JavaMan","1234",30);
UserInfo u2 =new UserInfo("JavaWoMan","4321",26);
ArrayList list = new ArrayList();
list.add(u1);
list.add(u2);
//객체를 직렬화 한다.
out.writeObject(u1);
out.writeObject(u2);
out.writeObject(list);
out.close();
fos.close();
System.out.println("직렬화가 잘 끝났습니다.");
}catch(IOException e){
e.printStackTrace();
}
}//main
}//class
package com.serial;
import java.util.ArrayList;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class SerialEx2 {
public static void main(String[] args) {
try{
String fileName = "UserInfo.ser";
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream in = new ObjectInputStream(fis);
//객체를 읽을때는 출력한 순서와 일치해야 한다.
UserInfo u1 = (UserInfo)in.readObject();
UserInfo u2 = (UserInfo)in.readObject();
ArrayList list = (ArrayList)in.readObject();
System.out.println(u1);
System.out.println(u2);
System.out.println(list);
in.close();
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}//main
}//class| 예비 (0) | 2012.04.13 |
|---|---|
| File 생성 및 변경 (0) | 2012.04.13 |
| FileEx4 (0) | 2012.04.13 |
| 디렉토리 정보 (0) | 2012.04.13 |
| 입출력으로 파일 상세정보 (0) | 2012.04.13 |
package com.file;
import java.io.File;
import java.io.IOException;
public class FileTest02 {
public static void main(String[] args) throws IOException{
System.out.println("===파일 생성===");
//시스템에서 사용하는 디렉토리 구분자를 반환
//System.getProperty("file.separator") = File.separator
File f3 = new File("c:"+File.separator+"test.txt");
//없으면 만들면서 true 리턴 있으면 false 리턴
System.out.println(f3.createNewFile());
System.out.println(f3.getPath());
System.out.println(f3.getName());
System.out.println(f3.getParent());
System.out.println("===파일명 변경===");
File f4 = new File("c:"+File.separator+"test1.txt");
System.out.println(f3.renameTo(f4));
//없는 디렉토리일 경우 만들고 true 리턴
System.out.println("===디렉토리 생성===");
//디렉토리 생성
File f5 = new File("c:"+File.separator+"year");
System.out.println(f5.mkdir());
}
}