Jeewantha Lahiru :
when I run this code I get error.Can you Show me the error?
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = "Hello world";
bw.write(s);
bw.newLine();
bw.close();
}
I see this code in more hackerrank problems and I need to know why we use this... this is the error message,
Exception in thread "main" java.lang.NullPointerException
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:226)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:124)
at java.base/java.io.FileWriter.<init>(FileWriter.java:66)
at com.company.Main.main(Main.java:8)
Trishul Singh Choudhary :
I guess you didn't give new File try this :
public static void main(String args[]) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:\\Users\\myName\\Desktop\\data.txt")));
String s = "Hello world";
bw.write(s);
bw.newLine();
bw.close();
}
}
This works fine for me and the txt file is generated as desired. Let me know if it helps.