本節将帶着讀者開發實際案例,複習File類和檔案流的相關基本知識,實作對檔案的建立和内容儲存等操作。
【本節目标】
通過閱讀本節内容,你将複習File類和檔案流的相關知識,結合工廠模式,編寫适當的接口和工具類實作對使用者輸入界面的一些優化,完成檔案的相關操作與内容儲存功能。
檔案儲存
從鍵盤輸入檔案的内容和要儲存的檔案名稱,然後根據輸入的名稱建立檔案,并将内容儲存到檔案中。
在本程式裡面隻要求開發者儲存的是檔案名稱而并沒有設定檔案路徑,那麼對于檔案路徑就應該在程式啟動之前就準備好。
1、 定義一個檔案操作的服務接口
public interface IFileService {
/**
* 定義檔案的儲存處理方法
*
* @return 儲存成功為true,儲存失敗傳回false
*/
public boolean save();
}
2、 在InputUtil類裡面追加有輸入字元串的處理方法
public static String getString(String prompt){
String str = null;
boolean flag = true;
while(flag) {
Scanner input = new Scanner(System.in);
System.out.println(prompt);
if(input.hasNext()) {
str = input.next().trim();
if(!" ".equals(str)) { //不是空字元串
flag = flag ; //結束循環
}else {
System.out.println("輸入的内容不允許為空!");
}
}else {
System.out.println("輸入的内容不允許為空!");
}
}
return str;
}
整體代碼:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class IOCaseDemo {
static { //項目啟動的時候該路徑應該首先建立
File file = new File(SAVE_DIR); //路徑,但是這個檔案目錄有可能不存在
if (!file.exists()) { //檔案目錄不存在
file.mkdirs(); //建立目錄
}
}
public static void main(String[] args) {
IFileService fileService= Factory.getInstance();
System.out.println(fileService.save());
}
}
class Factory{
private Factory(){}
public static IFileService getInstance(){
return new FileServiceImpl();
}
}
public interface IFileService {
public static final String SAVE_DIR = "D:" + File.separator + "mldndata" + File.separator;
/**
* 定義檔案的儲存處理方法
*
* @return 儲存成功為true,儲存失敗傳回false
*/
public boolean save();
}
public class FileServiceImpl implements IFileService {
private String name;
private String content;
public FileServiceImpl() {
this.name = InputUtil.getString("請輸入儲存檔案名稱:");
this.content = InputUtil.getString("請輸入儲存檔案的内容:");
}
@Override
public boolean save() {
File file = new File(IFileService.SAVE_DIR + this.name);
PrintWriter out = null;
try {
out = new PrintWriter(new FileOutputStream(file));
out.print(this.content);
} catch (FileNotFoundException e) {
return false;
} finally {
if (out != null) {
out.close();
}
}
return true;
}
}
public class InputUtil {
private InputUtil(){}
public static String getString(String prompt){
String str = null;
boolean flag = true;
while(flag) {
Scanner input = new Scanner(System.in);
System.out.println(prompt);
if(input.hasNext()) {
str = input.next().trim();
if(!" ".equals(str)) { //不是空字元串
flag = flag ; //結束循環
}else {
System.out.println("輸入的内容不允許為空!");
}
}else {
System.out.println("輸入的内容不允許為空!");
}
}
return str;
}
/**
* 實作鍵盤接收數字的操作
* @param prompt 提示資訊
* @return 一個可以使用的數字
*/
public static int getInt(String prompt){
int num = 0;
boolean flag = true;
while (flag){
Scanner input = new Scanner(System.in);
System.out.print(prompt); //列印提示資訊
if(input.hasNext("\\d+")){
num = Integer.parseInt(input.next("\\d+"));
flag = false ;
}else{
System.out.println("輸入的内容不是數字!");
}
}
return num ;
}
}
想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。
本内容視訊來源于
阿裡雲大學 下一篇:IO實戰篇:字元串逆序顯示 | 帶你學《Java語言進階特性》之七十四 更多Java面向對象程式設計文章檢視此處