Java Filechooser Speichern

Jay_LP_

Aktives Mitglied
Guten Abend,

ich habe folgendes Problem ich möchte einen Open Filechooser machen, der eine Ausgewählte Datei zu einen bestimmten Pfad speichern soll.

Der Pfad ist (localFolder1 = new File(pfad + "/AppData/Local/test/" + na);
localFile1 = new File(pfad + "/AppData/Local/test/"+ na +"/Operator.txt")😉

Wie geht das?

MfG
Code:
JFileChooser chooser = new JFileChooser();
            chooser.showDialog(null, "Bild auswählen");
            int rueckgabewert = chooser.showOpenDialog(null);
            if (rueckgabewert == JFileChooser.APPROVE_OPTION) {
            //restliches Skript
            }
 
Damit ich Dich richtig verstehe: Du willst mit einem JFileChooser eine Datei auswählen lassen und diese dann in ein bestimmtes Verzeichnis kopieren lassen? Soll die Zieldatei dann Operator.txt heißen?
 
Mit chooser.getSelectedFile() bekommst Du die ausgewählte Datei (ein File-Objekt).

Skizze:
Java:
File file = chooser.getSelectedFile();
if (file == null) {
    return;
}
Path srcPath = file.toPath();
Path dstPath = Paths.get("/","pfad","zum","Zielverzeichnis", srcPath.getFileName());
if (!Files.exists(dstPath)) {
    Files.copy(srcPath, dstPath);
}
 
Also ich habe das jetzt so und es geht nicht er meint: "The method get(String, String...) in the type Paths is not applicable for the arguments (String, Path)"
Code:
localFolder1 = new File(pfad + "/AppData/Local/test/" + na);
            localFile1 = new File(pfad + "/AppData/Local/test/" + na + "/Operator.txt");
            String fe = localFolder1.getPath();
            create1(localFolder1, localFile1);
            JFileChooser chooser = new JFileChooser();
            chooser.showDialog(null, "Bild auswählen");
            int rueckgabewert = chooser.showOpenDialog(null);
            if (rueckgabewert == JFileChooser.APPROVE_OPTION) {
                FileWriter fw = null;
                try {
                    fw = new FileWriter(localFile1, true);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
               
                File file = chooser.getSelectedFile();
                if(file == null) {
                    return;
                }
                Path srcPath = file.toPath();
                Path dstPath = Paths.get(fe, srcPath.getFileName());
               
                if(!Files.exists(dstPath)) {
                    Files.copy(srcPath, dstPath);
                }
 
Was machst du da für Verrenkungen? 😉
Java:
		File f = new File("abc.txt");
		Path a = f.toPath();
		Path b = Paths.get("/foo/bar/baz", f.getName());
		if (!Files.exists(b)) {
			Files.copy(a, b);
		}
 

Neue Themen


Zurück
Oben