Java Eclipse-将文件路径作为参数传递
这是来自一个类的代码的一部分Java Eclipse-将文件路径作为参数传递,java,eclipse,bufferedreader,Java,Eclipse,Bufferedreader,这是来自一个类的代码的一部分 public static Graph loadFromFile(String infile, boolean is_undirected) throws IOException { Graph g = new Graph(); BufferedReader br = new BufferedReader(new FileReader(infile)); String line; while ((line = br.readLine(
public static Graph loadFromFile(String infile, boolean is_undirected) throws IOException {
Graph g = new Graph();
BufferedReader br = new BufferedReader(new FileReader(infile));
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
int src = Integer.parseInt(tokens[0]);
int dst = Integer.parseInt(tokens[1]);
g.addEdge(src, dst);
if (is_undirected) {
g.addEdge(dst, src);
}
}
br.close();
return g;
}
这是主要功能,因此现在我想将一个名为xx.txt的txt文件分配给filereader,我该如何做。来自命令Promt 要运行Java程序,必须首先编译它。 要编译包,请从PadPack目录运行以下命令
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println("java Influencers INFILE [u|d]");
return;
}
Graph g = Graph.loadFromFile(args[0], args[1].equals("u"));
run(g);
}
在程序运行期间,可以将文件路径作为命令行参数传递
javac *.java -d .
例如,如果包名为a.b.c
,主类名为main
,则运行
java ${your_package_name}.${your_main_class_name} xx.txt u
来自Eclipse IDE
xx.txt u
谢谢您的回复,先生,但我使用的是JAVA eclipse,当我运行上面的代码时,输出是“JAVA影响者填充[u | d]”,这意味着我没有在主函数中将文件名传递给bufferedreader,我想知道如何将txt文件分配给主函数中的文件读取器。(顺便说一句,txt文件在eclipse项目默认包中)编辑了eclipse IDE的答案,将完整的文件路径c:\myproject\xx.txt作为文件路径传递给Arguments它的工作非常感谢。。还有…在主函数中传递参数还有其他方法吗?我的意思是我们不是通过在参数部分添加文本来传递参数,而是通过在主函数部分编译代码来传递参数?您只能通过上述方法将参数传递给主方法。如果不想硬编码程序中的文件路径,请尝试以编程方式从用户处读取。
java a.b.c.Main xx.txt u