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

2016/2/27

Writing in a file Help please!

Vellyxenya Vellyxenya

2016/2/27

#
Hi, I had issues with the UserInfo class because it doesn't work when the project is exported to a .jar file. I replaced all the storage methods by writing-in-files methods by using:
FileInputStream fIn = new FileInputStream("datas.txt");
                BufferedReader textFile = new BufferedReader(new InputStreamReader(fIn));
It still doesn't work... even if it works within Greenfoot? What's the problem? And what's the solution for that? Is the path to the .txt file false? should I name it like: "project.jar\datas.txt" Help me please, the deadline for my project is soon and I am still having issues with this...
danpost danpost

2016/2/27

#
Vellyxenya wrote...
It still doesn't work...
This is quite vague. By the title and the code provided, I can say that you cannot write through an input stream (you need an output stream to write).
Vellyxenya Vellyxenya

2016/2/27

#
I want to store/read datas from my datas.txt that I already placed in my project file (for passwords, scores etc..) The code of my data class is:
import greenfoot.*;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.io.*;

/**
 * Contient les méthodes permettant de stocker les crédits et le mot de passe/pseudo
 */
public class DATA extends Actor
{
    public boolean on;
    static int answer;
    GreenfootImage image = new GreenfootImage(150, 30);
    GreenfootImage image2 = new GreenfootImage("TransparentButtons.png");
    GreenfootSound sound = new GreenfootSound("ButtonSound.wav");
    public static final int MAX_USERS = 1000;
    public static final int NUMBER_OF_INFOS = 15;
    public static int lineNumber;
    public static String[][] table = new String[MAX_USERS][NUMBER_OF_INFOS+2];

    public DATA()
    {
        image.setColor(Color.WHITE);
        image.drawRect(0, 0, 149, 29);
        image2.scale(150, 30);
    }

    public void act()
    {
        track();
        makeSound();
    }

    public static void storeWealth(int x, String input)  //stocke les crédits actuellement en possession du joueur
    {
        Menu.previousWealth += Game.credit;
        storeInfos2(x, input);
    }

    public static void storeIDandPW() //stocke le pseudo et mot de passe de l'utilisateur
    {
        try
        {
            FileInputStream fIn = new FileInputStream("datas.txt");
            BufferedReader textFile = new BufferedReader(new InputStreamReader(fIn));
            String line = "";
            lineNumber = 0;
            while ((line = textFile.readLine()) != null)
            {
                String[] datas = line.split(" ");
                if(datas[0].equals(Background.ID))
                {
                    JOptionPane.showMessageDialog(null, "An account with this pseudo already exists, please choose another one");
                    textFile.close();
                    return;
                }
                lineNumber++;
            }
            BufferedWriter printWriter = new BufferedWriter(new FileWriter("datas.txt", true));
            printWriter.newLine();
            printWriter.write(Background.ID+" "+Background.PW);
            for(int j = 0; j<NUMBER_OF_INFOS; j++)
            {
                printWriter.write(" "+0);
            }
            printWriter.close();
            Menu.previousWealth = 0;
            Menu.account = lineNumber;
            Greenfoot.setWorld(new Menu());
        }
        catch(IOException e)
        {
            System.out.println("problem with the file...");
        }
    }

    public static void storeInfos(int x) //stocke les données de l'utilisateur après les actions d'achat.
    {
        try
        {
            FileInputStream fIn = new FileInputStream("datas.txt");
            BufferedReader textFile = new BufferedReader(new InputStreamReader(fIn));
            String line = "";
            lineNumber = 0;
            while ((line = textFile.readLine()) != null)
            {
                String[] datas = line.split(" ");
                for(int i = 0; i<NUMBER_OF_INFOS+2; i++)
                {
                    table[lineNumber][i] = datas[i];
                }
                lineNumber++;
            }
            table[Menu.account][x] = 1+"";
            table[Menu.account][2] = Menu.previousWealth+"";
            textFile.close();

            BufferedWriter printWriter = new BufferedWriter(new FileWriter("datas.txt"));
            for(int i = 0; i<lineNumber; i++)
            {
                for(int j = 0; j<NUMBER_OF_INFOS+2; j++)
                {
                    printWriter.write(table[i][j]+" ");
                }
                if(i<lineNumber-1)printWriter.newLine();
            }
            printWriter.close();
        }
        catch(IOException e)
        {
            System.out.println("problem with the file...");
        }
    }

    public static void storeInfos2(int x, String input) //stocke les données de l'utilisateur après les actions d'achat.
    {
        try
        {
            FileInputStream fIn = new FileInputStream("datas.txt");
            BufferedReader textFile = new BufferedReader(new InputStreamReader(fIn));
            String line = "";
            lineNumber = 0;
            while ((line = textFile.readLine()) != null)
            {
                String[] datas = line.split(" ");
                for(int i = 0; i<NUMBER_OF_INFOS+2; i++)
                {
                    table[lineNumber][i] = datas[i];
                }
                lineNumber++;
            }
            table[Menu.account][x] = input;
            textFile.close();

            BufferedWriter printWriter = new BufferedWriter(new FileWriter("datas.txt"));
            for(int i = 0; i<lineNumber; i++)
            {
                for(int j = 0; j<NUMBER_OF_INFOS+2; j++)
                {
                    printWriter.write(table[i][j]+" ");
                }
                if(i<lineNumber-1)printWriter.newLine();
            }
            printWriter.close();
        }
        catch(IOException e)
        {
            System.out.println("problem with the file...");
        }
    }

    public void track()
    {
        if(mouseOn() == false)
        {
            on = false;
        }
    }

    public void makeSound()
    {
        if(mouseOn() == true && on == false)
        {
            sound.play();
            on = true;
        }
    }

    public boolean mouseOn() //* Détecte si la souris est sur un certain objet donné.
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null)
        {
            if( (mouse.getX() < (getX()+ this.getImage().getWidth()/2)) &&
            (mouse.getX() > (getX()- this.getImage().getWidth()/2)) &&
            (mouse.getY() < (getY()+ this.getImage().getHeight()/2)) &&
            (mouse.getY() > (getY()- this.getImage().getHeight()/2)))
            {
                return true;
            }
        }
        return false;
    }

    public void changeImage()
    {
        if(mouseOn() == false)
        {
            setImage(image2);
        }
        else if(mouseOn() == true)
        {
            setImage(image);
        }
    }

    public void changeImage(GreenfootImage i1, GreenfootImage i2)
    {
        if(mouseOn() == true) setImage(i2);
        else setImage(i1);
    }

}
The problem is that it can't access the datas.txt file for unknown reason... What should I change?
Vellyxenya Vellyxenya

2016/2/27

#
For example, the storeIDandPW method contains the writing and the reading methods as well but it doesn't work in the jar file even if it works within greenfoot
Vellyxenya Vellyxenya

2016/2/28

#
It's ok, I finally solved it after hours or research, thank you^^
danpost danpost

2016/2/28

#
Vellyxenya wrote...
It's ok, I finally solved it after hours or research, thank you^^
You should show what you did so others can be informed and learn from your experience.
Vellyxenya Vellyxenya

2016/3/2

#
I uploaded a little Scenario showing how to do so here.
You need to login to post a reply.