简体   繁体   English

如何使用Java创建的“使用键盘和PrintWriter类”写入txt文件

[英]How to write to a txt file “using the keyboard and PrintWriter Class ” created in java

I created a method for writing to a file but the method could not execute as I called it in the main method. 我创建了一种用于写入文件的方法,但是该方法无法执行,因为我在main方法中调用了它。 I also want to find what to know if I can write to the file via keyboard instead of using the PrintWriter resource? 我还想知道我是否可以通过键盘而不是使用PrintWriter资源写入文件?

import java.io.*;
import java.util.*;
/**
 *
 * @author toshiba
 */
public  class EXERISESONFILEWRITINGANDREADING {
        File file;

    public static void main(String[] args) throws Exception{
        EXERISESONFILEWRITINGANDREADING obj = new   EXERISESONFILEWRITINGANDREADING();
        obj.Create("D:\\document\\work.txt");
        obj.Write();
        obj.Read();
        }

    public  void Create(String name){ //name implies the directory of your  folder/file.
         file = new File(name); 
    }

    public void Write() throws Exception{
        PrintWriter WRITE = new PrintWriter(file);
        WRITE.print("this is my abode");
        WRITE.print("\nthis is my apartment");
        WRITE.print("\nthis is my Private Jet");
    }
    public void Read() throws Exception{
        Scanner input = new Scanner(file);
        while(input.hasNext()){
            System.out.println(input.next());
        }
    }

Your code is not executing because, you are not flushing your PrintWriter resource in your write method. 您的代码未执行,因为您没有在write方法中刷新PrintWriter资源。 Your write method will write to a given file you change it to this: 您的write方法将写入给定的文件,将其更改为:

public void write() throws Exception {
    PrintWriter writer = new PrintWriter(file);
    writer.println("this is my abode");
    writer.println("this is my apartment");
    writer.println("this is my Private Jet");

    writer.flush();
    writer.close();
}

Now, if you want to accept inputs from keyboard and then write it to file, you can make use of Scanner . 现在,如果要接受键盘输入并将其写入文件,可以使用Scanner

Create a Scanner with System.in as InputStream : 使用System.in作为InputStream创建一个Scanner

Scanner sc = new Scanner(System.in);

Create or use your same PrintWriter resource: 创建或使用相同的PrintWriter资源:

PrintWriter writer = new PrintWriter(file);

Start accepting the inputs from the keyboard until the user gives in a blank line - "" and write it to your file. 开始接受来自键盘的输入,直到用户输入空白行“”并将其写入您的文件。

String in = "";
while ( !(in = sc.nextLine()).equals("") ) {
    writer.println(in);
}

And finally, the entire method should look like: 最后,整个方法应如下所示:

public void writeToFileFromKeyboard() throws FileNotFoundException {
    Scanner sc = new Scanner(System.in);
    PrintWriter writer = new PrintWriter(file);

    String in = "";
    while ( !(in = sc.nextLine()).equals("") ) {
        writer.println(in);
    }

    sc.close();
    writer.flush();
    writer.close();
}

Note: This is just a suggestion!! 注意: 这只是一个建议!

If you are using Java-8 , you can modify your read() method to make use of Java-8 Streams, so that your method becomes more declarative in nature . 如果您使用的是Java-8 ,则可以修改read()方法以使用Java-8流,从而使您的方法本质上更具声明性 Hence, the modified read() method looks like: 因此,修改后的read()方法如下所示:

public void read() throws Exception {
    Files.lines( Paths.get(filePath) )
         .forEach( System.out::println );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM