문제풀이/명품 자바 프로그래밍(개정4판)
명품 자바 프로그래밍(개정4판) 제 8장 실습문제
Hyeon-Uk
2020. 12. 7. 19:17
반응형
[1번]
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class p0801 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=null;
FileWriter fw=null;
Scanner sc=new Scanner(System.in);
System.out.println("전화번호 입력 프로그램입니다.");
String temp="";
try {
f=new File("temp/phone.txt");
fw=new FileWriter(f);
int c;
while(true) {
System.out.print("이름 전화번호 >> ");
temp=sc.nextLine();
if(temp.contentEquals("그만")) {
break;
}
fw.write(temp+"\r\n");
}
sc.close();
fw.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(f.getPath()+"에 저장하였습니다.");
}
}
[2번]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class p0802 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=null;
FileReader fr=null;
BufferedReader br=null;
int c;
try {
f=new File("temp/phone.txt");
fr=new FileReader(f);
System.out.println(f.getPath()+"를 출력합니다.");
/*
while((c=fr.read())!=-1) {
System.out.print((char)c);
}
fr.close();
*/
br=new BufferedReader(fr,30);
String temp="";
while(true) {
temp=br.readLine();
if(temp==null) {
break;
}
System.out.println(temp);
}
fr.close();
} catch (IOException e) {
// TODO: handle exception
}
}
}
[3번]
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class p0803 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=null;
FileReader fr=null;
try {
f=new File("c:\\windows\\system.ini");
fr=new FileReader(f);
int c;
char C;
while((c=fr.read())!=-1) {
C=(char)c;
if(C>='a'&&C<='z') {
C=Character.toUpperCase(C);
}
System.out.print(C);
}
fr.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
[4번]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class p0804 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=null;
BufferedReader bi=null;
try {
f=new File("c:\\windows\\system.ini");
bi=new BufferedReader(new FileReader(f));
String s="";
int i=1;
System.out.println(f.getPath()+" 파일을 읽어 출력합니다.");
while(true) {
s=bi.readLine();
if(s==null) {
break;
}
System.out.println("\t"+i+" : "+s);
i++;
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
[5번]
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class p0805 {
public static boolean com(FileInputStream f1,FileInputStream f2) throws IOException {
byte buf1[]=new byte[1024];
byte buf2[]=new byte[1024];
int l1=0,l2;
while(true) {
l1=f1.read(buf1,0,buf1.length);
l2=f2.read(buf2,0,buf2.length);
if(l1!=l2) {
return false;
}
if(l1==-1) {
break;
}
for(int i=0;i<l1;i++) {
if(buf1[i]!=buf2[i]) return false;
}
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream f1,f2;
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.");
System.out.print("첫번째 파일 이름을 입력하세요>>");
s1=sc.next();
System.out.print("두번째 파일 이름을 입력하세요>>");
s2=sc.next();
try {
f1=new FileInputStream(s1);
f2=new FileInputStream(s2);
if(!com(f1,f2)) {
System.out.println("파일이 서로 다릅니다.");
}
else {
System.out.println("파일이 같습니다.");
}
if(f1!=null) f1.close();
if(f2!=null) f2.close();
sc.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
반응형
[6번]
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class p0806 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream f1,f2;
FileOutputStream f3;
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.");
System.out.print("첫번째 파일 이름을 입력하세요>>");
s1=sc.next();
System.out.print("두번째 파일 이름을 입력하세요>>");
s2=sc.next();
try {
f1=new FileInputStream(s1);
f2=new FileInputStream(s2);
f3=new FileOutputStream(new File("appended.txt"));
byte buff[]=new byte[1024];
while(true) {
int n=f1.read(buff);
f3.write(buff,0,buff.length);
if(n<buff.length) break;
}
while(true) {
int n=f2.read(buff);
f3.write(buff,0,buff.length);
if(n<buff.length) break;
}
System.out.println("프로젝트 폴더 밑에 appended.txt 파일에 저장했습니다.");
f1.close();
f2.close();
f3.close();
sc.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
[7번]
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class p0807 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File a,b;
FileInputStream fin;
FileOutputStream fout;
long n,total;
try {
a=new File("temp/red.png");
b=new File("temp/copy.png");
System.out.println(a.getName()+"를 "+b.getName()+"로 복사합니다.");
System.out.println("10%마다 *를 출력합니다.");
fin=new FileInputStream(a);
fout=new FileOutputStream(b);
int c;
n=0;
total=a.length();
while((c=fin.read())!=-1) {
fout.write((byte)c);
n=(n+1);
if(n==total/10) {
System.out.print("*");
n=0;
}
}
fin.close();
fout.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
[8번]
import java.io.File;
public class p0808 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f;
File sub[];
try {
f=new File("c:\\");
sub=f.listFiles();
long max=-1;
int maxi=-1;
for(int i=0;i<sub.length;i++) {
if(max<sub[i].length()) {
max=sub[i].length();
maxi=i;
}
}
System.out.println("가장 큰 파일은 "+sub[maxi].getPath()+" "+sub[maxi].length()+"바이트");
} catch (Exception e) {
// TODO: handle exception
}
}
}
[9번]
import java.io.File;
import java.io.IOException;
public class p0809 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f;
File fs[];
try {
//c드라이브에 이미 TEMP폴더가 있어서 임시 폴더 만들어둠
f=new File("c:\\temp1\\");
fs=f.listFiles();
int cnt=0;
System.out.println(f.getPath()+"디렉토리의 .txt파일을 모두 삭제합니다.");
for(int i=0;i<fs.length;i++) {
String n=fs[i].getName();
int index=n.lastIndexOf(".txt");
if(index!=-1) {
System.out.println(fs[i].getName()+" 삭제");
fs[i].delete();
cnt++;
}
}
System.out.println("총 "+cnt+"개의 .txt파일을 삭제했습니다.");
}catch(Exception e) {
e.printStackTrace();
}
}
}
[10번]
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
public class p0810 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=null;
FileReader fr=null;
HashMap<String,String> phone;
Scanner sc;
try {
phone=new HashMap<String,String>();
f=new File("c:\\temp1\\phone.txt");
fr=new FileReader(f);
sc=new Scanner(fr);
while(sc.hasNext()) {
String n=sc.next();
String t=sc.next();
phone.put(n,t);
System.out.println("key:"+n+"value:"+phone.get(n));
}
System.out.println("총 "+phone.size()+"개의 정보를 읽었습니다.");
sc.close();
sc=new Scanner(System.in);
String temp="";
while(true) {
System.out.print("이름>>");
temp=sc.next();
System.out.println(temp);
if(temp.equals("그만")) {
break;
}
String tel=phone.get(temp);
if(tel==null) {
System.out.println("찾는 이름이 없습니다.");
}
else {
System.out.println(tel);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
[11번]
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.util.Vector;
public class p0811 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr=null;
Vector<String> v;
Scanner sc;
try {
fr=new FileReader(new File("words.txt"));
v=new Vector<String>();
sc=new Scanner(fr);
String word="";
while(sc.hasNext()) {
word=sc.next();
v.add(word);
}
System.out.println("프로젝트 폴더 밑의 words.txt 파일을 읽었습니다...");
sc.close();
sc=new Scanner(System.in);
while(true) {
boolean find=false;
System.out.print("단어>>");
word=sc.next();
if(word.equals("그만")) break;
for(int i=0;i<v.size();i++) {
String w=v.get(i);
if(w.length()<word.length()) continue;
String sub=w.substring(0,word.length());
if(sub.equals(word)){
System.out.println(w);
find=true;
}
}
if(!find) System.out.println("발견할 수 없음.");
}
System.out.println("종료합니다...");
sc.close();
fr.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
[12번]
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.util.Vector;
public class p0812 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr=null;
Vector<String> v;
Scanner fsc,sc;
try {
sc=new Scanner(System.in);
System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.");
System.out.print("대상 파일명 입력>> ");
String fileN=sc.next();
fr=new FileReader(new File(fileN));
v=new Vector<String>();
fsc=new Scanner(fr);
String line="";
while(fsc.hasNext()) {
line=fsc.nextLine();
v.add(line);
}
while(true) {
System.out.print("검색할 단어나 문장>> ");
String findN=sc.next();
boolean find=false;
if(findN.equals("그만")) break;
for(int i=0;i<v.size();i++) {
String temp=v.get(i);
if(temp.indexOf(findN)!=-1) {
find=true;
System.out.println(i+": "+temp);
}
}
if(!find) System.out.println("발견할 수 없음.");
}
System.out.println("프로그램을 종료합니다.");
sc.close();
fr.close();
fsc.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
[13번]
import java.io.File;
import java.util.Scanner;
public class p0813 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=null;
File fl[];
Scanner sc;
try {
sc=new Scanner(System.in);
f=new File("c:\\");
System.out.println("***** 파일 탐색기입니다. *****");
while(true) {
String nowD=f.getName();
System.out.println("["+nowD+"]");
fl=f.listFiles();
for(int i=0;i<fl.length;i++) {
String dir;
if(fl[i].isDirectory()) dir="dir";
else dir="file";
System.out.println(dir+"\t"+fl[i].length()+"바이트\t"+fl[i].getName());
}
System.out.print(">>");
String op=sc.next();
if(op.equals("그만")) break;
if(op.equals("..")) {
if(f.getParent()==null) continue;
else {
f=new File(f.getParent());
}
}
else {
if(new File(f,op).isDirectory()) {
f=new File(f,op);
}
else {
System.out.println("디렉토리가 아닙니다.");
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
[14번]
import java.io.File;
import java.util.Scanner;
import java.util.StringTokenizer;
public class p0814 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=null;
File fl[];
Scanner sc;
try {
sc=new Scanner(System.in);
f=new File("c:\\");
System.out.println("***** 파일 탐색기입니다. *****");
while(true) {
String nowD=f.getName();
System.out.println("["+nowD+"]");
fl=f.listFiles();
for(int i=0;i<fl.length;i++) {
String dir;
if(fl[i].isDirectory()) dir="dir";
else dir="file";
System.out.println(dir+"\t"+fl[i].length()+"바이트\t"+fl[i].getName());
}
System.out.print(">>");
String line=sc.nextLine();
StringTokenizer st=new StringTokenizer(line," ");
String op=st.nextToken();
if(op.equals("그만")) break;
if(op.equals("..")) {
if(f.getParent()==null) continue;
else {
f=new File(f.getParent());
}
}
else {
if(op.equals("rename")) {
String v1,v2;
try {
v1=st.nextToken();
v2=st.nextToken();
File on=new File(f,v1);
File rn=new File(f,v2);
if(rn.exists()) {
System.out.println("이미 존재하는 파일이나 디렉터리 이름입니다.");
}
else {
on.renameTo(rn);
}
}catch(Exception e) {
System.out.println("두개의 파일명이 주어지지 않았습니다.");
}
}
else if(op.equals("mkdir")) {
try {
String dname=st.nextToken();
File newD=new File(f,dname);
if(newD.exists()) System.out.println("이미 존재하는 파일이나 디렉토리입니다.");
else {
System.out.println(dname+" 디렉터리를 생성하였습니다.");
newD.mkdir();
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("생성할 디렉토리 명이 주어지지 않았습니다.");
}
}
else {
if(new File(f,op).isDirectory()) {
f=new File(f,op);
}
else {
System.out.println("디렉토리가 아닙니다.");
}
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
반응형