随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

题目:

向一个文件中输入内容然后保存在固定的目录中。

实现:

创建路径类:

 

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class FileCreate {   // 创建目录和文件
    public File createPath() {
        Scanner sc = new Scanner(System.in);
        String fileParent = "D:" + File.separator + "Demo2" + File.separator;
        System.out.print("请输入保存的文件名称:");
        String child = sc.next() + ".txt";
        String fileName = fileParent + child;
        File file = new File(fileName);
        if (file.getParentFile().exists()) {    // 如果文件父路径存在
                return file;
        } else if (!file.getParentFile().exists()) {    // 如果父路径不存在
            file.getParentFile().mkdirs();  // 创建父路径
            return file;
        } else {
            System.out.println("输入的路径有误!请重新输入!");
            createPath();
        }
        return null;
    }
}

 

定义一个接口来标准化输入内容:

public interface IWrite {
    public void writeString();
}

 

 

这里没有创建文件是因为下面OutputStream输出内容的时候会自动创建文件。

 

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class FileUtil implements IWrite{
    private FileCreate fileName;

    public FileUtil(FileCreate fileName) {
        this.fileName = fileName;
    }
    @Override
    public void writeString() {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));        // 个人觉得键盘输入最好是使用BufferedReader, Scanner不接收空格和空
        boolean flag = true;
        boolean changeH = false;
        OutputStream output = null;
        try {
            output = new FileOutputStream(this.fileName.createPath(), true);  // 创建输出流,为追加模式
            while (flag) {  // 如果想继续输入则为真,反之为假
                System.out.println("请在下方输入想要输入的内容:");
                String writeWord = "";
                if (changeH) {
                    try {
                        writeWord += "\n" + input.readLine();   // 接收输入的内容
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        writeWord += input.readLine();   // 接收输入的内容
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    output.write(writeWord.getBytes(StandardCharsets.UTF_8));   // 写入输入的内容
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("是否继续输入?\n继续输入:1\t结束输入:2");
                flag = reWrite();   // 继续输入返回真,反之为假
                if (flag) { // 如果继续输入再判断是否换行输入
                    System.out.println("是否换行输入?\n换行输入:1\t不换输入:2");
                    changeH = changeH();    // 换行返回真,反之为假
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private boolean changeH() {
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        if (choice == 1) {
            return true;    // 换行
        } else if (choice == 2) {
            return false;   // 不换行
        } else {
            System.out.println("错误!请输入1或2:");
            changeH();
        }
        return false;
    }

    private boolean reWrite() {    // 判断是否继续内容输入
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        if (choice == 1) {
            return true;
        } else if (choice == 2) {
            return false;
        } else {
            System.out.println("错误!请输入1或2:");
            reWrite();
        }
        return false;
    }
}

 

 

 

上面进行输出流的内容的输入操作,键盘的操作最好使用BufferedReader进行,因为它支持空格的输入。

 

public class Factory {  // 工厂模式获取对象
    private Factory(){}
    public static IWrite
getInstance(){ return new FileUtil(new FileCreate()); } }

 

 

 

获取对象不应该在客户端进行,所以创建一个工厂类进行对象的获取。

 

public class MAIN {
    public static void main(String[] args) {
        Factory.getInstance().writeString();   // 传入要进行内容输入的文件路径
    }
}

 

 

 

程序开始。

结果显示:

 

 

 

 

 

 

posted on 2022-02-23 15:02  时间完全不够用啊  阅读(834)  评论(0编辑  收藏  举报