Opening an existing file from netbeans - java

I've created an application in Netbeans 6.9 where my ultimate aim is to create a .tcl file(or text file will do). When i run my application once I create the .tcl file and save it at a location. When I run the 2nd time and if I open that file in my application, then I am not able to get that file.I mean the data is not appending to the file,it is replacing the text.My main aim is to use just that text file that is created. But for creating that file in my application I've many options. Can anyone suggest what should I do,such that when I run my application the 2nd or later times then my options whatever I selected before(in the previous run) should be present(selected) and also the text should be appended to that file instead of replacing.
I am not writing the text directly.I have created buttons in my application.If i click on "new node" button then a dialog box appears and asks for the node name.When I click ok button, then some text is written in that file and the status is shown in a text field(in my application) that the node is created. I want that when I open an existing file then all my statuses like "new node created","new link created",etc. should be there. Also in my link button,I've got names of all the nodes created,so I want that all those node names should be present when I open an existing file.

FileWriter fstream = new FileWriter("out.txt",true);//note true here/ it will open file in Appendmode
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
It has nothing to do with netbeans
Ref

Related

Eclipse RCP: Get current text from editor, even if it is not saved

I'm developing an Eclipse plugin in which I'm trying to get the text from the current open editor, I can do that using this code:
IFile file = ((FileEditorInput) editorInput).getFile();
InputStream inputStream = file.getContents();
String result = IOUtils.toString(inputStream);
But looks like it only returns the state of the file when it is saved, if I have that file open in the editor and make some changes I want to get this latest state, not the one when it was saved, is there any API I can use for that?

How to save files programmatically in Eclipse without using an editor?

I am developing an Eclipse plug-in that programmatically modifies C++ files from the workspace. I am now trying to save the changes made. I have taken a look at this solution : How can I call save method in eclipse plugin development programmatically but this includes having the files I want to save open in the editor. Since I have
5k+ files to modify and save, I cannot afford to open them all in an editor before saving.
While looking for a solution I found this topic on the official Eclipse forum : https://www.eclipse.org/forums/index.php/t/1070377/ . Unfortunately, no answer has been provided.
I have tried using :
PlatformUI.getWorkbench().getService(IHandlerService.class).executeCommand("org.eclipse.ui.file.saveAll", event)
or
IWorkspace.save(boolean, IProgressMonitor)
or
ITranslationUnit.save(IProgressMonitor, boolean)
or
ICProject.save(IProgressMonitor, boolean)
but none of these solution worked.
My question is therefore :
How to save files programmatically in Eclipse without using an editor ?
Thanks a lot in advance
EDIT :
example of what I'm trying to achieve
deleteAndSave(IFunction functionToDelete) {
boolean forceDeletion = true;
IProgressMonitor progressMonitor = new NullProgressMonitor();
//delete the portion of code
functionToDelete.delete(forceDeletion, progressMonitor);
//we get the file containing the function
IFile file = (IFile) functionToDelete.getUnderlyingResource();
//save the file to the disk (ideally 'file.save()' ?)
file.getWorkspace().save(true, progressMonitor);
}
At the end of this execution, I would expect the new version of the file to be saved on the disk. The function is correctly deleted and the modifications appear in the IDE if I open the file in the editor, but the file is marked as unsaved by Eclipse (showing a star before the file name).
EDIT :
While this doesn't answer the question (without using an editor), I got the correct behaviour with :
deleteAndSave(IFunction functionToDelete) {
IProgressMonitor progressMonitor = new NullProgressMonitor();
IWorkbenchPage activePage = PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage();
functionToDelete.delete(true, progressMonitor);
IFile file = (IFile) functionToDelete.getUnderlyingResource();
IEditorPart editorPart = IDE.openEditor(activePage, file);
editorPart.doSave(progressMonitor);
activePage.closeEditor(editorPart, false);
}
However this opens an editor page for each file and closes it immediately, so the performance are not satisfying for a large volume of files.
It sounds like the problem might be that your operation is modifying the working copy of a file, and not the underlying file itself.
Changes to the working copy can be synced to the underlying file by calling IWorkingCopy.commit().
Does the following help?
...
functionToDelete.delete(forceDeletion, progressMonitor);
ITranslationUnit tu = functionToDelete.getTranslationUnit();
if (tu.isWorkingCopy()) {
boolean forceCommit = true;
((IWorkingCopy) tu).commit(forceCommit, progressMonitor);
}

Saving txt file to a user-defined location in Java SWT

I am making a simple java application using SWT GUI to save a txt file to a user defined location. When a button is clicked, I open the directories like this:
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
directoryDialog.setFilterPath(selectedDir);
directoryDialog.setMessage("Please select a directory and click OK");
String dir = directoryDialog.open();
Now I need to be able to make a file and save it to that location. How can I make and save to the user given location?

Java read/write text file on a website

Basically I've uploaded a text file to my host and I want to edit the file and read it with java. I've created the permissions for it but im not sure how to do it with Java. This is my code which read/writes locally:
Read:
BufferedReader mainChat = new BufferedReader(new FileReader("./messages/messages.txt"));
String str;
while ((str = mainChat.readLine()) != null)
{
System.out.println(decrypt.Decrypt(str, salt));
}
mainChat.close();
Write:
FileWriter chatBuffer = new FileWriter("./messages/messages.txt",true);
BufferedWriter mainChat = new BufferedWriter(chatBuffer);
mainChat.write(message);
mainChat.newLine();
mainChat.flush();
mainChat.close();
How would I have to modify this to make it work? Thanks
I don't think you can read/write directly to a file on a web server the way you would on a local filesystem. What you'll probably need to do is:
download the file
open it in the local editor
when it's saved, automatically re-upload the file
You can do this all within the editor, and hide this in the app by having it do the download-edit-save-upload in the background. Many text editors will do this by establishing a remote connection similarly, and making the file writing round trip transparent to user.
You should implement some sort of remote procedure call. Basically, from the client send the server a message containing what you'd like to put in the file. Then have the server actually open the file and write the content of the message to the file.

Why doesn't the CSV file in my applet work?

I created an applet that requires a CSV file for information. The way the applet works, is that there is a text field in which you type in your zip code, then you press a button. That causes the program to parse through the CSV file which contains a latitude and longitude, then display the latitude and longitude on a JLabel in the applet.
When I created it, I debugged it and tested it, so I know it works on my desktop (when running in eclipse). The problem is when I put in on the web, it displays but can't do anything, meaning it is just an applet with a text field and a button, but when you press the button, nothing happens. I know that it is not my ActionListener, because it works on the desktop, but I must be doing something wrong with the HTML of it. The name of the CSV file is zips.csv. The name of the main class is main.class (or main.java) and the action listener is myActionListener.class (or myActionListener.java).
Here is the HTML that I am using for it right now:
<applet archive="sites/default/files/myApplet.jar" code="main.class" width="500" height="200">
</applet>
Revision:
Romething else that someone recommended to me was to create a php script that will parse the csv file, and than have that return a value to the java applet. My knowledge of PHP ls limited, so I was wonder if someone could tell me how I could go about doing this, or telling me where I can learn how to do this.
CsvReader products = new CsvReader("zips.csv");
My crystal ball tells me that CsvReader presumes the String to represent a File object. It might also have another constructor that accepts an URL.
A sand-boxed applet cannot access File objects, and a trusted applet can only access File objects on the computer of the end user. That is useless to this applet. If the API has a constructor that accepts an URL, that is the one to use here. Something like:
URL url = this.getClass().getResource("zips.csv");
//CsvReader products = new CsvReader(url);
InputStream is = url.openStream();
CsvReader products = new CsvReader(is);
A constructor that accepts an InputStream is even more versatile, and only a line longer.
If the CsvReader accepts neither of URL or InputStream, I suggest you find another API. One that was not written by amateurs.

Categories