1

Im having problems writing/reading object as resources on file.

Project structure

I run code from the Main.java Class, and I want to write/read an object to the users.txt file. The object that I want to write/read is the TryStructure Object, which just incapsulates a 1 element array and implements the Serializable interface, needed for object writing.

package Main;

import java.io.Serializable;
import java.util.ArrayList;

public class TryStructure implements Serializable
{
    private static final long serialVersionUID = 1L;

    private ArrayList<Integer> a = new ArrayList<Integer>();

    public TryStructure()
    {
        a.add(5);
    }

    public String getStructure()
    {
        return a.toString();
    }
}

The Main.java class writes and reads the obj from the users.txt file.

package Main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URISyntaxException;
import java.net.URL;

public class Main
{
    public static void main(String[] args)
    {
        String res = "/res/users.txt";

        write(res);
        read(res);
    }

    private static void write(String resName)
    {
        TryStructure list = new TryStructure();

        FileOutputStream fos;
        ObjectOutputStream oos;

        URL resource = Main.class.getClass().getResource(resName);
        File file = null;

        try
        {
            file = new File(resource.toURI());

            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);

            System.out.println("WRITING: " + list.getStructure());

            oos.writeObject(list);

            oos.close();
            fos.close();
        }
        catch (IOException e)
        {
            System.err.println("users.txt not found !");
        }
        catch (URISyntaxException e)
        {
            e.printStackTrace();
        }
    }

    private static void read(String resName)
    {
        FileInputStream fos;
        ObjectInputStream oos;

        URL resource = Main.class.getClass().getResource(resName);
        File file = null;

        try
        {
            file = new File(resource.toURI());

            fos = new FileInputStream(file);
            oos = new ObjectInputStream(fos);

            System.out.println("READING: " + ((TryStructure) oos.readObject()).getStructure());

            oos.close();
            fos.close();
        }
        catch (IOException e)
        {
            System.err.println("users.txt not found !");
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("AA");
        }
        catch (URISyntaxException e)
        {
            e.printStackTrace();
        }
    }
}

If I run the code I get the following output:

WRITING: [5]
READING: [5]

If I then check the user.txt file I find it empty, and I find the information in bin/res/user.txt. I dont understand why it only updates the file in bin folder and not also the one in res folder, why is this happening? I would like to write to that specific file.

Thank you.

StefanoN
  • 147
  • 1
  • 7
  • try changing your file path from `/res` to `../res` –  May 22 '20 at 13:17
  • Is `Test/src/res/users.txt` part of your source code and associated resources, and are you seeking to write to it? Are you seeking to make your program write to a file that is part of your program's source code? I am not quite certain what you are seeking to do. – MelvinWM May 22 '20 at 14:20
  • @Hummingbird it throws null pointer exception, of course. – StefanoN May 22 '20 at 15:00
  • @MelvinWM The code has to write/read the list of user objects during its execution. Originally I had put the res folder outside src, like it should be, but no matter what path I put in it I couldn't load it. I looked at tons of resources loading questions, and at the end I decided to put it inside src and call it a day, but then this will happen. – StefanoN May 22 '20 at 15:16
  • @Steve Does it make sense to attempt to write to an application resource that is a part of the application itself? Which I/O target are you writing to? What does that I/O target allow reg. reading and writing? And are there alternative I/O targets? – MelvinWM May 22 '20 at 17:33
  • @Steve For instance, could you write to a file instead, such as a file located in the same folder as the application, or some other appropriate location? For instance, instead of `Main.class.getClass().getResource(resName)` and the like, could you use `new File(...)` or similar? See also https://stackoverflow.com/questions/193474/how-to-create-an-ini-file-to-store-some-settings-in-java . – MelvinWM May 23 '20 at 12:15
  • giving an absolute should work –  May 25 '20 at 06:29

0 Answers0