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

King11

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). 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();
  }
}
Elliott Frisch

You don't, the OS handles the IO redirection with java main < in.txt > out.txt. Instead you read from System.in and write to 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].

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

how can I read from an uploaded text file

분류에서Dev

How can I copy a text file into another?

분류에서Dev

How can I display text from a file automatically after powering up my computer, in text editor or terminal?

분류에서Dev

How can I get a part of text file by terminal

분류에서Dev

Save arp output in terminal to text file every minute using crontab

분류에서Dev

How to send output from one terminal to another without making any new pipe or file

분류에서Dev

How can I read a crash file from /var/crash

분류에서Dev

How can I read a crash file from /var/crash

분류에서Dev

How can I save the last command to a file?

분류에서Dev

How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

분류에서Dev

How to read in from a text file in C++?

분류에서Dev

How do I permanently enable terminal output to a file?

분류에서Dev

How to save the JS output to a file

분류에서Dev

How do i print a line from an imported text file in java if the inputted int is found in that text file?

분류에서Dev

How can I print output whilst detached from controlling terminal?

분류에서Dev

Shell script to filter date alone from a particular column of a .csv file and save the output in another csv file

분류에서Dev

I can't rename a file using the terminal if it came from a parameter

분류에서Dev

Extract integers from file and output to another file?

분류에서Dev

How can I call a batch file within another batch file?

분류에서Dev

Read a text file to an array Java

분류에서Dev

Read text from file and correct it (commas and dots)[Java]

분류에서Dev

How can I get a multiple page pdf output from php file?

분류에서Dev

How do I produce a tab separated file from a text file?

분류에서Dev

How can I post data to another file and render from it using DomPDF

분류에서Dev

How can i get from the List<string> of files each time another file?

분류에서Dev

How to read int,text and double from a file and store it into an arraylist?

분류에서Dev

How to read a text file from a url line by line

분류에서Dev

How could I search pattern of one file in to another and save the result of each pattern in new file

분류에서Dev

How to sum the output of two commands and save it to a file?

Related 관련 기사

  1. 1

    how can I read from an uploaded text file

  2. 2

    How can I copy a text file into another?

  3. 3

    How can I display text from a file automatically after powering up my computer, in text editor or terminal?

  4. 4

    How can I get a part of text file by terminal

  5. 5

    Save arp output in terminal to text file every minute using crontab

  6. 6

    How to send output from one terminal to another without making any new pipe or file

  7. 7

    How can I read a crash file from /var/crash

  8. 8

    How can I read a crash file from /var/crash

  9. 9

    How can I save the last command to a file?

  10. 10

    How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

  11. 11

    How to read in from a text file in C++?

  12. 12

    How do I permanently enable terminal output to a file?

  13. 13

    How to save the JS output to a file

  14. 14

    How do i print a line from an imported text file in java if the inputted int is found in that text file?

  15. 15

    How can I print output whilst detached from controlling terminal?

  16. 16

    Shell script to filter date alone from a particular column of a .csv file and save the output in another csv file

  17. 17

    I can't rename a file using the terminal if it came from a parameter

  18. 18

    Extract integers from file and output to another file?

  19. 19

    How can I call a batch file within another batch file?

  20. 20

    Read a text file to an array Java

  21. 21

    Read text from file and correct it (commas and dots)[Java]

  22. 22

    How can I get a multiple page pdf output from php file?

  23. 23

    How do I produce a tab separated file from a text file?

  24. 24

    How can I post data to another file and render from it using DomPDF

  25. 25

    How can i get from the List<string> of files each time another file?

  26. 26

    How to read int,text and double from a file and store it into an arraylist?

  27. 27

    How to read a text file from a url line by line

  28. 28

    How could I search pattern of one file in to another and save the result of each pattern in new file

  29. 29

    How to sum the output of two commands and save it to a file?

뜨겁다태그

보관