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]);
            }

} 

}
}

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks anubhava, please can you help me how can i used it in above java code
@DheyaMajid: Please see the link I provided.
Thanks, buy i want to read it word by word and put word to args[0]
As per your question you want to pass in the file name to your Java code and read file content from inside the Java code. Is my interpretation not correct?
Yes , that correct , i need to read the content word by word. because i want to used that word for another processing.
|
0

Pass the value in double quotes like this :

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

2 Comments

i used Java myprogram "c:\\inputfile.txt"> "c:\\outputfile.txt" in CMD but not working
where is the code to read the file? You need to write code to read the input file. Similarly for writing to a file.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.