gudii9
asked on
eclipse argument
Hi,
I have a program that creates test.txt when i pass test.txt from command prompt as below
java WriteTextFile test.txt
how to run above program by passing test.txt. please advise
i tried run as run coniguration then gave program arguments as test.txt but no luck
please advise
I have a program that creates test.txt when i pass test.txt from command prompt as below
java WriteTextFile test.txt
how to run above program by passing test.txt. please advise
i tried run as run coniguration then gave program arguments as test.txt but no luck
please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you post the main method of your program?
ASKER
i will provide this soon
ASKER
https://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
how to pass below as command line argument in eclipse?
E:\\test\\filename.txt
?
please advise
package com.mkyong;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample1 {
private static final String FILENAME = "E:\\test\\filename.txt";
public static void main(String[] args) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String content = "This is the content to write into file\n";
fw = new FileWriter(FILENAME);
bw = new BufferedWriter(fw);
bw.write(content);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
how to pass below as command line argument in eclipse?
E:\\test\\filename.txt
?
please advise
Hi,
You need to change your code to first accept a command line argument and then need to ensure value passed from command line is not null. Later you can pass the variable as suggested above.
fw = new FileWriter(FILENAME);
Should become
fw = new FileWriter(args[0]);
Even better validate args[0] is not null. Thanks
You need to change your code to first accept a command line argument and then need to ensure value passed from command line is not null. Later you can pass the variable as suggested above.
fw = new FileWriter(FILENAME);
Should become
fw = new FileWriter(args[0]);
Even better validate args[0] is not null. Thanks
ASKER
as attached i passed program argument
still did not create. please advise
package com.mkyong;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample1 {
private static final String FILENAME = "E:\\test\\filename.txt";
public static void main(String[] args) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String content = "This is the content to write into file\n";
fw = new FileWriter(FILENAME);
bw = new BufferedWriter(fw);
bw.write(content);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
still did not create. please advise
Because you didn't change line 20 in the above code as I suggested above.
change this to
and then pass the parameter.
fw = new FileWriter(FILENAME);
change this to
fw = new FileWriter(args[0]);
and then pass the parameter.
ASKER
package com.gp.maven.calculator.calculator;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample1 {
//private static final String FILENAME = "C:\\test\\filename.txt";
public static void main(String[] args) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String content = "This is the content to write into000 file\n";
//fw = new FileWriter(FILENAME);
fw = new FileWriter(args[0]);
bw = new BufferedWriter(fw);
bw.write(content);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
i changed even then same issue?
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok
ASKER
from command prompt we never have to give complete path though
That is because the value of File name was hardcoded here:
private static final String FILENAME = "C:\\test\\filename.txt";
Check this basic tutorial, I think your program is fine, just that you are not running the configuration which you added param to.
http://theopentutorials.com/tutorials/java/cmd-line-args/how-to-pass-command-line-arguments-in-eclipse-ide/