error in trainingmodel in openNLP - java

Error in code:
If that train is removed then also show compiler error as below. I tried a lot to remove that and classify sentence as positive or negative.

An InputStreamFactory in not the same as an InputStream. Here is a simple bit of code that will create an InputStreamFactory for you. (You might say it is an InputStreamFactoryFactory :-) )
public static InputStreamFactory getInputStreamFactory(final File file) throws IOException{
return new InputStreamFactory() {
#Override
public InputStream createInputStream() throws IOException {
return new FileInputStream(file);
}
};
}
see javadocs: https://opennlp.apache.org/documentation/1.7.2/apidocs/opennlp-tools/opennlp/tools/util/InputStreamFactory.html

Related

Is there some standard effecient way to log data written to stream in Java

I'm writting tiny XMPP client as university project.
To generate valid XML markup I use XMLStreamWriter from standard library.
For debugging purposes I want to see all content that was written by it to output stream, i.e both send it via opened socket to the world and log it to stderr. One possible solution I found is to create subclasss of Writer class to construct new XMLStreamWriter.
Something like the following code:
public XMPPWriter(final Writer out) throws XMLStreamException {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
this.writer = factory.createXMLStreamWriter(new Writer() {
#Override
public void flush() throws IOException {
System.err.println("");
out.flush();
}
#Override
public void write(char[] cbuf, int off, int len) throws IOException {
System.err.write(String.valueOf(cbuf, off, len).getBytes());
out.write(cbuf, off, len);
}
#Override
public void close() throws IOException {
out.close();
}
});
}
Of course, it's the simplest case and this solution can be impoved in a lot of ways. But I'm wondering is there any standard well-known way to do such logging?

New to Java : Constructor error control

I'm new to Java and I'm porting one of my C++ libraries to Java as a learning experiment. This is not homework (as should be obvious from looking at my code). I have a few questions concerning the following code of my constructor for an ESRI shape file reader.
import java.io.*;
/**
*
* #author Bill
*/
public class ShapeFileReader {
public FileInputStream inStream;
/*
* #param fileName File name string. Must not be null or zero length.
* #throws Exception if file specified by fileName fails to open
*/
public ShapeFileReader(String fileName) throws IOException {
if(fileName == null)
throw new NullPointerException("fileName was null");
if(fileName.length() == 0)
throw new IllegalArgumentException("fileName string length was zero");
File fi = new File(fileName);
if(fi.exists() == false)
throw new IOException("File does not exist: " + fileName);
// Catch-or-specify (this method already throws IOException)
inStream = new FileInputStream(fileName);
}
}
During parameter validation and existence should I be throwing the exceptions as shown? The validation throws unchecked exceptions, and the existence throws checked exceptions. I assume that FileInputStream constructor will also throw an IOException, but I specified that in the method throws clause.
I was considering refactoring the opening of the file to a seperate function, but I figured it would be more useful and simple to do this in the constructor, and also learns me how to control errors here. Besides, any instance of this object will not have a closed/open state. All of these objects are reserved strictly for READING a file only, so they are created on a as-needed basis per file. I will provide a close() method seperately.
Also, from an extensibility point of view, would it be difficult to adapt to reading a file over a network using the FileInputStream with the current constructor? Or should I use a different class and multiple constructors?
Thanks for any and all input.
I wouldn't bother with the exceptions, FileInputStream will throw an exception for you, you're not adding anything other than clutter to your code.
For it to work with the network rather than just a file you'd modify thus:
public class ShapeFileReader {
private final InputStream inStream;
public ShapeFileReader(InputStream inStream) {
this.inStream = inStream;
}
/*
* #param fileName File name string. Must not be null or zero length.
* #throws Exception if file specified by fileName fails to open
*/
public ShapeFileReader(String fileName) throws IOException {
this(new FileInputStream(fileName));
}
Since this has been accepted as the answer I'm editing it as Roland (in the comments) is quite correct and this isn't how I'd have approached the problem.
public class ShapeReader {
public static Shape readShape(InputStream inStream) {
... do the work
}
/*
* #param fileName File name string. Must not be null or zero length.
* #throws Exception if file specified by fileName fails to open
*/
public static Shape readShape(String fileName) throws IOException {
FileInputStream fis = new FileInputStream(fileName);
try {
return readShape(fis);
} finally {
fis.close();
}
}
}

How to input filePath on FileReader

I have this code that Reads a file from the file path given.
I have hardcoded F://dom.txt. I need the user to input that filepath instead.
How should i do it? Thanks
import java.io.BufferedReader;
import java.io.FileReader;
public class BuffReader extends Converter {
private static BufferedReader br() throws FileNotFoundException{
return new BufferedReader(new FileReader("F://dom.txt")); //<--filepath
}
static String strTem;
public static String readData(String Message){
try{
System.out.print(Message);
strTem = br().readLine();
}catch(Exception e){
System.err.println("Muling tingan ang iyong numerong ibinigay");
}
return strTem;
}
}
Well you need to give the filename to the oddly-named br() method. For example:
private static BufferedReader br(String path) throws FileNotFoundException {
return new BufferedReader(new FileReader(path));
}
public static String readData(String message){
try{
System.out.print(message);
strTem = br(message).readLine();
}catch(Exception e){
System.err.println("Muling tingan ang iyong numerong ibinigay");
}
return strTem;
}
That's assuming the parameter to readData is actually the file you want to read from... otherwise, you'll need to work out where you are going to get the file name from.
(It would be a good idea to work on exception handling and naming, by the way.)
Assuming you mean the input should come from the console (it could also be command line parameter or a gui or whatever you like really) then you should be able to use System.console().readLine("prompt for input")
(assuming it is a standalone) Change your main method to read the variable args[0].
public static void main (String[] args) {
readData(Message, args[0]);
}
Then change the method signatures for readData() and so on. Basically the above code tells you how to read a string from command line.
If you are asking in the middle of the program:
You can use
System.console().readLine("Enter file name: )
to get the user input.
Also i would suggest to keep this file in a config file and read it from the file in order for the program to be flexible.

is there an existing FileInputStream delete on close?

Is there an existing way to have a FileInputStream delete the underlying file automatically when closed?
I was planning to make my own utility class to extend FileInputStreamand do it myself, but I'm kinda surprised that there isn't something already existing.
edit: Use case is that I have a Struts 2 action that returns an InputStream for file download from a page. As far as I can tell, I don't get notified when the action is finished, or the FileInputStream is not in use anymore, and I don't want the (potentially large) temporary files that are generated to be downloaded left lying around.
The question wasn't Struts 2 specific, so I didn't include that info originally and complicate the question.
There's no such thing in the standard libraries, and not any of the apache-commons libs either , so something like:
public class DeleteOnCloseFileInputStream extends FileInputStream {
private File file;
public DeleteOnCloseFileInputStream(String fileName) throws FileNotFoundException{
this(new File(fileName));
}
public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException{
super(file);
this.file = file;
}
public void close() throws IOException {
try {
super.close();
} finally {
if(file != null) {
file.delete();
file = null;
}
}
}
}
I know this is a fairly old question; however, it's one of the first results in Google, and Java 7+ has this functionality built in:
Path path = Paths.get(filePath);
InputStream fileStream = Files.newInputStream(path, StandardOpenOption.DELETE_ON_CLOSE);
There are a couple caveats with this approach though, they're written up here, but the gist is that the implementation makes a best effort attempt to delete the file when the input stream is closed, and if that fails makes another best effort attempt when the JVM terminates. It is intended for use with temp files that are used solely by a single instance of the JVM. If the application is security sensitive, there are also a few other caveats.
Can you can use File.deleteOnExit() before opening the file ?
EDIT: On you can subclass a FileInputStream that will delete the file on 'close()';
class MyFileInputStream extends FileInputStream
{
File file;
MyFileInputStream(File file) { super(file); this.file=file;}
public void close() { super.close(); file.delete();}
}
I know this is an old question, but I just ran into this issue, and found another answer: javax.ws.rs.core.StreamingOutput.
Here's how I used it:
File downloadFile = ...figure out what file to download...
StreamingOutput so = new StreamingOutput(){
public void write(OutputStream os) throws IOException {
FileUtils.copyFile(downloadFile, os);
downloadFile.delete();
}
ResponseBuilder response = Response.ok(so, mimeType);
response.header("Content-Disposition", "attachment; filename=\""+downloadFile.getName()+"\"");
result = response.build();

Append data into a file using Apache Commons I/O

The FileUtils.writeStringToFile(fileName, text) function of Apache Commons I/O overwrites previous text in a file. I would like to append data to my file. Is there any way I could use Commons I/O for the same? I can do it using normal BufferedWriter from Java but I'm curious regarding the same using Commons I/O.
It has been implemented in 2.1 version of Apache IO.
To append string to the file just pass true as an additional parameter in functions:
FileUtils.writeStringToFile
FileUtils.openOutputStream
FileUtils.write
FileUtils.writeByteArrayToFile
FileUtils.writeLines
ex:
FileUtils.writeStringToFile(file, "String to append", true);
Download the latest version Commons-io 2.1
FileUtils.writeStringToFile(File,Data,append)
set append to true....
Careful. That implementation seems to be leaking a file handle...
public final class AppendUtils {
public static void appendToFile(final InputStream in, final File f) throws IOException {
OutputStream stream = null;
try {
stream = outStream(f);
IOUtils.copy(in, stream);
} finally {
IOUtils.closeQuietly(stream);
}
}
public static void appendToFile(final String in, final File f) throws IOException {
InputStream stream = null;
try {
stream = IOUtils.toInputStream(in);
appendToFile(stream, f);
} finally {
IOUtils.closeQuietly(stream);
}
}
private static OutputStream outStream(final File f) throws IOException {
return new BufferedOutputStream(new FileOutputStream(f, true));
}
private AppendUtils() {}
}
this little thingy should do the trick:
package com.yourpackage;
// you're gonna want to optimize these imports
import java.io.*;
import org.apache.commons.io.*;
public final class AppendUtils {
public static void appendToFile(final InputStream in, final File f)
throws IOException {
IOUtils.copy(in, outStream(f));
}
public static void appendToFile(final String in, final File f)
throws IOException {
appendToFile(IOUtils.toInputStream(in), f);
}
private static OutputStream outStream(final File f) throws IOException {
return new BufferedOutputStream(new FileOutputStream(f, true));
}
private AppendUtils() {
}
}
edit: my eclipse was broken, so it didn't show me the errors earlier. fixed errors
Actually, version 2.4 of apache-commons-io FileUtils now has append mode for collections as well.
Here's the Javadoc
And the maven dependency:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<type>jar</type>
</dependency>
in version 2.5 you need to pass one extra parameter i.e, encoding.
FileUtils.writeStringToFile(file, "line to append", "UTF-8", true);
public static void writeStringToFile(File file,
String data,
boolean append)
throws IOException
Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
Parameters:
file - the file to write to
lines - the lines to write, null entries produce blank lines
append - if true, then the lines will be added to the end of the file rather than overwriting
Throws:
IOException - in case of an I/O error
Since:
Commons IO 2.1

Categories