|
|
|
|
|
|
|
|
Re: Writing Files [message #897121 is a reply to message #897115] |
Sun, 22 July 2012 05:41 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
Tim,
To be fully general, you should be careful about what encoding is used,
so be aware that there are methods like getCharSet and setCharSet. This
is important because the workspace has preferences that determine what
the default encoding is for new files. It might well be UTF8 or
something else. Even if the file doesn't exist, you can determine what
the char set should be for that new file and you should use that for
your getBytes call.
On 22/07/2012 2:08 AM, Tim Armstrong wrote:
> Thanks Ed.
>
> It was the fact that there was no OutputStream anywhere that was, and
> to some extent, is still breaking my head.
>
> Anyway to complete this for others I tried the following code
> successfully.
>
> summaryFile = theProject.getFile("SummaryFile.txt");
> try {
> if (summaryFile.exists()) {
> String headerString = "Reset";
> InputStream source = new
> ByteArrayInputStream(headerString.getBytes());
> summaryFile.setContents(source, IResource.FORCE, null);
> } else {
> String headerString = "Init";
> InputStream source = new
> ByteArrayInputStream(headerString.getBytes());
> summaryFile.create(source, IResource.FORCE, null);
> }
> } catch (CoreException e) {
> e.printStackTrace();
> }
>
> Next is to work through IFile.appendContents().
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: Writing Files [message #897315 is a reply to message #897121] |
Mon, 23 July 2012 14:53 |
Dani Megert Messages: 3802 Registered: July 2009 |
Senior Member |
|
|
On 22.07.2012 07:41, Ed Merks wrote:
> Tim,
>
> To be fully general, you should be careful about what encoding is
> used, so be aware that there are methods like getCharSet and
> setCharSet. This is important because the workspace has preferences
> that determine what the default encoding is for new files. It might
> well be UTF8 or something else. Even if the file doesn't exist, you
> can determine what the char set should be for that new file and you
> should use that for your getBytes call.
I suggest you simply use file buffers, which takes care of all that for
you. Something along these lines:
ITextFileBufferManager manager= ITextFileBufferManager.DEFAULT;
manager.connect(file.getFullPath(), LocationKind.IFILE,
monitor);
ITextFileBuffer buffer=
manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
IDocument document= buffer.getDocument();
// modify the document as you like
buffer.commit(monitor, true);
manager.disconnect(file.getFullPath(), LocationKind.IFILE,
monitor);
Dani
>
>
> On 22/07/2012 2:08 AM, Tim Armstrong wrote:
>> Thanks Ed.
>>
>> It was the fact that there was no OutputStream anywhere that was, and
>> to some extent, is still breaking my head.
>>
>> Anyway to complete this for others I tried the following code
>> successfully.
>>
>> summaryFile = theProject.getFile("SummaryFile.txt");
>> try {
>> if (summaryFile.exists()) {
>> String headerString = "Reset";
>> InputStream source = new
>> ByteArrayInputStream(headerString.getBytes());
>> summaryFile.setContents(source, IResource.FORCE, null);
>> } else {
>> String headerString = "Init";
>> InputStream source = new
>> ByteArrayInputStream(headerString.getBytes());
>> summaryFile.create(source, IResource.FORCE, null);
>> }
>> } catch (CoreException e) {
>> e.printStackTrace();
>> }
>>
>> Next is to work through IFile.appendContents().
>
|
|
|
Powered by
FUDForum. Page generated in 0.05126 seconds