How do I save values stored in an array as a text file in Java?

Asked 2 years ago, Updated 2 years ago, 47 views

You want to save strings stored in an array as a text file. I don't know what to do.

java

2022-09-22 21:42

1 Answers

String[] texts = ...
//FileOutoutStream fos = newFileOutputStream ("file path to save");
// If you want to save bytes

// With Java 8's autoclose:
try(FileWiterfout=newFileWriter("file path to save") {
    PrintWriter out = new PrintWriter(fout);
    for( String line:texts ) {
        out.println(line);
    }
}

Shouldn't we do it like above?

Study the similar classes above. Depending on the type of use, there are various classes and methods.


2022-09-22 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.