Java variables across methods
        Posted  
        
            by 
                NardCake
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by NardCake
        
        
        
        Published on 2012-09-23T03:27:25Z
        Indexed on 
            2012/09/23
            3:37 UTC
        
        
        Read the original article
        Hit count: 157
        
I'm making a basic text editor, and I have 2 methods the first one is triggered when a user click 'Open' and it prompts the user to pick a file and it opens the file fine. I just want to access the same file path which is in a variable in the method that is triggered when the user clicks save. My methods are public, Iv'e tried accessing it through a class, still no. Please help! Code:
public void open(){
    try{
        //Open file
    JFileChooser fc = new JFileChooser();
    fc.showOpenDialog(null);
    File file = fc.getSelectedFile();
    String haha = file.getPath();
    BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
    String line;
    while((line = br.readLine()) != null){
        text.append(line + "\n");
    }
    } catch (FileNotFoundException e){
        e.printStackTrace();
    }catch (IOException e){
    }
}
public void save(){
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file.filePath));
        bw.write(text.getText());
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
© Stack Overflow or respective owner