I/O編程(一)

1.編寫java程序,輸入三個整數,並求出三個整數的最大值和最小值。``

package com.lxh.sixteenchapter;

import java.util.Scanner;

public class InputUtil {

	private InputUtil() {
		
	}
	private static final Scanner INPUT=new Scanner(System.in);
	public static int getInt(String prompt) {
		int num=0;
		boolean flag=true;
		while(flag) {
			System.out.println(prompt);  //打印提示信息
			if(INPUT.hasNext()) {
				flag=false;
				num=INPUT.nextInt();
			}else {
				System.out.println("輸入有誤");
			}
		}
		return num;
	}
	
	
}


package com.lxh.sixteenchapter;

public interface INumberSerService {
       public int [] stat(int count);
}

package com.lxh.sixteenchapter;

public class NumberServiceImpl implements INumberSerService {

	@Override
	public int[] stat(int count) {
		int result[]=new int[2];
		int data[]=new int[count];
		for(int x=0;x<data.length;x++) {
			data[x]=InputUtil.getInt("請輸入第"+(x+1)+"數字");
		}
		result[0]=data[0];   //最大值
		result[1]=data[0];   //最小值
		for(int x=0;x<data.length;x++) {
			if(data[x]>result[0]) {
				result[0]=data[x];
			}
			if(data[x]<result[0]) {
				result[1]=data[x];
			}
		}
		return result;
	}

}

package com.lxh.sixteenchapter;

public class Factory {
   private static INumberSerService getInstance() {
	   return new NumberServiceImpl();
   }


}

package com.lxh.sixteenchapter;

public class IOCaseDemo {
       public static void main(String[] args) {
    	   INumberService numberService = Factory.getInstance() ;
   		int result [] = numberService.stat(5) ;
   		System.out.println("最大值:" + result[0] + "、最小值:" + result[1]);
	}
}

2、 從鍵盤輸入文件的內容和要保存的文件名稱,然後根據輸入的名稱創建文件,並將內容保存到文件中。
(1),定義一個文件操作類的服務接口

public interface IFileService {
       public boolean save();
}

(2)在InputUtil類裏面追加有輸入字符串的處理方法

package com.lxh.sixteenchapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputUtil02 {
     private static final BufferedReader INPUT=new BufferedReader(new InputStreamReader(System.in));
     private InputUtil02() {}
 	public static String getString(String prompt) {
 		String str = null ;
 		boolean flag = true ;
 		while(flag) {
 			System.out.print(prompt);
 			try {
 				str = INPUT.readLine() ;
 				if (!"".equals(str)) {
 					flag = false ;
 				} else {
 					System.out.println("輸入的內容不允許爲空!");
 				}
 			} catch (IOException e) {
 				System.out.println("輸入的內容不允許爲空!");
 			}
 		}
 		return str ;
}
}

(3)定義文件操作的業務標準

import java.io.File;

public interface IFileServic02 {
       public static final String SAVE_DIR ="E:"+File.separator+"File"+File.separator+"ll.txt";
       public boolean save();
}

(4)定義實現子類

package com.lxh.sixteenchapter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class FileServiceImpl02 implements IFileServic02 {

	private String name;
	private String count;
	
	public FileServiceImpl02(String name, String count) {
		
		this.name = InputUtil02.getString("請輸入保存文件名稱");
		this.count = InputUtil02.getString("請輸入保存文件內容");
	}

	@Override
	public boolean save() {
		File file=new File(IFileServic02.SAVE_DIR+this.name);
		PrintWriter out=null;
		try {
			out=new PrintWriter(new FileOutputStream(file));
			out.print(this.count);
		} catch (FileNotFoundException e) {
			return false;
		}finally {
			if(out!=null) {
				out.close();
			}
		}
		return true;
	}

}

(5)建立工廠類

public class Factory02 {
       public static IFileServic02 getInstance() {
    	   return new FileServiceImpl02();
       }
}

(6)測試


public class Test02 {
       public static void main(String[] args) {
		IFileServic02 test02=Factory02.getInstance();
		System.out.println(test02.save());
	}
}

執行結果

請輸入保存文件名稱1232564154841
請輸入保存文件內容51651
true

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章