package demo.writer;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

public class PrintWriterDemo1 {

	public static void main(String[] args) throws Exception {
		
		// PrintWriter writer = new PrintWriter("c:/temp/scores.csv");
		
		//File file = new File("c:/temp/scores.csv");
		//PrintWriter writer = new PrintWriter(file);
		
		PrintWriter writer = new PrintWriter(new FileWriter("c:/temp/scores.csv"));
		
		writer.println("김유신,전자공학과,3,3.8");
		writer.println("이순신,기계공학과,3,4.1");
		writer.println("강감찬,무기재료공학과,1,4.5");
		writer.println("홍길동,환경공학과,2,3.2");
		
		writer.flush();
		
		writer.close();
	}
}

'자바 > io' 카테고리의 다른 글

serialization  (0) 2019.06.10
reader  (0) 2019.06.10
file  (0) 2019.06.10
bytestream  (0) 2019.06.10
bridge  (0) 2019.06.10
package demo.serialization;

import java.io.Serializable;

public class Employee implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private String name;
	private String position;
	// 직렬화, 역직렬화 대상에서 제외된다.
	private transient int password;
	private Family child;
	
	public Employee() {}

	
	
	public Employee(String name, String position, int password, Family child) {
		super();
		this.name = name;
		this.position = position;
		this.password = password;
		this.child = child;
	}



	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public int getPassword() {
		return password;
	}

	public void setPassword(int password) {
		this.password = password;
	}

	public Family getChild() {
		return child;
	}

	public void setChild(Family child) {
		this.child = child;
	}
	
	
}
package demo.serialization;

import java.io.Serializable;

public class Family implements Serializable {

	private String name;
	private int count;
	
	public Family() {}

	public Family(String name, int count) {
		this.name = name;
		this.count = count;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
}
package demo.serialization;

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

public class SerializationDemo1 {

	public static void main(String[] args) throws Exception {
		
		Family f = new Family("자녀", 2);
		Employee employee = new Employee();
		employee.setName("홍길동");
		employee.setPosition("과장");
		employee.setPassword(1234);
		employee.setChild(f);
		
		FileOutputStream fos = new FileOutputStream("c:/temp/emp.sav");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		// void writeObject(Object o)
		// 객체를 직렬화한다.
		oos.writeObject(employee);
		
		oos.close();
	}
}
package demo.serialization;

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

public class DeserializationDemo1 {

	public static void main(String[] args) throws Exception {
		
		FileInputStream fis = new FileInputStream("c:/temp/emp.sav");
		ObjectInputStream ois = new ObjectInputStream(fis);
		
		// Object readObject()
		// 스트림으로 읽어들인 데이터를 이용해서 객체를 복원(역직렬화)한다.
		Object obj = ois.readObject();
		// System.out.println(obj);
		Employee emp = (Employee) obj;
		System.out.println("이름 : " + emp.getName());
		System.out.println("직위 : " + emp.getPosition());
		System.out.println("비번 : " + emp.getPassword());
		System.out.println("가족 : " + emp.getChild().getName());
		System.out.println("가족수 : " + emp.getChild().getCount());
		
		ois.close();
	}
}

'자바 > io' 카테고리의 다른 글

writer  (0) 2019.06.10
reader  (0) 2019.06.10
file  (0) 2019.06.10
bytestream  (0) 2019.06.10
bridge  (0) 2019.06.10
package demo.reader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderDemo1 {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		FileReader reader = new FileReader("c:/temp/data.csv");
		BufferedReader br = new BufferedReader(reader);
		
		/*
		br.readLine();
		br.readLine();
		br.readLine();
		
		// 15~17 이미 읽어서 null 뜸.
		String text1 = br.readLine();
		String text2 = br.readLine();
		String text3 = br.readLine();
		String text4 = br.readLine();
		
		System.out.println(text1);
		System.out.println(text2);
		System.out.println(text3);
		System.out.println(text4);
		*/
		
		String text = null;
		while ((text=br.readLine()) != null) {
			System.out.println(text);
		}
		
		br.close();
	}
}
package demo.reader;

import java.io.BufferedReader;
import java.io.FileReader;

public class BufferedReaderDemo2 {

	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new FileReader("c:/temp/data.csv"));

		int totalEventCount = 0;
		String text = null;
		while ((text=reader.readLine()) != null) {
			String[] items = text.split(",");
			
			String location = items[5].replace("\"", "");
			if (location.startsWith("서울특별시")) {
			// 발생건수
			int event = Integer.parseInt(items[6]);
			totalEventCount += event;
			}
		}
		System.out.println("서울지역 발생건수 : " + totalEventCount);
	}
}

'자바 > io' 카테고리의 다른 글

writer  (0) 2019.06.10
serialization  (0) 2019.06.10
file  (0) 2019.06.10
bytestream  (0) 2019.06.10
bridge  (0) 2019.06.10
package demo.file;

import java.io.File;
import java.util.Date;

public class FileDemo1 {

	public static void main(String[] args) {
		
		// 지정된 파일의 정보를 가지는 파일객체 생성하기
		File file = new File("c:/java_workspace/oop6/Account.java");
		
		// String getName()
		// 파일명 획득하기
		String filename = file.getName();
		System.out.println("파일명 : " + filename);
		
		// long length()
		// 파일 사이즈 획득하기
		long filesize = file.length();
		System.out.println("파일 사이즈 : " + filesize + "바이트");

		// String getPath()
		// 전체 경로 획득하기
		String path = file.getPath();
		System.out.println("전체 경로 : " + path);
		
		// String getParent()
		// 파일이 위치한 디렉토리 경로 획득하기
		String directoryPath = file.getParent();
		System.out.println("디렉토리 경로 : " + directoryPath);
		
		// boolean isFile()
		// 파일인지 여부를 반환한다. 파일인 경우 true를 반환
		boolean isFile = file.isFile();
		System.out.println("파일인가? " + isFile);
		
		// boolean isDirectory()
		// 디렉토리인지 여부를 반환한다. 디렉토리인 경우 true를 반환
		boolean isDirectory = file.isDirectory();
		System.out.println("디렉토리인가 ? " + isDirectory);
		
		// boolean isExist()
		// 존재하는지 여부를 반환한다.
		boolean exist = file.exists();
		System.out.println("존재하는가? " + exist);
		
		// long lastModified()
		// 파일의 최종 수정 일자를 유닉스 시간으로 반환한다.
		long time = file.lastModified();
		System.out.println("최종 수정 일자 : " + time);
		Date date = new Date(time);
		System.out.println("최종 수정 일자 : " + date);
		
	}
}

package demo.file;

import java.io.File;

public class FileDemo2 {

	public static void main(String[] args) throws Exception {
		
		File file = new File("c:/temp/sample.txt");

		// 새 파일 생성하기
		file.createNewFile();
		
	}
}
package demo.file;

import java.io.File;

public class FileDemo3 {

	public static void main(String[] args) {
		
		File dir = new File("c:/temp/source");
		dir.mkdir();
		
		File dirs = new File("c:/temp/resource/images/logo");
		dirs.mkdirs();
	}
}
package demo.file;

import java.io.File;

public class FileDemo4 {

	public static void main(String[] args) {
		
		// 파일의 전체 경로를 활용해서 파일 객체를 초기화
		File file1 = new File("c:/temp/sample.txt");
		
		// 디렉토리명과 파일명을 활용해서 파일객체를 초기화
		File file2 = new File("c:/temp", "sample.txt");

		// 디렉토리 정보를 가진 파일 객체와 파일명을 활용해서 파일 객체를 초기화
		File dir = new File("c:/temp");
		File file3 = new File(dir, "sample.txt");
	}
}
package demo.file;

import java.io.File;

public class FileDemo5 {

	public static void main(String[] args) {
		
		File file1 = new File("c:/temp/source/a.txt");
		file1.delete();
		
		// 폴더 안에 다른 파일이 있으면 폴더는 지워지지 않는다.
		File dir = new File("c:/temp/source");
		dir.delete();
		
	}
}

'자바 > io' 카테고리의 다른 글

writer  (0) 2019.06.10
serialization  (0) 2019.06.10
reader  (0) 2019.06.10
bytestream  (0) 2019.06.10
bridge  (0) 2019.06.10
package demo.bytestream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {

	public static void main(String[] args) throws IOException {

		// 지정된 파일을 읽어오는 FileInputStream 생성하기
		FileInputStream in = new FileInputStream("c:/temp/suyoung.jpg");
		
		// 지정된 파일명을 기록하는 FileOutputStream 생성하기
		FileOutputStream out = new FileOutputStream("c:/temp/copy_suyoung.jpg");
		
		int value = 0;
		// InputStream으로부터 1byte씩 읽어온다.
		while ((value = in.read()) != -1) {
			// OutputStream을 사용해서 읽어온 값을 기록한다.
			out.write(value);
		}
		in.close();
		out.close();
	}
}
package demo.bytestream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

public class FileCopy2 {

	public static void main(String[] args) throws Exception {
		InputStream in = new FileInputStream("c:/temp/suyoung.jpg");
		OutputStream out = new FileOutputStream("c:/temp/copycopy_suyoung.jpg");
		
		IOUtils.copy(in, out);
	}
}
package demo.bytestream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class ImageCrawler {

	public static void main(String[] args) throws Exception {
		long startTime = System.currentTimeMillis();
		
		// URL url = new URL("https://img3.daumcdn.net/thumb/R658x0.q70/?fname=https://t1.daumcdn.net/news/201903/26/newsen/20190326101733645qadi.jpg");
		
		// InputStream in = url.openStream();
		// FileOutputStream out = new FileOutputStream("C:/temp/lee.jpg");
		
		FileInputStream in = new FileInputStream("c:/temp/tomcat.exe");
		FileOutputStream out = new FileOutputStream("c:/temp/copy_tomcat.ext");
		
		// 배열에 저장되는 데이터의 개수를 저장하는 변수
		int len = 0;
		// intputStream으로 읽어들인 데이터를 저장하는 배열
		byte[] buf = new byte[1024*8];
		
		// int red(byte[] buf)는 inputStream으로 읽어들인 데이터를
		// 전달받은 배열에 저장하고, 최종적으로 저장된 개수를 반환한다.
		
		while ((len = in.read(buf)) != -1) {
			// void write(byte[] buf, int offset, int len)은 전달받은 배열에 저장된 데이터를
			// offset위치부터 len 개수만큼 기록한다.
			out.write(buf, 0, len);
			
		}
		
		long endTime = System.currentTimeMillis();
		
		//int value = 0;
		//while ((value = in.read()) != -1) {
		//	out.write(value);
		//}
		in.close();
		out.close();
		
	}
}
package demo.bytestream;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;

public class ImageCrawler2 {

	public static void main(String[] args) throws Exception {
		
		URL url = new URL("https://imgnews.pstatic.net/image/346/2019/03/26/0000025295_001_20190326072103336.jpg?type=w647");
		
		InputStream in = url.openStream();
		OutputStream out = new FileOutputStream("c:/temp/미니_소세지.jpeg");
		
		IOUtils.copy(in, out);
	}
}

'자바 > io' 카테고리의 다른 글

writer  (0) 2019.06.10
serialization  (0) 2019.06.10
reader  (0) 2019.06.10
file  (0) 2019.06.10
bridge  (0) 2019.06.10
package demo.bridge;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BridgeStreamDemo1 {

	public static void main(String[] args) throws Exception {
		
		System.out.println("이름을 입력하세요.");
		
		// 소스(키보드)와 연결된 InputStream
		// InputStream is = System.in;
		// 바이트스트림을 문자스트림으로 변환해주는 InputStreamReader
		// InputStreamReader isr = new InputStreamReader(is);
		// 다른 문자스트림과 연결해서 성능을 향상시키는 BufferedReader
		// BufferedReader br = new BufferedReader(isr);
		
		// 압축
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		
		String text = br.readLine();
		System.out.println(text);
		
		// 키보드와 연결된 스트림 사용하기
		// InputStream in = System.in;
		// 키보드와 연결된 스트림으로 1byte 읽어오기
		// int value = in.read();
		
		// System.out.println(value);
		
	}
}

'자바 > io' 카테고리의 다른 글

writer  (0) 2019.06.10
serialization  (0) 2019.06.10
reader  (0) 2019.06.10
file  (0) 2019.06.10
bytestream  (0) 2019.06.10

+ Recent posts