Assumming that program name is "XML.java",
I need to perform the followings for each input file in command line--without running the code for every test file:

java Xml < input1.txt
java Xml < input2.txt
So My Code IS:

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String fileName = bf.readLine();
File file = new File(fileName);
FileReader fr = new FileReader(file);


Question: but I can not redirect to file by <. So everytime I run "XML.java",
I must enter "javac.XML java", but this does not meet the requirement above.
How can I do?
Mind adding a few lines please???????????
Extremely appreciate for any sort of help. Thanks in advance!!!!!!!

6 Years
Discussion Span
Last Post by tedtdu

May I suggest you write a program to open and read a file, and the file name is the first String argument ( args[0]) for the main method :
public static void main(String args[]) {...}
so that each time your command should simply be :
java XML input1.txt


Thanks tong1:

Yes, we can snd text file name to main method. What if I send another text file name and continue the process without rerunning my application. Could it be possible for you illustrate it with a few lines of code?


You may write a program to open and read several files whose names are passed via the String args[] array as the arguments of the main methods. The first argument tells the program how many files to be proceesed, and the following arguments represent the names of every files. For example:
java XML 4 input1.txt input2.txt input3.txt input4.txt
provided the files are located under the same folder

Edited by tong1: n/a


It would be extremely helpful, to be provided
a few line codes


The following code is an example demonstrating what I am thinking about. I have tested positively. You may write code differently to read the file one by one. I hope you may understand me.

import java.io.*;// read several input files as indicated via the arguments of main method
class XML{
  public static void main(String args[]){
    int length;
    byte buf[]=new byte[1024];
    for (int i=0; i< args.length ;i++ )  // each loop read one file. 
      FileInputStream in;      
      in =new FileInputStream(args[i]);  // establish the input channel for read each file
      while((length=in.read(buf,0,1024))!=-1) // read one line of character
      System.out.println(new String(buf));    // print out the line of character on DOS
    }catch(IOException e){
      System.out.println("Error: "+e);

Under the same folder, for example, you may have several files to be read: 1.txt, 2.txt, 3.txt.
You may compile the program, and then run the program with the following command lines on DOS

javac XML.java
java XML 1.txt 2.txt 3.txt

The program thus prints the contents of all the files on DOS window accordingly.

Edited by tong1: n/a


Dear tong1

Thanks a lot for your kind and prompt help.
I do not know how to thank, it realy does great help.
Sincere thank and appreciation from me.

Thanks again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.