简体   繁体   English

如何从终端读取文本文件并将输出保存到Java中的另一个文件?

[英]How can I read a text file from the terminal and save the output to another file in java?

Hey all I'm fairly new to this but I want to not hardcode a file to be read in but I want to read it in from the terminal/command prompt. 嘿,我对此还很陌生,但是我不想对要读取的文件进行硬编码,但是我想从终端/命令提示符中读取它。 Here is what I have so far, I'm hardcoding the filename in bufferedWriter but how can I make it to where I can do a command such as (java main < in.txt > out.txt). 到目前为止,这是我要的东西,我正在bufferedWriter中对文件名进行硬编码,但是如何将其命名为可以执行命令的地方,例如(java main <in.txt> out.txt)。 Thanks in advance. 提前致谢。

public static void main(String[] args) {

    String inFile = "in.txt";
    String outFile = "out.txt";

    if (args.length > 1) {
        inFile = args[0];
        outFile = args[1];
    }

    Lexer lexer = new Lexer(inFile);

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));

        Token t;

        while ((t = lexer.nextToken()) != null) {
            writer.write(t.toString());
            writer.newLine();
        }

        writer.close(); 

        System.out.println("Done tokenizing file: " + inFile);
        System.out.println("Output written in file: " + outFile);
    } catch (IOException e) {
        e.printStackTrace();
  }
}

You don't, the OS handles the IO redirection with java main < in.txt > out.txt . 不这样做,操作系统处理与IO重定向java main < in.txt > out.txt Instead you read from System.in and write to System.out . 相反,您从System.in读取并写入System.out Alternatively , based on the code you posted you might run it with 或者 ,根据您发布的代码,您可以使用

java main in.txt out.txt

Then your program would receive "in.txt" as args[0] and "out.txt" as args[1] . 然后您的程序将以args[0]形式接收"in.txt" ,以args[1]形式接收"in.txt" "out.txt"

Read from System.in and write to System.out - also remember anything you write to System.out will be written to the file. System.in读取并写入System.out还记得您写入System.out都会写入该文件。 such as Done tokenizing file: + inFile 例如Done tokenizing file: + inFile

暂无
暂无

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

相关问题 如何将Java所有行从java控制台输出保存到文本文件并正确附加? - How can I save java all lines from java console output to a text file and properly append it? Java:从现有的文本文件中读取整数并输出到另一个文件 - Java: Read integers from an existing text file and output to another file 如何从Java网站读取.dat文件中的文本? - How can I read text in a .dat file from the website in java? 如何使用Java从互联网读取文本文件? - How can I read a text file from the internet with Java? Java:如何从文件中读取文本和数字 - Java: How can I read text and numbers from a file 如何从 Java 代码中的终端(Ubuntu)输出中读取(输出永不结束/退出) - How can I read from terminal (Ubuntu) output in Java code (output never ends / quit) (Java)如何读取可以使用各种编码的文本文件并将内容输出到看起来正常的文本文件中? - (Java) How can I read in a text file that could use various encodings and output the contents in a text file that looks normal? Q re Java 程序从文本文件读取,创建数组列表,重新格式化并输出到另一个文件 - Q re Java Programme to read from a text file, create an arraylist, reformat, and output to another file Java,我如何自动将打印内容保存到文本文件 - Java, How can I Automatically Save Prints into a Text File 从一个文件读取,修改并输出到Java中的另一个文件 - read from one file, modify and output to another file in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM