3

I have an IFile shows the absolute path as

/dynamic/WebContent/testing.html

I want to make some changes in the above testing.html. It means I need to load the file, then need to modify it and then save it . What is the procedure to execute the above steps in eclipse plug-in development.

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128

2 Answers2

10

See IFile.getContents(), setContents() and create() methods.

Use getContents to read file into memory, modify it, then use setContents to write it out. That will keep your workspace in sync. If you try to convert to File and use standard Java file I/O, your workspace will be out of sync (requiring refresh) and you will be completely incompatible with other file storage systems used by Eclipse

Konstantin Komissarchik
  • 28,879
  • 6
  • 61
  • 61
  • How to set the contents in some specific position of the file.Don't I need to dynamically load the file. – Abhishek Choudhary Apr 26 '11 at 06:47
  • Use getContents to read file into memory, modify it, then use setContents to write it out. That will keep your workspace in sync. If you try to conver to File and use Java RandomAccessFile, your workspace will be out of sync (requiring refresh) and you will be completely incompatible with other file storage systems used by Eclipse. – Konstantin Komissarchik Apr 26 '11 at 16:31
  • If you have questions about how to modify HTML content with new information, please post that as a separate question, as it doesn't have much to do with Eclipse or file I/o. – Konstantin Komissarchik Apr 26 '11 at 16:33
  • @Konstantin Komissarchik: My +1 for out-of-sync point. Normally it is overlooked. Suggest you to put that in your answer too. – Favonius Apr 26 '11 at 17:55
  • After using getContents, to modify the content do I need to use bUfferedInput/datainputStream for writing in a specific point of the file. – Abhishek Choudhary Apr 27 '11 at 06:10
  • IFile API does not support random access i/o. Read contents using getContents(), manipulate them in memory and then write them all out using the setContents(). The setContents() method takes an InputStream, so you will need to convert your in-memory represenation into an InputStream first... See ByteArrayInputStream, String.getBytes(), etc. You do not need to to buffer the input stream you give to setContents() as the data is already coming from memory. – Konstantin Komissarchik Apr 27 '11 at 16:08
-3

If your file is local try :

import java.io.RandomAccessFile;
RandomAccessFile file = new RandomAccessFile("IFile".getLocation().toFile(), "rwd");
TomaC
  • 89
  • 6
  • Good point, but this is just an alternative in case you need random access and getContents(), setContents() are too slow. – TomaC Apr 27 '11 at 05:47