来自文本文件的 Java 输入参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15847200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java input parameter from text file
提问by Dheya Majid
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.
我想知道如何从命令行将文本文件中的数据获取到 java 程序。我正在使用窗户。
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"
我需要从文本文件“inputfile.txt”中读取并写入“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]);
}
}
}
}
回答by anubhava
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.
如果您需要从文件中读取输入数据,那么您需要在 Java 类中编写用于读取文本文件的代码以进行读取。
It appears that your code is just getting input from command line and your are treating that command line argument
as data.
看来您的代码只是从命令行获取输入,而您将其command line argument
视为数据。
回答by Ankur Shanbhag
Pass the value in double quotes like this :
在双引号中传递值,如下所示:
Java myprogram good > "c:\inputfile.txt"
回答by VKPRO
Below cmd will works only in unix flavor OS.
以下 cmd 仅适用于 unix 风格的操作系统。
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
对于逐字符阅读,请参阅下面的 how-do-i-read-input-character-by-character-in-java