0

Im hallway through my java course and Im learning how to write programs that save information to files. The code written below actually works fine when I run it only problem is that When i try to open the pages file after writing to it pages cannot open the file. I get a message saying pages can't read the file. it seems that when the print stream writes to a pages file it corrupts it in some way. the error message is coming from the Mac OS so there is nothing wrong with the code (which is being run in eclipse). Ihe exact error message is (Pages couldn’t read the file. You can try restoring to a previous version)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

class WriteOK {

    public static void main(String args[])throws FileNotFoundException {

        PrintStream diskWriter = new PrintStream(new File("/Users/jonathanhickey/Desktop/test.pages"));

   
        diskWriter.println("OK");

        diskWriter.close();
    }
}
John
  • 21
  • 5
  • What program are you using to open the file? A normal editor? What exactly is the error you're receiving? Is it from your operating system or java? Please provide more information via the [edit] button below your question. – Lino Mar 10 '21 at 15:38
  • Is "Pages" really using plaintext for its files or is it xml based (like Microsoft Word)? – Lino Mar 10 '21 at 15:51
  • sure I've made some edits im using Mac OS so I just click on the pages document on my desktop. the error message is (Pages couldn’t read the file. You can try restoring to a previous version) – John Mar 10 '21 at 15:59
  • also that is an interesting point about pages not being plain text, perhaps java trying to write plain text to it corrupts the file – John Mar 10 '21 at 16:01
  • java *doesn't know* that it's writing a `.pages` file, you have to **tell** java what it should write inside that file. Maybe there exists a java library for easy writing to such a file format. But I can ensure you java does **not** have such a feature built in. – Lino Mar 10 '21 at 16:04
  • I just Wrote into a text edit plain text file instead that seems to be ok. – John Mar 10 '21 at 16:23

0 Answers0