This site requires JavaScript, please enable it in your browser!
Greenfoot back
GreenAnthony
GreenAnthony wrote ...

2014/2/21

Writing a file in a certain place

GreenAnthony GreenAnthony

2014/2/21

#
Well, I am using a code that creates a file. Right now it creates the file on the place where the game is. However I would like the fie to be written in the documents. Also I would like to know how to create folders through the game. So here is the code I am using right now:
 BufferedWriter file = null;  
        try {  
            file = new BufferedWriter(new FileWriter("MustRead.txt"));  
            file.write("Warning! A virus has been detected!");  
            file.close();  
        }  
        catch (IOException ioe) {  
            ioe.printStackTrace();  
        }  
        finally {  
            try {  
                file.close();  
            }  
            catch (IOException ioe) {  
                ioe.printStackTrace();  
            }  
How do I make it write the file in the documents and how do I create files? I need to know this since my game has a "creator" mode where the player is able to create his own level.
Kytuzian Kytuzian

2014/2/21

#
To create a folder, simple type:
boolean Success = new File(Path).mkdir();
If I recall correctly, creating a FileWriter of a file that does not exist will simply create the file unless there is an error, so you should be set on that.
You need to login to post a reply.