본문 바로가기
대학교/3.컴퓨터네트워크응용및실습

자바 스트림 실습 문제

by Jcoder 2018. 9. 9.


1. 커맨드 라인으로부터 파일 이름들을 입력 받아 System.out 으로 출력하는 클래스를 작성하시오. 화면에 출력 시 파일 이름과 함께 내용을 출력하고 각 파일의 출력 내용 사이에 적당한 기호로 구분하시오. 파일이름은 편의상 file1.txt, file2.txt, ,,,로 하시오.


package chapter1;
import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ex01_1
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
while(true)
{
try
{
System.out.println("종료를 원하시면 ctrl + z 를 입력하세요.");
System.out.print("파일 이름을 입력하세요 : ");
String name = scan.next();//파일 이름 입력받기
FileInputStream in = null;
try
{
System.out.println("파일 이름 " + name);
System.out.println("파일 내용");
int i = 0;
in = new FileInputStream(name); // 받은 입력파일 열기
while((i = in.read()) != -1)
System.out.write(i); //화면에 출력
System.out.println();
}
catch(FileNotFoundException e)
{
System.out.println("파일 이름을 정확히 입력해주세요."); // 존재하지 않는 파일 오류메세지
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(in != null)
in.close(); //파일 닫기
}
catch(IOException io)
{
}
}
}
catch(NoSuchElementException e) //ctrl + z시 종료문구
{
System.out.println("종료되었습니다.");
break;
}
}
}
}


2. 커맨드 라인으로부터 2개의 파일 이름을 입력 받아 첫 번째 파일의 내용을 두 번째 파일로 복사하는 클래스를 작성하시오. , 복사하는 메소드는 예제 1.8'copy' 메소드를 호출하여 이용하시오.




package chapter1;
import java.io.*;
import java.util.Scanner;
public class ex01_2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
try
{
String name1 = scan.next(); //복사할 파일 이름
String name2 = scan.next(); //복사한 파일을 저장할 이름
copy(name1, name2);
System.out.println("복사하셨습니다.");
}
catch(IOException e)
{
System.err.println("스트림으로부터 데이터를 읽을 수 없습니다.");
}
}
public static void copy(String n1, String n2) throws IOException
{
int byteRead;
byte[] buffer = new byte[256];
FileInputStream in = new FileInputStream(n1);
FileOutputStream out = new FileOutputStream(n2);
synchronized(in)
{
while((byteRead = in.read(buffer)) >= 0)
out.write(buffer, 0, byteRead);
}
}
}

3. GUI 기반으로 그림과 같이 파일을 복사하고, 파일 내용을 TextArea에 출력하는 클래스를 작성하시오.



package chapter1;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ex01_3 extends Frame implements ActionListener
{
Label lfile, ldata;
TextField tfile, tdata;
TextArea txarea;
Button cpy, prt,exit;
String filename1, filename2;
byte buffer[] = new byte[80];
public ex01_3(String str)
{
super(str);
this.setSize(300, 500);
this.setLayout(new FlowLayout());
lfile = new Label("입력파일");
add(lfile);
tfile = new TextField(20);
add(tfile);
cpy = new Button("복사"); // 복사
cpy.addActionListener(this);
add(cpy);
ldata = new Label("출력파일");
add(ldata);
tdata = new TextField(20);
add(tdata);
prt = new Button("출력"); //출력
prt.addActionListener(this);
add(prt);
txarea = new TextArea(10, 35);
add(txarea);
addWindowListener(new WinListener());
exit = new Button("닫기"); //닫기
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
add(exit);
addWindowListener(new WinListener());
}
public static void main(String[] args)
{
ex01_3 text = new ex01_3("파일 복사/출력");
text.setSize(300,300); //고정된 창 크기
text.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
filename1 = tfile.getText(); //복사할 파일 이름
filename2 = tdata.getText(); //복사한 파일 이름
FileInputStream in = null;
FileOutputStream out = null;
try
{
int byteRead;
byte[] buffer = new byte[256];
if(ae.getSource() == cpy) //복사
{
in = new FileInputStream(filename1);
out = new FileOutputStream(filename2);
while((byteRead = in.read(buffer)) >= 0)
out.write(buffer, 0, byteRead);
txarea.setText("복사하셨습니다."); //textArea에 복사 성공 출력
}
if(ae.getSource() == prt) //출력
{
in = new FileInputStream(filename2); //두 번째 파일 읽어오기
in.read(buffer);
String data = new String(buffer);
txarea.setText("복사한 파일 내용\n"+data+"\n"); //복사한 한 파일 내용 출력
}
}
catch(FileNotFoundException e) // 파일을 찾을 수 없을 때 오류메세지
{
txarea.setText("파일을 찾을 수 없습니다");
}
catch(IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
if (in != null) in.close();
if (out != null) out.close();
}catch(IOException e) {}
}
}
class WinListener extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
}





'대학교 > 3.컴퓨터네트워크응용및실습' 카테고리의 다른 글

[7,8장] 서버를 다중처리 가능하도록 수정  (0) 2019.01.03
[5,6장]  (0) 2019.01.03
실습문제 4  (0) 2019.01.03
실습문제3  (0) 2018.10.06
자바 AWT 계산기, COPY() 오버라이딩  (0) 2018.10.06