题目:
向一个文件中输入内容然后保存在固定的目录中。
实现:
创建路径类:
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(); // 传入要进行内容输入的文件路径
}
}
程序开始。
结果显示:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· DeepSeek “源神”启动!「GitHub 热点速览」
· 上周热点回顾(2.17-2.23)