新手,写的一般,LZ凑合看吧- import java.io.*;
- /**
- * 从键盘输入一段文字 例如 爱我中华 保存到F盘的line.txt文档中 求代码 用流实现
- *
- * @author DevLover
- */
- public class WriteToFile
- {
- public static void main(String[] args)
- {
- File file = new File("F:\\line.txt");
- try
- {
- file.createNewFile();
- }
- catch (IOException e)
- {
- throw new RuntimeException("文件创建失败!");
- }
- writeToFile(file);
- }
- public static void writeToFile(File file)
- {
- BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
- String str = "";
- try
- {
- str = bufr.readLine();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- PrintWriter pw = null;
- try
- {
- pw = new PrintWriter(new FileWriter(file));
- pw.write(str);
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- finally
- {
- pw.close();
- }
- }
- }
复制代码 |