Reputation: 118
I would like to save an ArrayList's content to file (the user should choose the .txt's location) but I am not sure how to do that since that code does not work properly.
Do you have any idea how to do that?
package vizsgaquiz;
import java.io.File;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class VizsgaQuiz extends Application {
ArrayList<String> kerdeslista = new ArrayList<String>();
String a ="a";
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Quiz Játék");
stage.show();
save();
}
public void save(){
kerdeslista.add(a);
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(stage);
if(file != null){
SaveFile(kerdeslista, file);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 0
Views: 2877
Reputation: 609
Here is an example how to save a new file to specified directory and name of file from FileDialog , Strings taken from a vector of Strings.It works for me !
public static void SaveFileTo(Vector<String> myLines) {
FileOutputStream f = null;
DataOutputStream h = null;
FileDialog d = new FileDialog(new JFrame(), "Save", FileDialog.SAVE);
d.setVisible(true);
String dir;
dir = d.getDirectory();
File oneFile = new File(dir + d.getFile());
try {
oneFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
f = new FileOutputStream(oneFile);
h = new DataOutputStream(f);
for (String de : myLines) {
h.writeBytes(de);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
h.close();
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 53
There are several issues that may be causing this problem. For starters, this code wouldn't compile. This is because you call the variable stage in the method save which "died" in the method start. To call stage in save, you either need to pass it to save or save it as a field. The second issue is that the method SaveFile doesn't seem to exist. An example of SaveFile might look something like the code included below. Please note that I updated the method save to take in a Stage and I changed the name of the method SaveFile to saveFile to match Java naming conventions. Also, the code below prints each value of the arraylist on a new line, which you may not want.
package vizsgaquiz;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class VizsgaQuiz extends Application {
ArrayList<String> kerdeslista = new ArrayList<String>();
String a ="a";
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Quiz Játék");
stage.show();
save(stage);
}
public void save(Stage stage){
kerdeslista.add(a);
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(stage);
if(file != null){
saveFile(kerdeslista, file);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public static void saveFile(ArrayList<String> list, File file) {
try {
PrintWriter out = new PrintWriter(file);
for (String val : list)
out.println(val + "\n");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 1