Java - 读取键盘输入并将其写入文本文件

问题描述:

有没有办法从命令行读取输入并将其写入文本文件?Java - 读取键盘输入并将其写入文本文件

public class Tester 
{ 
    public static void main(String[] args) 
     { 
      //Yes, I am clueless so I have no idea what to put 
     } 
} 
+0

读取用户输入(http://stackoverflow.com/questions/5287538/how-can-i-get-the -user-input-in-java),然后保存该文件(https://www.tutorialspoint.com/java/java_files_io.htm)。这就是它。 – AppleCrazy

+0

http://www.oracle.com/technetwork/java/javase/downloads/上的JDK演示和示例包在其demo/jfc/Notepad目录下有一个这样的例子。 – VGR

+0

对于这样的基本内容,请首先查看[Java Tutorials](http://docs.oracle.com/javase/tutorial/essential/io/)。 – MarsAtomic

因为我假设你是Java的初学者,我写了一个简短的代码示例,并带有不言自明的注释。应用程序在命令行上运行并逐行读取,其中一行通过按Enter键结束。如果您输入“EXIT”,则应用程序退出。要编写的文本文件位于C:\Folder\Text.txt

这是一个非常基本的例子,所以只要将它扩展到您的需求即可。为了获得更好的平台独立性,您可以用File.separator替代硬编码的\\斜线。作为finally块的注释:在较新版本的Java(> = 7)中,通过使用try-with语法,可以节省大量样板代码。阅读更多关于它here,如果你有兴趣。

您可以通过自己了解教程here中的基本I/O机制。

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

public class Tester { 

    public static void main(String[] args) { 

     // The reader and writer objects must be declared BEFORE 
     // the try block, otherwise they are not 'visible' to close 
     // in the finally block 
     Scanner reader = null; 
     FileWriter writer = null; 
     String inputText; 

     try { 

      // Reader and writer are instantiated within the try block, 
      // because they can already throw an IOException 
      reader = new Scanner(System.in); 

      // Do not forget, '\\' is the escape sequence for a backslash 
      writer = new FileWriter("C:\\Folder\\Text.txt"); 

      // We read line by line, a line ends with a newline character 
      // (in Java a '\n') and then we write it into the file 
      while (true) { 

       inputText = reader.nextLine(); 

       // If you type 'EXIT', the application quits 
       if (inputText.equals("EXIT")) { 
        break; 
       } 

       writer.write(inputText); 

       // Add the newline character, because it is cut off by 
       // the reader, when reading a whole line 
       writer.write("\n"); 

      } 

     } catch (IOException e) { 

      // This exception may occur while reading or writing a line 
      System.out.println("A fatal exception occurred!"); 

     } finally { 

      // The finally branch is ALWAYS executed after the try 
      // or potential catch block execution 

      if (reader != null) { 
       reader.close(); 
      } 

      try { 

       if (writer != null) { 
        writer.close(); 
       } 

      } catch (IOException e) { 

       // This second catch block is a clumsy notation we need in Java, 
       // because the 'close()' call can itself throw an IOException. 
       System.out.println("Closing was not successful."); 

      } 

     } 

    } 

} 

使用Java 8,这可能是非常简明:在

public static void main(String[] args) { 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    Stream<String> stream = in.lines().limit(20); // 20 is number of lines you want to read 

    try (PrintWriter pw = new PrintWriter("FileName.txt", "UTF-8")) { 
     stream.map(s -> s) 
       .forEachOrdered(pw::println); 
    } 
    catch (UnsupportedEncodingException | FileNotFoundException e) { 
     e.printStackTrace(); 
    }