IO
案例:创建文件在里面写东西
package com.zhsj.mybatis.test;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @program: test
* @description:
* @author: liulq
* @create: 2022-03-13 20:55
*/
public class Day2 {
public static void main(String[] args) throws IOException {
File targFile =createFile();
writerFile(targFile);
System.out.println("结束");
}
private static void writerFile(File targFile) throws IOException {
try (
FileOutputStream fileOutputStream = new FileOutputStream(targFile);
OutputStreamWriter f = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
PrintWriter pw = new PrintWriter(f);
){
while (true){
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine().trim();
System.out.println("输入的内容为"+line);
if (line.trim().isBlank()){
System.out.println("输入结束");
break;
}else {
pw.println(line);
pw.flush();
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
private static File createFile() throws IOException {
System.out.println("输出文件名");
Scanner scanner = new Scanner(System.in);
String fileName =scanner.nextLine().trim();
File f= new File("."+File.separator+fileName+".txt");
if (f.isFile()){
System.out.println("目录文件存在,删除"+f.delete());
}
System.out.println(f.createNewFile());
return f;
}
}