Hej alle sammen, jeg har fået det problem at jeg ikke kan få mine filer til at ende med den valgte fil type når den skal gemmes.
Foreksempel : Jeg vælger i JFileChooser at det skal være en .html fil (og ikke bare alle fil typer). Og når jeg så gemmer filen ender den ikke med .html men med .asm eller ikke noget.
Nogen som vil være søde at hjælpe med det?
Kode
- JFileChooser jchooser = new JFileChooser();
- FileNameExtensionFilter asm = new FileNameExtensionFilter("Assembly (.asm)", "asm");
- FileNameExtensionFilter cpp = new FileNameExtensionFilter("C++ (.cpp)", "cpp");
- FileNameExtensionFilter cpph = new FileNameExtensionFilter("C++ Header (.h)", "h");
- FileNameExtensionFilter cpp1 = new FileNameExtensionFilter("C++ (.cc)", "cc");
- FileNameExtensionFilter cpp2 = new FileNameExtensionFilter("C++ (.cxx", "cxx");
- FileNameExtensionFilter c = new FileNameExtensionFilter("C (.c)", "C");
- FileNameExtensionFilter css = new FileNameExtensionFilter("CSS (.css)", "css");
- FileNameExtensionFilter HTML = new FileNameExtensionFilter("HTML (.html)", "html");
- FileNameExtensionFilter PHP = new FileNameExtensionFilter("PHP (.php)", "php");
- FileNameExtensionFilter JS = new FileNameExtensionFilter("JavaScript (.js)", "js");
- FileNameExtensionFilter java = new FileNameExtensionFilter("Java (.java)", "java");
- FileNameExtensionFilter javas = new FileNameExtensionFilter("Java Server Page (.jsp)", "jsp");
- FileNameExtensionFilter xml = new FileNameExtensionFilter("XML (.xml)", "xml");
-
- jchooser.setApproveButtonText("gem");
-
- jchooser.setFileFilter(asm);
- jchooser.setFileFilter(c);
- jchooser.setFileFilter(cpp);
- jchooser.setFileFilter(cpph);
- jchooser.setFileFilter(cpp1);
- jchooser.setFileFilter(cpp2);
- jchooser.setFileFilter(css);
- jchooser.setFileFilter(HTML);
- jchooser.setFileFilter(JS);
- jchooser.setFileFilter(java);
- jchooser.setFileFilter(javas);
- jchooser.setFileFilter(PHP);
- jchooser.setFileFilter(xml);
-
- int action = jchooser.showSaveDialog(this);
- if (action != JFileChooser.APPROVE_OPTION)
- return;
-
- File fi = jchooser.getSelectedFile();
- if(!fi.getName().endsWith(".asm")) {
- fi = new File(fi.getAbsolutePath() + ".asm");
- }
-
- if (!fi.getName().endsWith(".cpp")) {
- fi = new File(fi.getAbsolutePath() + ".cpp");
- }
-
- if (!fi.getName().endsWith(".h")) {
- fi = new File(fi.getAbsolutePath()+ ".h");
- }
-
- if (!fi.getName().endsWith(".cc")) {
- fi = new File(fi.getAbsolutePath() + ".cc");
- }
-
- if (!fi.getName().endsWith(".cxx")) {
- fi = new File(fi.getAbsolutePath() + ".cxx");
- }
-
- if (!fi.getName().endsWith(".c")) {
- fi = new File(fi.getAbsolutePath() + ".c");
- }
- if (!fi.getName().endsWith(".css")) {
- fi = new File(fi.getAbsolutePath() + ".css");
- }
-
- if (!fi.getName().endsWith(".html")) {
- fi = new File(fi.getAbsolutePath() + ".html");
- }
-
- if (!fi.getName().endsWith(".php")) {
- fi = new File(fi.getAbsolutePath() + ".php");
- }
-
- if (!fi.getName().endsWith(".js")) {
- fi = new File(fi.getAbsolutePath() + ".js");
- }
-
- if (!fi.getName().endsWith(".java")) {
- fi = new File(fi.getAbsolutePath() + ".java");
- }
-
- if (!fi.getName().endsWith(".jsp")) {
- fi = new File(fi.getAbsolutePath() + ".jsp");
- }
-
- if (!fi.getName().endsWith(".xml")) {
- fi = new File(fi.getAbsolutePath() + ".xml");
- }
-
- BufferedWriter bf = null;
-
- try {
- bf = new BufferedWriter(new FileWriter(fi));
-
- editor.getJTP().write(bf);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
-
- finally {
- if (bf != null) {
- try {
- bf.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Det skal lige meddeles at der ikke kommer nogen fejl eller advarsler.