0

I'm a new be for java coding. I was trying my first code. I used this two lines of codes

printer = new PrintStream(new FileOutputStream("C:/Users/GM/Desktop/Out.docx", true));

printer = new PrintStream(new FileOutputStream("C:/Users/GM/Desktop/Out.txt", true));

and generated two files (a word file (Out.docx) and text file (Out.txt)) as output. The text file is working flawlessly. The word file opens in eclipse workstation but it doesn't open outside eclipse. My word processes are working properly. I tried both word 2012 and 2016. They doesn't open it. Is there any one out there who have the solution.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
FDEGM
  • 1
  • 1
    What are you writing into the two files? While a .txt is just a collection of text characters, a docx file has a particular format for it's contents, and if that's incorrect in some way, Word will detect that and not open it, whereas Eclipse won't be trying to open it in the same way and might just show 'something' but not as a Document. – Gwyn Evans Jun 11 '17 at 11:30
  • Are you trying to write something to MS Word document from Java? If it is the case, you need a third party library like Apache POI. But you can open ( not read) an existing word document from Java. Eclipse might be doing same - open but not read/write to docx. – SomeDude Jun 11 '17 at 11:38
  • Thank you for the answer. I do write on the .docx file and eclipse display it. No problem for writing on .docx and displaying in eclipse. My problem is it is not opened in the normal word processor (office 2012 and 2016 for example). Farther more I was trying to open the created .docx file in android word processor and it does open it properly. Again I transferred the file from the android to the PC now word open it properly. – FDEGM Jun 16 '17 at 08:01

1 Answers1

0

What You are doing here is saving plain text in a file. So, if you wrote to file like theAmazingFile.myCustomFileType it would be perfectly valid and you would be able to read that file with a notepad or any other text editor. So, You can read "C:/Users/GM/Desktop/Out.docx" if You renamed it into ".txt" but not like that.

However, You cannot save plain text to MS Word file because .docs files have their own format. If you find any valid word document and open it with notepad you will see jibberish. So, if you need exactly that, consider looking up an API.

Sam
  • 1,832
  • 19
  • 35