[java]代码库
import java.io.*;
public class Chp17_3 {//作业三:输入路径和内容保存文件
Chp17_3(){
InputStreamReader fisr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(fisr);
try {
System.out.println("输入文件路径");
String wenjianlujing = br.readLine();
BufferedWriter bw = new BufferedWriter(new FileWriter(wenjianlujing));
String wenjianneirong;
System.out.println("请输入要保存到上述文件的内容(输入exit保存并退出)");
while((wenjianneirong = br.readLine())!=null) {
if(wenjianneirong.equals("exit")) {
break;
}
bw.write(wenjianneirong);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
System.out.println("文件已创建!路径:"+wenjianlujing);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Chp17_3 app = new Chp17_3();
}
}
[代码运行效果截图]
[源代码打包下载]