Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Writing Files(Writing Files Without an Editor)
Writing Files [message #897059] Sat, 21 July 2012 09:49 Go to next message
Tim Armstrong is currently offline Tim ArmstrongFriend
Messages: 6
Registered: July 2009
Junior Member
I just want to write to a file directly. No editors or views. The issue being of course if I just use java.io unless I force a Refresh the files don't appear or are "out of sync"
Re: Writing Files [message #897060 is a reply to message #897059] Sat, 21 July 2012 09:56 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Tim,

You still should use the IFile; that also ensures you build up the local
history in the workspace.


On 21/07/2012 11:49 AM, Tim Armstrong wrote:
> I just want to write to a file directly. No editors or views. The
> issue being of course if I just use java.io unless I force a Refresh
> the files don't appear or are "out of sync"


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Writing Files [message #897061 is a reply to message #897060] Sat, 21 July 2012 10:55 Go to previous messageGo to next message
Tim Armstrong is currently offline Tim ArmstrongFriend
Messages: 6
Registered: July 2009
Junior Member
That's the bit that has me completely mystified.

I mean I know I can get one from the IProject but how one earth do I actually write to it?

In the utility I'm trying to port I set up a PrintWriter
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(targetFileDirectory + "\\" + targetFileNamePrefix + ".mye")));

Then insert the lines I want
pw.printf("%sA line with some leading blanks.%n", lb);

There don't seem to be any write operations and most of the functions reference InputStreams.
Re: Writing Files [message #897062 is a reply to message #897060] Sat, 21 July 2012 11:09 Go to previous messageGo to next message
Tim Armstrong is currently offline Tim ArmstrongFriend
Messages: 6
Registered: July 2009
Junior Member
I've managed to work through some of the mysteries of Eclipse and for the simple navigator I'm trying to add a utility popup menu extension point to I've worked out how to get the TreeSelection. And that the first segment of the TreePath gives me the project and the last seems to be the file I'm using as input for the reformatted output.

IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage activePage = window.getActivePage();
ISelection selection = activePage.getSelection();
if (selection != null) {
System.out.println("Got selection");
if (selection instanceof IStructuredSelection) {
System.out.println("Got a structured selection");
if (selection instanceof ITreeSelection) {
TreeSelection treeSelection = (TreeSelection) selection;
TreePath[] treePaths = treeSelection.getPaths();
TreePath treePath = treePaths[0];

Object firstSegmentObj = treePath.getFirstSegment();
IProject theProject = (IProject) ((IAdaptable) firstSegmentObj).getAdapter(IProject.class);

Object lastSegmentObj = treePath.getLastSegment();
if(lastSegmentObj instanceof IAdaptable) {
IFile file = (IFile) ((IAdaptable) lastSegmentObj).getAdapter(IFile.class);
if(file != null) {
System.out.println("File=" + file.getName());
String path = file.getRawLocation().toOSString();
System.out.println("path: " + path);
}
}

IFile theFile = theProject.getFile("MyNewFile.txt");
Re: Writing Files [message #897063 is a reply to message #897060] Sat, 21 July 2012 11:10 Go to previous messageGo to next message
Tim Armstrong is currently offline Tim ArmstrongFriend
Messages: 6
Registered: July 2009
Junior Member
I even use Java Reflection so I could look at all the different segments and the classes, superclasses and interfaces.

System.out.println("First");
Object firstSegmentObj = treePath.getFirstSegment();
Class currClass = firstSegmentObj.getClass();
while(currClass != null) {
System.out.println(" Class=" + currClass.getName());
Class[] interfaces = currClass.getInterfaces();
for(Class interfacey : interfaces) {
System.out.println(" I=" + interfacey.getName());
}
currClass = currClass.getSuperclass();
}
Re: Writing Files [message #897064 is a reply to message #897060] Sat, 21 July 2012 11:14 Go to previous messageGo to next message
Tim Armstrong is currently offline Tim ArmstrongFriend
Messages: 6
Registered: July 2009
Junior Member
So I can find the input file I want to summarise and I know the project I want to write my new file into and I understand that the IFile getFile method doesn't have to reference an existing file

IFile theNewFile = theProject.getFile("MyNewFile.txt");

But back to the mystery, at least to this newbie, how on earth do I write to "theNewFile".
Re: Writing Files [message #897074 is a reply to message #897064] Sat, 21 July 2012 13:14 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
IFile.setContents().

On 21/07/2012 1:14 PM, Tim Armstrong wrote:
> So I can find the input file I want to summarise and I know the
> project I want to write my new file into and I understand that the
> IFile getFile method doesn't have to reference an existing file
> IFile theNewFile = theProject.getFile("MyNewFile.txt");
>
> But back to the mystery, at least to this newbie, how on earth do I
> write to "theNewFile".


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Writing Files [message #897115 is a reply to message #897074] Sun, 22 July 2012 00:08 Go to previous messageGo to next message
Tim Armstrong is currently offline Tim ArmstrongFriend
Messages: 6
Registered: July 2009
Junior Member
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().
Re: Writing Files [message #897121 is a reply to message #897115] Sun, 22 July 2012 05:41 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
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 Go to previous message
Dani Megert is currently offline Dani MegertFriend
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().
>
Previous Topic:how to configure assertions
Next Topic:Can't create new java project
Goto Forum:
  


Current Time: Thu Mar 28 17:29:08 GMT 2024

Powered by FUDForum. Page generated in 0.05126 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top