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

2014/1/23

Creating a file using Greenfoot

GreenAnthony GreenAnthony

2014/1/23

#
Okay following I have already posted a discussion like this but I got a website as an answer and the website had some coding, so I copied everything and inserted it on the right spot but now it still doesn't create a .txt file. After I thought for a little I decided to make the class that has the coding spawn using the addObject code but it told me I can not use it in this context so what should I do in order to create .txt files using Greenfoot??? Also the class I made is in a new over all class called "Other classes". By the way here is the coding:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
 
public class PrintWriterExample
{
    public static void main(String args) {
        String filename = "c:" + File.separator + "JustExample.txt";
        File f = new File(filename);
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(f);
            String strContent = "Just Example";
            int intContent = 1;
            double doubleContent = Math.random();
            //convinient way to add new line while print content
            pw.println(strContent);
            //using printf to format content. SInce java 1.5
            pw.printf("Hello this is %s. I am %d years old. My lucky number is  %f", strContent, intContent, doubleContent);
            pw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            //no matter what happen. close the output stream always.
            //note that closing a printer will not throw IOException
            if(pw!=null){
                pw.close();
            }
        }
    }
}
If I try using the addObject code it tells me this error: addObject(greenfoot.Actor,int,int) in greenfoot.World cannot be applied to (PrintWriterExample,int,int) Can you guys help me?!
davmac davmac

2014/1/23

#
You have copied the whole class, instead of just the part of code which writes the file. The code inside the main(...) method is what you want. Copy it and paste it into your own class at the appropriate place.
GreenAnthony GreenAnthony

2014/1/23

#
Can you copy and paste the codes but delete the ones that are not needed, cause right now I don't really know what you mean with main method...
danpost danpost

2014/1/23

#
davmac wrote...
The code inside the main(...) method is what you want.
That is what davmac wrote. Let me point out one thing; -- that was 'main(...) method', not 'main method'.
GreenAnthony GreenAnthony

2014/1/23

#
What is the "main(...) method"???
davmac davmac

2014/1/23

#
The main(...) method starts at line 08 and ends at line 31. The code "inside" the main method therefore is from lines 09 through 30. By main(...) method we mean the method that is called main and which takes some arguments. You really need to learn what a method is, how one is written and how to recognize one, before you're going to have much hope of writing your own code! Try going through the tutorials.
GreenAnthony GreenAnthony

2014/1/24

#
Oh my god.... xD I did learn the terminology but I forgot most of it just because I usually don't use it and as I said I already know how to program it's just that I only know the basics. And how to create files is lets say more advanced!
GreenAnthony GreenAnthony

2014/1/24

#
And by the way I know that I pasted to much but i thought that some people will tell me that I forgot to add the import codes...! And how the f*ck is any of this supposed to help me?! If you can't help me stay out of this discussion!
davmac davmac

2014/1/24

#
You've said this:
If I try using the addObject code it tells me this error: addObject(greenfoot.Actor,int,int) in greenfoot.World cannot be applied to (PrintWriterExample,int,int)
But the code you posted doesn't even have an addObject(...) call in it. How is anyone supposed to help you if they can't see what you've done wrong? And why would you expect anyone to help you if you're rude to the first person who tries? Post the code that you're actually having a problem with, and be polite.
GreenAnthony GreenAnthony

2014/1/25

#
Basically I don't even care about the error, I just want to know how to create a file using Greenfoot... So could somebody just please tell me what codes I have to write in order to create a .txt file?
Gevater_Tod4711 Gevater_Tod4711

2014/1/25

#
Basically to write a file you need to import this classes:
1
2
3
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
And use this code to write the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BufferedWriter file = null;
        try {
            file = new BufferedWriter(new FileWriter("filename.txt"));
            file.write("The text you want to write down in the file.");
            file.close();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        finally {
            try {
                file.close();
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
davmac davmac

2014/1/25

#
GreenAnthony wrote...
So could somebody just please tell me what codes I have to write in order to create a .txt file?
as I already told you - the code that you have inside your main(...) method. You just need to put it inside another class (and method) at whatever place you want to actually write a file.
You need to login to post a reply.