Backend/JAVA

JAVA 코딩 API (Application Programming Interface) - String, Object

쏠솔랄라 2023. 3. 3. 11:11

 

 

api.io.object

 

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Test01 {
public static void main(String[] args) {

int[] data = new int[] {4,1,9,15,11,6,2};
boolean[] flag = new boolean[] {true,true,false};
String day = "WED";

//객체 입출력
ObjectOutputStream oos = null;

try {
oos = new ObjectOutputStream(new FileOutputStream("arrays.txt"));
// CPU -> oos -> out -> target -> 실제 파일
//ObjectOutputStream을 열게 되면 기본 4byte의 출력이 일단 일어난다...

oos.writeObject(data);
oos.writeObject(flag);
oos.writeObject(day);

} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(oos != null) {
oos.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}


파일내용

콘솔창에 출력되지 않음

 

 

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.util.Arrays;

public class Test02 {

public static void main(String[] args) {

 

ObjectInputStream ois = null;

 

//객체 입출력

try {

ois = new ObjectInputStream(new FileInputStream("arrays.txt"));

//ObjectInputStream을 열게 되면 기본 4byte의 입력이 일단 일어난다

 

Object o = ois.readObject();

System.out.println(o.getClass());

System.out.println(o instanceof int[]);

 

int[] data = (int[])o;

System.out.println(Arrays.toString(data));

 

boolean[] flags = (boolean[])ois.readObject();

System.out.println(Arrays.toString(flags));

 

Object s = ois.readObject();

 

if (s instanceof String) {

System.out.println("문자열이다");

String day = (String)s;

System.out.println(day);

} else {

System.out.println("문자열이 아니다");

}

 

} catch(IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

 

 

출력화면

 

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.List;

class Item implements Serializable {

 

/**

*

*/

private static final long serialVersionUID = 8324696945366661518L;

 

String name;

 

public Item(String name) {

this.name = name;

}

}

class User implements Serializable {

 

/**

*

*/

private static final long serialVersionUID = 1L;

// 직렬화 할 때 그 값을 체크하는 용도의 아이디값

 

String nick;

int exp;

int money = 500;

List<Item> i;

 

public User(String nick) {

this.nick = nick;

i = new ArrayList<Item>();

}

 

@Override

public String toString () {

return nick + "\t[" + exp + "xp, " + money + "]";

}

}

public class Test03 {

 

public static void main (String[] args) {

 

User u = new User("유저아이디");

 

System.out.println(u);

 

// close() : 클로즈 작업이 가능한 Closeable 객체에 한해 가능하므로 객체 상속을 받아야 한다

// 자동으로 close까지 가능하도록 만든 문법 ; JAVA SE 7 버전에서 나옴

 

try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(u.nick + ".txt"))) {

oos.writeObject(u);

// java.io.NotSerializableException: api.io.object.User

// 모든 객체가 Object I/O가 가능한 것이 아니다

// byte(숫자)화가 가능한 객체들에 한해서만 가능하다 : 직렬화

// -> serializable을 상속받아 사용하면 된다

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

출력화면

 

 

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Test04 {
public static void main(String[] args) {

try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("유저아이디.txt"))){
User u = (User)ois.readObject();

System.out.println(u);

} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
 

 

파일 내 입력

 

 




api.io.String

 

 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Test01 {
public static void main (String[] args) {

// 파일 내 문자열 저장
File target = new File("files", "string1.txt");

OutputStream out = null;

String text = "어둠만이 나의 전부였던 동안\r\n"
+ "숨이 벅차도록 달려왔잖아\r\n"
+ "Never say “time's up”\r\n"
+ "경계의 끝자락\r\n"
+ "내 끝은 아니니까\r\n"
+ "울타리 밖에 일렁이는 무언가\r\n"
+ "그 아무도 모르는 별일지 몰라\r\n"
+ "I wanna wanna be there\r\n"
+ "I'm gonna gonna be there\r\n"
+ "벅찬 맘으로 이 궤도를 벗어나\r\n"
+ "Let's go!\r\n"
+ "새로운 길의 탐험가\r\n"
+ "Beyond the road\r\n"
+ "껍질을 깨뜨려버리자\r\n"
+ "두려움은 이제 거둬\r\n"
+ "오로지 나를 믿어\r\n"
+ "지금이 바로 time to fly\r\n"
+ "두 눈 앞의 끝, 사뿐 넘어가\r\n"
+ "한계 밖의 trip, 짜릿하잖아\r\n"
+ "녹이 슨 심장에 쉼 없이 피는 꿈\r\n"
+ "무모하대도 믿어 난";

try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

byte[] data = text.getBytes();

try {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}

try {
if(out !=null) {
out.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}

 

 

파일 내 입력

 


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class Test02 {
public static void main(String[] args) {

//문자열 전용 Stream을 이용하면 편하게 입출력도 가능하다

File target = new File("files","string2.txt");

Writer out = null;
PrintWriter printer = null;

try {
out = new FileWriter(target);
printer = new PrintWriter(target);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}

// try {
// out.write("자바\n");
//
// } catch (IOException e) {
// e.printStackTrace();
// }

printer.append("자바\n");
printer.append("자바");
// 기존 파일 뒤에 붙임

printer.println("자바");
printer.println("자바");
printer.println("자바");
printer.println("자바");
printer.println("자바");

try {
if(out != null) {
out.close();
}
if(printer != null) {
printer.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}


파일 내 입력