1

I want to know how I can get data from a text file to a java program from the command line. I am using windows.

I used

Java myprogram < c:\inputfile.txt

it doesn't work, but when I used

Java myprogram good

it works. 'good' is the word that i used it as input

FYI: when I used

Java myprogram good > c:\outfile.txt

this is for writing output to a text file ..

I need to read from the text file "inputfile.txt" and write to "outputfile.txt"

i used this

Java myprogram "c:\\inputfile.txt" > "c:\\outputfile.txt"

but not working

This code that i used it

import edu.smu.tspell.wordnet.*;

public class myprogram{
public static void main (String [] args) {

System.setProperty("wordnet.database.dir", "C:\\Program Files (x86)\\WordNet\\2.1\\dict\\");
WordNetDatabase database = WordNetDatabase.getFileInstance();
      String result = "";
        NounSynset nounSynset;
        NounSynset[] hyponyms;
        Synset[] synsets = database.getSynsets(args[0]);

        for (int i = 0; i < synsets.length; i++) { //iteratre over all senses
            String[] wordForms = synsets[i].getWordForms();
            for (int j = 0; j < wordForms.length; j++) {
                System.out.println(wordForms[j]);
            }

} 

}
}
Dheya Majid
  • 383
  • 3
  • 6
  • 13

3 Answers3

2

If you need to read input data from file then you need to write code for reading text file in your Java class to read.

It appears that your code is just getting input from command line and your are treating that command line argument as data.

See this example on how to read inputs from a text file.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Pass the value in double quotes like this :

Java myprogram good > "c:\\inputfile.txt"
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

Below cmd will works only in unix flavor OS.

      java classfile > "/path/logfilename.log"

Below program will read the file and write to a different file ( You can do improvements as per your use case)

    public class ReadWriteFile {

        public static void main(String args[]) throws Exception {

        if (args.length == 0) {
            System.out.println("Enter the file name");
        } else {

            String fileName = args[0];

            BufferedReader input = new BufferedReader(new FileReader(fileName));

            String line = null;

            BufferedWriter writer = new BufferedWriter( new FileWriter( "output.txt"));

            try {
                while (( line = input.readLine()) != null){
                    System.out.println(line);
                    //or
                    writer.write(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(input != null) input.close();                
                if ( writer != null) writer.close( );
            }
        }
    }

}

For character by character reading refer the below how-do-i-read-input-character-by-character-in-java

Community
  • 1
  • 1
VKPRO
  • 159
  • 1
  • 3
  • 8