I want to write string array in .txt file. But after running my code, I am getting an empty file. Here is my code.
public void DataSave() throws IOException {
File fout = new File("Data.txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
String[] numberOfProperty = new String[3];
numberOfProperty[0] = "1";
numberOfProperty[1] = "3";
numberOfProperty[2] = "4";
for (int i = 0; i < numberOfProperty.length; i++) {
bw.write(numberOfProperty[i]);
bw.newLine();
}
}
What's wrong in my code I cannot understand.Compiler shows no error. Please Help. All answer would be appreciated.
public void DataSave() {
File fout = new File("Data.txt");
try (FileOutputStream fos = new FileOutputStream(fout); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));) {
String[] numberOfProperty = new String[3];
numberOfProperty[0] = "1";
numberOfProperty[1] = "3";
numberOfProperty[2] = "4";
for (String s : numberOfProperty) {
bw.write(s);
bw.newLine();
}
} catch (IOException ignored) {
}
}
You need to close the BufferedReader. A better solution is using try-with-resources, so you don't need to worry about closing.
You can have multiple resources in the try-with-resources separated by a ;.
Java 13+, you can use Path.of():
public void DataSave() {
File fout = new File("Data.txt");
try (FileOutputStream fos = new FileOutputStream(fout); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));) {
String[] numberOfProperty = new String[3];
numberOfProperty[0] = "1";
numberOfProperty[1] = "3";
numberOfProperty[2] = "4";
Files.write(Path.of("Data.txt"), Collections.singletonList(numberOfProperty));
} catch (IOException ignored) {
}
}
You can also write your array in one line as:
String[] numberOfProperty = {"1", "2", "3"};
First thing I realised is your method naming, try using camel casing. Instead of DataSave() try dataSave() for more information on Naming Conventions see this link: https://www.javatpoint.com/java-naming-conventions
Secondly when using java resources to read and or write to files be sure to close the resource when processing has finished. You can see more information on this here: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
See my example below with a try-with-resource Statement.
public void dataSave() {
File fout = new File("data.txt");
String[] numberOfProperty = new String[3];
numberOfProperty[0] = "1";
numberOfProperty[1] = "3";
numberOfProperty[2] = "4";
try (FileWriter fileWriter = new FileWriter(fout);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)){
for(String str: numberOfProperty)
{
bufferedWriter.write(str);
bufferedWriter.newLine();
}
} catch (FileNotFoundException e) {
System.out.println("Unable to open file, file not found.");
} catch (IOException e) {
System.out.println("Unable to write to file." + fout.getName());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With