4.3
package ch14.unit03;
import java.io.RandomAccessFile;
public class Ex61_RandomFile {
public static void main(String[] args) {
RandomAccessFile raf=null;
byte b;
try {
raf = new RandomAccessFile("ex.txt", "rw");
for(int n = 65; n<=90;n++) {
raf.write(n);
}
raf.seek(5); // ๋ด๊ฐ ์ฝ๊ณ ์ถ์ ์์น๋ก ์ด๋, A=0
b=raf.readByte();
System.out.write(b); // F
raf.seek(10); // ์ฒ์๋ถํฐ 10 ๊ฑด๋๋
b=raf.readByte();
System.out.write(b); // K
b=raf.readByte();
System.out.write(b); // L
System.out.flush();
System.out.println();
// ์ฒ์๋ถํฐ ์ฝ๊ธฐ
raf.seek(0);
for(int i=0; i<(int)raf.length();i++) {
b=raf.readByte();
System.out.write(b);
}
System.out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(raf!=null) {
try {
raf.close();
} catch (Exception e2) {
}
}
}
}
}
PrintWriter
package ch14.unit04;
import java.io.PrintWriter;
public class Ex01_PrintWriter {
public static void main(String[] args) {
// PrintWriter : ๋ฌธ์ ์คํธ๋ฆผ
/*
* PrintWriter pw=new PrintWriter(System.out);
* pw.print("์๋ฐ");
* pw.print("์ค๋ผํด");
* pw.println("์น");
*
*
* pw.flush(); // flush()๋ฅผ ํด์ผ ์ถ๋ ฅ ๊ฐ๋ฅ
*/
//autoFlush๋ฅผ true๋ก ์ค์ ํ๋ฉด println()์ ํธ์ถํ๊ฑฐ๋ flush()๋ฅผ ํธ์ถํ๋ฉด ์ถ๋ ฅ
PrintWriter pw=new PrintWriter(System.out,true);
pw.print("์๋ฐ");
pw.print("์ค๋ผํด");
pw.println("์น");
pw.flush(); // flush()๋ฅผ ํด์ผ ์ถ๋ ฅ ๊ฐ๋ฅ
}
}
DataOutputStream
package ch14.unit04;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class Ex02_DataOutputStream {
public static void main(String[] args) {
// DataOutputStream : ๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ถ๋ ฅ์ด ๊ฐ๋ฅ
DataOutputStream dos=null;
try {
dos=new DataOutputStream(new FileOutputStream("test.txt"));
dos.writeUTF("์์ธ");
dos.writeByte(10);
dos.writeChar('A');
dos.writeInt(50);
dos.writeUTF("๋ํ");
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ!");
} catch (Exception e) {
e.printStackTrace();
}finally {
if(dos!=null) {
try {
dos.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}
package ch14.unit04;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Ex03 {
public static void main(String[] args) {
try(DataInputStream dis=new DataInputStream(new FileInputStream("test.txt"))) {
String s=dis.readUTF();
System.out.println(s);
System.out.println(dis.readByte());
System.out.println(dis.readChar());
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
//์ ์ฅ๋ ์์๋๋ก ์ฝ์ด์์ผ ํ๋ค.
} catch (FileNotFoundException e) {
} catch (EOFException e) {
// ํ์ผ์ ๋์ ๋๋ฌํ๋ฉด (์ฝ์๊ฒ์ด ์์ผ๋ฉด) EOFException ๋ฐ์
} catch (IOException e) {
} catch (Exception e) {
// TODO: handle exception
}
}
}
ObjectOutputStream
package ch15.unit01;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;
public class Ex01_ObjectOutputStream {
public static void main(String[] args) {
Hashtable<String, Integer> ht=new Hashtable<String, Integer>();
ht.put("java", 100);
ht.put("spring", 70);
ht.put("html", 80);
ht.put("css", 80);
ht.put("oracle", 90);
String pathname="objext.txt";
// ObjectOutputStream
// ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ์ ์๋ ์คํธ๋ฆผ
// ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๊ธฐ ์ํด์๋ ํด๋น ํด๋์ค๋ ์ง๋ ฌํ๊ฐ ๋์ด์์ด์ผ ํ๋ค.
// Serializable ์ธํฐํ์ด์ค ๊ตฌํ๋์ด ์์ด์ผ ํ๋ค.
// ํ์ผ์ ์ต์ฌ์๋จ์๋ ํด๋์ค๋ช
์ด ์ ์ฅ๋๋ค.
try(ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(pathname))) {
oos.writeObject(ht);
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ!~!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package ch15.unit01;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Hashtable;
import java.util.Iterator;
public class Ex02_ObjectInputStream {
public static void main(String[] args) {
String pathname="objext.txt";
try(ObjectInputStream ois=new ObjectInputStream(new FileInputStream(pathname))) {
@SuppressWarnings("unchecked") // ๊ฒฝ๊ณ ํ์ ๋์ค๊ฒ ํ์ง๋ง!
Hashtable<String, Integer>ht=(Hashtable<String, Integer>)ois.readObject();
Iterator<String>it=ht.keySet().iterator();
while(it.hasNext()) {
String subject=it.next();
int score=ht.get(subject);
System.out.println(subject+"->"+score);
}
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ!~!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
'๐ป > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(38) [JAVA] 22์ผ (0) | 2024.04.10 |
---|---|
(36-37) [JAVA] 20-21์ผ (0) | 2024.04.10 |
(34) [JAVA] 18์ผ ์ ์ถ๋ ฅ ์คํธ๋ฆผ(I/O Stream) - 2 (0) | 2024.04.03 |
(33) [JAVA] 18์ผ ์ ์ถ๋ ฅ ์คํธ๋ฆผ(I/O Stream) (0) | 2024.04.03 |
[Java] 17์ผ์ฐจ : ์ปฌ๋ ์ (Collection) - 4 (0) | 2024.03.11 |