Skip to content

Commit

Permalink
multicampus-day12
Browse files Browse the repository at this point in the history
  • Loading branch information
respect98 committed Nov 23, 2022
1 parent 0c6ccc7 commit 085b99f
Show file tree
Hide file tree
Showing 19 changed files with 630 additions and 0 deletions.
30 changes: 30 additions & 0 deletions day12/BufferedReaderPrintWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day12;
import java.io.*;
//파일을 읽되 라인단위로 읽어서 앞에 줄번호를 붙여 콘솔에 출력하자
//데이터소스: 파일(FileInputStream)==>InputStreamReader=>BufferedReader
// : 파일(FileReader) ==> BufferedReader
//데이터목적지: 콘솔(System.out) ==> PrintWriter
public class BufferedReaderPrintWriterTest {

public static void main(String[] args)
throws Exception
{
String fname="src/day12/BufferedReaderWriterTest.java";
BufferedReader br
=new BufferedReader(new FileReader(fname));
PrintWriter pw
=new PrintWriter(System.out, true);
//true를 주면 new line을 만났을 때 자동으로 flush해준다(auto flush)
String line="";
int cnt=0;
while((line=br.readLine())!=null) {
// pw.write(++cnt+": "+line+"\n");
// pw.flush();
pw.println(++cnt+": "+line);
//println()을 하면 true줄 경우 자동 플러시 된다.
}
pw.close();
br.close();
}

}
35 changes: 35 additions & 0 deletions day12/BufferedReaderWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package day12;
import java.io.*;
//키보드 입력 => System.in => InputStreamReader => BufferedReader
//라인단위로 입력받아보자
//콘솔출력 => System.out => OutputStreamWriter => BufferedWriter/PrintWriter
public class BufferedReaderWriterTest {

public static void main(String[] args)
throws Exception
{
BufferedReader br
=new BufferedReader(new InputStreamReader(System.in));
//BufferedReader는 생성자에 Reader계열의 객체를 받아들인다.
//따라서 1바이트기반 스트림과 연결해야 할 때는 브릿지 스트림이 필요하다.
//InputStreamReader는 1byte데이터를 2byte로 조합하여 읽음
//===> charset을 맞추는 기능도 있음

BufferedWriter bw
=new BufferedWriter(new OutputStreamWriter(System.out));

String line="";
bw.write("입력하세요=>\n");
bw.flush();
//안녕하세요? 엔터
while((line=br.readLine())!=null) {
bw.write(line+"\n");
bw.flush();

}
br.close();
bw.close();

}

}
19 changes: 19 additions & 0 deletions day12/FileInputStreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package day12;
import java.io.*;
/*파일을 읽어서 콘솔에 출력해보자
* 데이터 소스: 파일 => 노드스트림(FileInputStream)
* 데이터 목적지: 콘솔 => 노드스트림(System.out)
*
* */
public class FileInputStreamTest {

public static void main(String[] args)
throws IOException
{ String fname="src/day11/Account.java";
FileInputStream fis = new FileInputStream(fname);
//배열에 담아서 읽고 출력하세요
byte[] data=new byte[1024];//1kb
int n=0, count=0, total=0;
}

}
27 changes: 27 additions & 0 deletions day12/FileInputStreamTest2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package day12;
import java.io.*;
/*파일을 읽어서 콘솔에 출력해보자
* 데이터 소스: 파일 => 노드스트림(FileInputStream)
* 데이터 목적지: 콘솔 => 노드스트림(System.out)
*
* */
public class FileInputStreamTest2 {

public static void main(String[] args)
throws IOException
{ String fname="src/day11/Account.java";
FileInputStream fis = new FileInputStream(fname);
//배열에 담아서 읽고 출력하세요
byte[] data=new byte[1024];//1kb
int n=0, count=0,total=0;
while((n=fis.read(data))!=-1) {
System.out.write(data,0,n);
count++;
total+=n;
}
System.out.println("반복문 횟수: "+count+",총바이트수"+total );
fis.close();
System.out.close();
}

}
26 changes: 26 additions & 0 deletions day12/FileOutputStreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package day12;
/*[실습]키보드로 입력받은 내용을 파일(C:/MyJava/Workspace/Begin/result.txt)에 저장하는 기능
* -------------------------------------
* 데이터 소스 : 키보드===>노드스트림(System.in)
* 데이터목적지: 파일 ===>노드스트림(FileOutputStream)
* */
import java.io.*;
public class FileOutputStreamTest {

public static void main(String[] args) throws Exception
{
String fname="C:/MyJava/Workspace/Begin/result.txt";
//키보드 입력받아(배열에 담아서) FileOutputStream객체로 write()해보기
FileOutputStream fos = new FileOutputStream(fname);
int n=0, count=0, total=0;
byte[] data = new byte[100];
while((n=System.in.read(data))!=-1) {
fos.write(data, 0, n);
fos.flush();//스트림에 남아있는 데이터를 밀어내기 해줌
total+=n;
}
System.out.println(fname+"에 "+total+"bytes 씀");
fos.close();
}

}
36 changes: 36 additions & 0 deletions day12/FileReaderWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package day12;
/*파일 카피
* FileReader / FileWriter
* - 2byte 기반(char) 스트림
* - 노드 스트림(파일과 노드 연결함)
* */
import java.io.*;
import javax.swing.*;
public class FileReaderWriterTest {

public static void main(String[] args)
throws Exception
{
String file = JOptionPane.showInputDialog("읽을 파일명을 입력하세요");
//src/day12/ObjectInOut.java
if(file==null) return;

FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter("src/day12/copy4.txt", true);

File f = new File(file);//파일 객체
long fsize=f.length();//파일 크리를 반환
System.out.println("읽을 파일 크기: "+fsize+"bytes");

int n=0;
char [] data = new char[1000];
while((n=fr.read(data))!=-1) {
fw.write(data, 0, n);
fw.flush();
}
System.out.println("copy완료");
fw.close();
fr.close();
}

}
30 changes: 30 additions & 0 deletions day12/ImageCopy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day12;
import java.io.*;
/*bg.jpg를 읽어서 copy.jpg로 내보내자
* 데이터 소스: bg.jpg(파일) ===>FileInputStream
* 데이터 목적지 : copy.jpg(파일) ===>FilOutputStream
* 바이트 수 출력하기
* */
public class ImageCopy {

public static void main(String[] args)
throws Exception
{
String fname = "images/bg.jpg"; // 읽을 파일
String target = "copy.jpg";//목적 파일

FileInputStream fis=new FileInputStream(fname);
byte data[] = new byte[1000];
FileOutputStream fos = new FileOutputStream(target);
int n=0, count=0, total=0;
while((n=fis.read(data))!=-1) {
fos.write(data, 0, n);
fos.flush();
total+=n;
}
System.out.println(target+"에"+total+"바이트만큼 썼어요");
fos.close();

}

}
31 changes: 31 additions & 0 deletions day12/InputStreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package day12;
/*키보드 입력을 받아서 도스 콘솔에 출력해봅시다
* [1]데이터 소스: 키보드 =====> 노드 스트림(System.in)
* [2]데이터 목적지: 콘솔 =====> 노드 스트림(System.out)
* -System.in
* - 1byte 기반 스트림
* - 노드 스트림(키보드와 노드 연결)
* -InputStream타입
* int read()
* int read(byte[] data)
* */
import java.io.*;
public class InputStreamTest {

public static void main(String[] args) throws IOException{
int n =0;
int count=0;
System.out.println("입력하세요=>");
while(true) {
n = System.in.read(); //엔터 =>\r\n 13 10
count++;
System.out.println("n: "+n);
//Ctrl+C(윈도우) Ctrl+D(리눅스)를 누르면 -1을 반환함
if(n=='x'||n=='X') break;
}//while---------
System.out.println("********************************");
System.out.println(count+"bytes 읽어드림");
System.out.println("********************************");
}

}
26 changes: 26 additions & 0 deletions day12/InputStreamTest2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package day12;
import java.io.*;
//System.out : PrintStream
// - 1byte기반 스트림
// - 노드 스트림(콘솔하고 노드 연결)
// - print(), println(), printf()
// - write(int n)
// - write(byte[] data, int off, int len)
public class InputStreamTest2 {

public static void main(String[] args) throws IOException {
int n=0, count=0;
System.out.println("입력하세요=>");
while((n=System.in.read())!=-1) {//Ctrl+C를 누르기 전까지 계속 입력받아
// System.out.println((char)n);
System.out.write(n);
count++;
}
System.out.println("***************************");
System.out.println(count+"바이트 읽음");
System.out.println("***************************");
System.in.close();
System.out.close();
}

}
27 changes: 27 additions & 0 deletions day12/InputStreamTest3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package day12;
import java.io.*;
public class InputStreamTest3 {

public static void main(String[] args)
throws IOException{
//달걀판 만들기
byte[] data=new byte[6];
int n=0, count=0, total=0;
System.out.println("입력하세요=>");
while((n=System.in.read(data))!=-1);{
//입력받은 데이터는 data배열에 담긴다
//n=> 달갈판에 담은 달걀개수를 반환
// System.out.write(n); [x]
// System.out.write(data);==>쓰레기 값이 나올 수 있다.
System.out.write(data,0,n);//[0]
count++; //반복문 횟수
total+=n; //입력받은 바이트 수
}
System.out.println("*****************");
System.out.println(total+"bytes");
System.out.println("*****************");
System.in.close();
System.out.close();
}

}
41 changes: 41 additions & 0 deletions day12/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package day12;
//객체를 파일에 쓰거나, 네트워크로 원격에 전달하고자 하면 해당 객체는 직렬화가 구현되어 있어야 한다
//java.io.Serializable 인터페이스를 상속받아 구현한다.
//=>추상메서드는 없음 => 단순히 표시하는 기능(직렬화된 객체임을 표시)
public class Member implements java.io.Serializable{

private String name;
private String pwd;
private String tel;

public Member() {
this("아무개","","");
}
public Member(String n, String pwd, String tel) {
this.name=n;
this.pwd=pwd;
this.tel=tel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}

public String showInfo() {
return name+"\t"+tel+"\t"+pwd;
}
}//////////////////////////////////
48 changes: 48 additions & 0 deletions day12/ObjectInOut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package day12;
/*ObjectOutputStream
* : 자바의 다양한 자료형과 객체를 저장할 수 있는 스트림
* - 1byte기반 스트림
* - 필터 스트림
* */
import java.io.*;
import javax.swing.*;
import java.util.Date;
public class ObjectInOut {

public static void main(String[] args)
throws Exception
{
String fname="src/day12/obj2.txt";
//데이터 목적지(파일)==>FileOutputStream==>ObjectOutputStream
FileOutputStream fos = new FileOutputStream(fname);
ObjectOutputStream oos = new ObjectOutputStream(fos);
JFrame f = new JFrame("오브젝트 스트림");

oos.writeObject(f);//객체를 파일에 쓰기한다
oos.flush();

Date d=new Date();
oos.writeObject(d);
oos.flush();

Member m1 =new Member("홍길동","123","010-1111-2222");
Member m2 =new Member("김길동","234","010-2111-2222");
Member m3 =new Member("홍영희","345","010-3111-2222");

oos.writeObject(m1);
oos.flush();

oos.writeObject(m2);
oos.flush();

oos.writeObject(m3);
oos.flush();

System.out.println(fname+"에 저장 완료!!!");

oos.close();
fos.close();

}

}

0 comments on commit 085b99f

Please sign in to comment.