I have a JEditorPane in my application and I was loading java files into it using jsyntaxpane and the following code and it was working perfectly:
to highlight
jsyntaxpane.DefaultSyntaxKit.initKit();
textarea.setContentType("text/java");
to load file in
int a = filesToCompileList.getSelectedIndex();
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
textarea.read.read(br, index);
br.close();
textarea.requestFocus();
but know i had to change the way i was loading the file in and i am currently loading the files in like
File file = new File(filePath);
textarea.setPage(file.toURI().toURL());
this is loading the files in the way i want but isn't highlighting the text for java files anymore! does anybody know how i can fix this or get java highlighting a different way?
AFAIK, it can not be done if you don't implement your own version of the jeditorpane. From the javadoc
The setPage method can be used to initialize the component from a URL.
In this case, the content type will be determined from the URL, and
the registered EditorKit for that content type will be set.
So, the mime type of the content will be inherit from the mime type of the url. Call setContentyType later will have no effect, as this will change the model of the jeditorpane, cleaning the content. Again from the Javadoc
NOTE: This has the side effect of changing the model, because the
EditorKit is the source of how a particular type of content is
modeled. This method will cause setDocument to be called on behalf of
the caller to ensure integrity of the internal state.
So you must keep using the read method.
Related
I am a beginner in java and I am building an android app.
I want to have an xml file that has text in it.
Whenever the server sends updates, I want to change some lines in that file (what I mean by update is changing some lines in that file by erasing the some part of the text written already and replace by the update)
I know nothing about creating,writing or reading from files.
When I searched I found out that Internal storage suits me best.
But I do not know if I have to create an xml file manually in any directory or just use the code bellow to create this file automatically?
// If this is the first time run,execute one time code
// create XML Internal store
String FILENAME = "My_XML_file";
try{
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
} catch (final IOException e) {
e.printStackTrace();
}
Thank you in advance!
- First give the External Storage permission in the Manifest.xml file.
- You can use JAXP & JAXB, and even CASTOR to handle XML in a better way, but still DOM and SAX are inbuilt into Android.
You can use something like this
String s = "/sdcard/Myfolder/mytext.txt";
File f = new File(s);
The code you have will create a file in internal storage but you need a bit more to create and maintain an XML file easily.
I suggest you use the Build in Android DOM Parser (Android developers site docs on XML Parse options)
I found this example which explains how to use the dom parser to build a specific (new) XML file from code. In your context where the output stream in created:
StreamResult result = new StreamResult(new File("C:\\file.xml"));
you might want to use the other constructor based on the output stream you created above
StreamResult result = new StreamResult(fos);
In a similar fashion this DOM library allows you to read from an input stream (which you might get from android openFileInput) using DocumentBuilder.parse()
I've a signed applet that retrieves a PDF document from a web service, then stores it on a temp folder, and opens it on Adobe Reader. I would like to avoid storing the file locally, but I really don't know how to achieve it (I'm a newbie with Java applets).
If it were a web application (i.e. a simple servlet), I could just write the PDF content over the ServletResponse; then the browser would store it on its temporary folder, and open it with Adobe Reader (or whatever application is associated with the MIME type).
Is there a similar way to do this... on a Java applet?
This is my code so far:
public class MyListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Retrieve the document contents
byte[] content = webService.getPdfDocument(...);
// Write to file
File f = new File("my-document-filename.pdf");
FileOutputStream fos = new FileOutputStream(f);
fos.write(content);
fos.close();
// Open the file
Desktop.getDesktop().open(new File("my-document-filename.pdf"));
}
}
Any alternative to Desktop.open(File), allowing me to pass a byte[] instead of a File?
Adobe reader can handle URL:s, so it could be a way forward to create a temporary (?) URL for the document.
Otherwise you can create a temporary file use File.createTempFile, from the API:
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:
The file denoted by the returned abstract pathname did not exist before this method was invoked, and
Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method.
So in your case, instead of creating a new file yourself you can use this method:
File f = File.createTempFile("tmp", ".pdf");
f.deleteOnExit(); // deletes the file on exit
...
today I was damaging my brain for too long on how I am able in Apache wicket to access the actual filecontent of a CssResourceReference?
The CSSResourceReference is declared like that:
private final CssResourceReference CSS_GLOBAL = new CssResourceReference(BasePage.class, "css/global.css");
...and is meant to be used for basic header contribution as well as it's contents need to be accessible from within a behaviour for feeding a LessCSSEngine.
I know that I am able to get the url (via the WicketWiki):
RequestCycle.get().urlFor(CSS_GLOBAL, null);
But from there on, I am stuck on how to actually access the file and contents.
Any help appreciated.
You can't access the content that easily because a ResourceReference in Wicket is meant to load by a Request. You can simulate a Request and read the response but this is too much work.
Anyways you don't need Wicket to access files in your class path:
final InputStream stream = BasePage.class.getResourceAsStream("css/global.css");
final Reader reader = new InputStreamReader(stream);
I want to implement a help system for my tiny SWT desktop application.
I've thought about a SWT browser widget containing a single html markup page and a set of anchors to navigate (there are only very few things to explain).
Everything works fine, but how do I load the html file from a jar?
I know aboutgetClass().getClassLoader().getResourceAsStream("foo");, but what is the best practice when reading from the input stream? The answer to Load a resource contained in a jar dissuades using a FileInputStream.
Thanks in advance
Well, I found a rather simple solution that obviously just works:
InputStream in = getClass().getClassLoader().getResourceAsStream("html/index.html");
Scanner scanner = new Scanner(in);
StringBuffer buffer = new StringBuffer();
while(scanner.hasNextLine()) {
buffer.append(scanner.nextLine());
}
browser.setText(buffer.toString());
i tend to use commons-io for such a task giving me simple abstraction methods like IOUtils.toString(InputStream in); and leaving the choice of best implementations to the able people at apache ;)
commons-io: http://commons.apache.org/io/
apidocs: http://commons.apache.org/io/api-release/index.html
I have some text configuration file that need to be read by my program. My current code is:
protected File getConfigFile() {
URL url = getClass().getResource("wof.txt");
return new File(url.getFile().replaceAll("%20", " "));
}
This works when I run it locally in eclipse, though I did have to do that hack to deal with the space in the path name. The config file is in the same package as the method above. However, when I export the application as a jar I am having problems with it. The jar exists on a shared, mapped network drive Z:. When I run the application from command line I get this error:
java.io.FileNotFoundException: file:\Z:\apps\jar\apps.jar!\vp\fsm\configs\wof.txt
How can I get this working? I just want to tell java to read a file in the same directory as the current class.
Thanks,
Jonah
When the file is inside a jar, you can't use the File class to represent it, since it is a jar: URI. Instead, the URL class itself already gives you with openStream() the possibility to read the contents.
Or you can shortcut this by using getResourceAsStream() instead of getResource().
To get a BufferedReader (which is easier to use, as it has a readLine() method), use the usual stream-wrapping:
InputStream configStream = getClass().getResourceAsStream("wof.txt");
BufferedReader configReader = new BufferedReader(new InputStreamReader(configStream, "UTF-8"));
Instead of "UTF-8" use the encoding actually used by the file (i.e. which you used in the editor).
Another point: Even if you only have file: URIs, you should not do the URL to File-conversion yourself, instead use new File(url.toURI()). This works for other problematic characters as well.