0

Found the solution: you have to open the Streams like this:

FileInputStream  inputStream = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(inputStream);

Same with Output. This fixed it for me, should anyone stumble upon this in search for an answer.

Original question: Through a few tests with Toasts I have found that when I call the Constructor for ObjectOutputStream I get an IOException thrown.

My code looks like this. Note that this is merely a test project, and I can't even get this one to work.

    Button b = new Button(this);
    b.setText("write");
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            try {
                File f = new File("Filepath");
                if (!f.exists()) {
                    f.createNewFile();
                }

                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(f)); //IOException here!

                Series x = new Series("Test", 20, 12);
                // oos.writeObject(x);

                oos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    tv = new TextView(this);
    tv.setText("Not read anything yet!");

    Button r = new Button(this);
    r.setText("Read");
    r.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                ObjectInputStream ois = new ObjectInputStream(
                        new FileInputStream(new File("Filepath")));
                Series y = (Series) ois.readObject();
                tv.setText(y.getName() + "-" + y.getNumOfSeason() + "-"
                        + y.getNumOfEpisode());
                ois.close();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

The problem seems to be my constructor call. Before I added the part with

if (!f.exists()) {
                f.createNewFile();
            }

I got a FileNotFoundException.

What am I doing wrong?

Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30

4 Answers4

2

Here's an excerpt of the Oracle documentation for FileOutputStream

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

In your case, I think that the file is treated as a directory since it has no extension, so the exception is thrown.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • I added an extension, still getting FNFE thrown. – Adrian Jandl Apr 01 '13 at 08:29
  • @AdrianJandl, I was just making assumptions, you gotta investigate your issue based on the cases that the documentation mentions. Are you sure that there's nothing preventing the file from being created or opened? – Egor Apr 01 '13 at 09:17
0

A FileNotFoundException is thrown when program doesn't find the file in disk or wherever you are trying to access the file. Check your file path and try again.

javadev
  • 1,639
  • 2
  • 17
  • 35
  • I avoided the `FileNotFoundException` by checking if the file exists and if it doesn't creating it. I get an IOException at my Constructor call from OOS. – Adrian Jandl Apr 01 '13 at 08:21
  • The same, you are avoiding the FileNotFoundExecption but you are getting the IOException after. Are you sure about your file path? – javadev Apr 01 '13 at 08:23
  • I just put a string instead of my Filepath that says "test". Still getting the FileNotFoundException. Should it not be created when I open the Stream? – Adrian Jandl Apr 01 '13 at 08:27
  • Test with a real file, put it in C:/ for example and make the File constructor refer to it by File f = new File("C:/yourFile.txt"); – javadev Apr 01 '13 at 08:28
  • I am testing this on my phone. Should it not work just by using a `String` with the `File` Constructor? – Adrian Jandl Apr 01 '13 at 08:36
  • Well, I don't think so, if you don't provide with a path to your file (either full path or relative path) you will fail to an IOException anyway (either FNF exception or IO one) – javadev Apr 01 '13 at 09:08
0

I am not sure about this but i think you can try using a formatter, the formatter creates the file if it does not exist.

Formatter formatter = new Formatter (file);

I hope it helps.

avi
  • 371
  • 1
  • 7
  • 20
0

Found the solution: you have to open the Streams like this:

FileInputStream  inputStream = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(inputStream);

Same with Output. This fixed it for me, should anyone stumble upon this in search for an answer.

Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30