0

I have a fairly simple program that once some data is entered into a JTable it can then be exported or "saved" to an excel spreadsheet. All of this is working fine and it is making a saved excel file perfectly.

The problem I have run into is this:

When you try to put the excel file in a subfolder of, say, the desktop folder (desktop/folder) it saves it on the desktop instead; but it only does this on macs.

When I do this on a windows computer it works 100% of the time.

I was wondering if anyone has any insight into the problem or a fix for this?

This is my JFileChooser code which would in theory be causing the problem.

JFileChooser fc = new JFileChooser();
fc.setSelectedFile(new File(jTextField3.getText() + jTextField6.getText() + "-" + jTextField7.getText() + "-" + jTextField8.getText()));
int option = fc.showSaveDialog(PScalcUI.this);
if(option == JFileChooser.APPROVE_OPTION){

    String filename = fc.getSelectedFile().getName(); 
    String path = fc.getSelectedFile().getParentFile().getPath();

    int len = filename.length();
    String ext = "";
    String file = "";

    if(len > 4){
        ext = filename.substring(len-4, len);
    }

    if(ext.equals(".xls")){
        file = path + "\\" + filename; 
    }else{
        file = path + "\\" + filename + ".xcl"; 
    }
    toExcel(jTable1, new File(file));
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • 3
    Please consider add a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) to your question so others can test the behavior you're facing. – dic19 Jul 22 '14 at 12:56
  • 1
    Don't use backslashes (``\``) in your paths - use forward slashes (`/`). It doesn't make any difference on Windows, but will cause you headaches on most other platforms (including OS X). – Mac Jul 23 '14 at 02:47
  • 1
    Don't use platform-dependent strings at all. Either use `File.separator` or use the constructor `new File(String parent, String child)` – Erwin Bolwidt Jul 23 '14 at 08:10
  • Why is this closed? The post adheres to all the MCVE rules: it is small, it is complete (if you don't know how to put a `main` method around some code you shouldn't be here) and it clearly describes the problem: when he tries to place a file in a subfolder of the desktop, it ends up on the desktop instead, but only on a Mac; not on a PC. Bot @Mac and I could easily understand the problem without any ambiguity. – Erwin Bolwidt Jul 23 '14 at 10:50

1 Answers1

1

You're building the 'file' in a platform dependent way using backslashes, which only works on Windows.

You need to use the constructor of java.io.File that takes both a parent path and a file name to construct the right file object:

    File file;

    if(len > 4){
        ext = filename.substring(len-4, len);
    }

    if(ext.equals(".xls")){
        file = new File(path, filename); 
    }else{
        file = new File(path, filename + ".xcl"); 
    }
    toExcel(jTable1, file);
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79