๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/JAVA

(35) [JAVA] 19์ผ

by ๋”ฐ๊ถˆ 2024. 4. 10.

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();
	}

}
}