반응형

문제

File 클래스를 이용하여 C:\에 있는 파일 중에서 제일 큰 파일의 이름과 크기를 출력하라.

 

출력 예시

가장 큰 파일은 C:\hiberfil.sys 3394002944바이트

 

소스 코드

import java.io.File;

public class Main8 {
	public static void MaxByteFile(File dir) {
		File[] subFiles = dir.listFiles();
		int Max = 0;
		File MAX = null;
		for(int i = 0;i<subFiles.length;i++)
		{
			File temp = subFiles[i];
			if(Max < temp.length()) {
				Max = (int)temp.length();
				MAX = temp;
			}			
		}
		System.out.println("가장 큰 파일은"+MAX.getPath()+" "+ Max + "바이트");
	}
	public static void main(String[] args) {
		File f = new File("C:\\");
		MaxByteFile(f);
	}

}
반응형
반응형

문제

파일 복사 연습을 해보자. 이미지 복사가 진행하는 동안 10% 진행할 때마다 '*' 하나씩 출력하도록 하라. 아래 예시는 프로젝트 폴더 밑에 a.jpg을 미리 준비해두었다.

 

출력 예시

a.jpg를 b.jpg로 복사합니다.
10%마다 *를 출력합니다.
**********

 

소스 코드

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

public class Main7 {

	public static void main(String[] args) {
		File src = new File("파일경로\\a.jfif");
		File dest = new File("파일경로\\b.jfif");
		int c;
		int count = 0,div = 1;
		try {
			FileInputStream fi = new FileInputStream(src);
			FileOutputStream fo = new FileOutputStream(dest);
			System.out.println(src.getPath() + "를 "+ dest.getPath()+"로 복사합니다");
			System.out.println("10%마다 *를 출력합니다");
			while((c=fi.read())!=-1) {
				fo.write((byte)c);
				count++;
				if(((src.length())/10*div ) == count ) {
					System.out.print("*");
					div++;
				}
			}
			fi.close();
			fo.close();
			
		}catch(IOException e) {
			System.out.println("파일 복사 오류");
		}
	}

}

jpg가 아닌 jfif그림파일을 사용했습니다.

 

a.jfif
0.02MB
b.jfif
0.02MB

반응형
반응형

문제

사용자로부터 두 개의 텍스트 파일 이름을 입력받고 첫 번째 파일 뒤에 두 번째 파일을 덧붙인 새로운 파일을 생성하는 프로그램을 작성하라. 아래 실행 예시에서는 프로젝트 폴더에 elvis1.txt와 elvis2.txt를 미리 준비해 두었다.

 

출력 예시

전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.
첫번째 파일 이름을 입력하세요>>elvis1.txt
두번째 파일 이름을 입력하세요>>elvis2.txt
프로젝트 폴더 밑에 appended.txt. 파일에 저장하였습니다.

 

소스 코드

import java.io.*;
import java.util.Scanner;

public class Main6 {
	public static void main(String[] args) {
		FileReader fin1 = null;
		FileReader fin2 = null;
		FileWriter fin3 = null;
		BufferedReader bufReader1 = null,bufReader2 = null;
		String Line1 = "",Line2 = "";
		String File1,File2;
		Scanner s = new Scanner(System.in);
		
		System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.");
		try {
			System.out.print("첫번째 파일 이름을 입력하세요>>");
			File1 = s.nextLine();
			System.out.print("두번째 파일 이름을 입력하세요>>");
			File2 = s.nextLine();
			fin1 = new FileReader("파일경로"+File1);
			fin2 = new FileReader("파일경로"+File2);
			fin3 = new FileWriter("파일경로\\appended.txt");
			bufReader1 = new BufferedReader(fin1);
			bufReader2 = new BufferedReader(fin2);
			while((Line1 = bufReader1.readLine())!=null) {
				fin3.write(Line1,0,Line1.length());
				fin3.write("\r\n", 0, 2);	
			}
			while((Line2 = bufReader2.readLine())!=null) {
				fin3.write(Line2,0,Line2.length());
				fin3.write("\r\n", 0, 2);	
			}
			
			System.out.println("프로젝트 폴더 밑에 appended.txt 파일에 저장하였습니다.");
			fin1.close();
			fin2.close();
			fin3.close();
			bufReader1.close();
			bufReader2.close();
		}
		catch(IOException e) {
			System.out.println("입출력 오류");
		}
		s.close();
	}
}

elvis1.txt
0.00MB
elvis2.txt
0.00MB
appended.txt
0.00MB

반응형

+ Recent posts