I am trying to serialize a DoccatModel Object in Java (I am using OpenNLP algorithm).
It's able to serialize the object properly and I am writing that to an external file. But, I'm not able to deserialise the object back.
I am trying to use ObjectInputStream to read that file back. It's throwing an error:
"java.io.StreamCorruptedException: invalid stream header: 504B0304"
I just want to know how to de serialize the DoccatModel object back so that I can use it further.
The code is as follows. [Apologies for bad code since its still in development stage]
private static void runDocCat(String textField, String updateField, String tgtField)
{
String dbName;
String tableName;
DoccatModel model = null;
JSONArray allDesc = null;
DataConfig BSC = null;
try
{
BSC = new DataConfig();
}
catch (Exception e1)
{
e1.printStackTrace();
}
try
{
dbName = BSC.getdbName();
tableName = BSC.tablename;
String dataQuery = "SELECT ID, VOC, " + tgtField + " from " + dbName + "." + tableName;
allDesc = BSC.getJSONArray(dataQuery);
File file = new File("voc.train");
BufferedWriter output = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < allDesc.length(); i++)
{
String tgt = allDesc.getJSONObject(i).getString(tgtField);
if (tgt.length() < 3)
tgt = "Unknown";
tgt = tgt.replaceAll(" ", "");
String desc = allDesc.getJSONObject(i).getString(textField);
desc = desc.replaceAll("\\r", " ").replaceAll("\\n", ".");
if (!desc.trim().equalsIgnoreCase("nothing"))
{
DocumentSample currDoc = new DocumentSample(tgt, desc);
output.write(currDoc.toString());
output.newLine();
}
}
output.close();
System.out.println("Training Data Generated!");
ObjectStream<String> lineStream = new PlainTextByLineStream(new FileReader(file));
ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);
model = DocumentCategorizerME.train("en", sampleStream);
System.out.println("Model Data \n\n" + model);
// Write to a file
OutputStream modelOut = null;
try
{
String modelfile = "C:\\VOC_Classification\\model.ser";
modelOut = new BufferedOutputStream(new FileOutputStream(modelfile));
model.serialize(modelOut);
System.out.println("Model Data \n\n" + model);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (modelOut != null)
try
{
modelOut.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
// try
// {
// FileOutputStream fileOut = new FileOutputStream("modelfile.ser");
// ObjectOutputStream out = new ObjectOutputStream(fileOut);
// out.writeObject(model);
// out.close();
// fileOut.close();
// System.out.printf("Serialized data is saved in modelfile.ser");
// } catch (IOException i)
// {
// i.printStackTrace();
// }
DoccatModel model_SER = null;
try
{
FileInputStream fileIn = new FileInputStream("C:\\VOC_Classification\\model.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
model_SER = (DoccatModel) in.readObject();
in.close();
fileIn.close();
} catch (IOException e)
{
e.printStackTrace();
}
System.out.println("\nTraining Done!\n");
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model);
System.out.println(" ---------- Starting categorising process ----------");
System.out.println("\nProcess Running, Please wait...\n");
for (int i = 0; i < allDesc.length(); i++)
{
dataQuery = "SELECT ID, VOC," + tgtField + " from " + dbName + "." + tableName;
allDesc = BSC.getJSONArray(dataQuery);
String ID = allDesc.getJSONObject(i).getString("ID");
String desc = allDesc.getJSONObject(i).getString(textField);
String newdesc = desc.replaceAll("\\n", ".").replaceAll("\\r", " ");
if (!newdesc.trim().equalsIgnoreCase("nothing"))
{
double[] outcomes = myCategorizer.categorize(newdesc);
String category = myCategorizer.getBestCategory(outcomes);
if (!category.equalsIgnoreCase("Unknown"))
{
String updQuery = "UPDATE " + dbName + "." + tableName + " set " + updateField + " = " + "'"
+ category + "'" + " WHERE ID = " + ID;
BSC.executeUpdate(updQuery);
}
}
}
System.out.println(" ---------- Process Completed Successfully ----------");
System.out.println("\nCheck your table \"" + tableName + "\" for results");
System.out.print("\n\nPress 1 to finish : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int ch = Integer.parseInt(br.readLine());
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
BSC.connectionClose();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The OpenNLP documentation is unclear, but there is a simple way of doing this.
private DoccatModel deSerializeModel(String modelPath) throws
IOException {
FileInputStream fileInputStream = new FileInputStream(modelPath);
DoccatModel model = new DoccatModel(fileInputStream);
return model;
}
Related
Being a beginner in programing I'm actually writing a program to practice, wich use url.open to download pages on a forum but after 88-90 loops the url.openstream became very slow and slow down all the program's execution and i don't know why. i searched on internet the explanations but not found it.
private Set<utilisateur> arbretemp = new HashSet<utilisateur>();
private ArrayList<String> arbretempString = new ArrayList<String>();
private ArrayList<utilisateur> tempArray = new ArrayList<utilisateur>();
private String lienPartie1;
private String lienPartie2;
private int nbrePage;
private String nom;
--
String temporaire = "";
for (int i = 1; i <= nbrePage; i++) {
String line = "";
String lien = lienPartie1 + i + lienPartie2;
bar.setValue(i);
try {
url = new URL(lien);
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
System.out.println("avant while");
while (((line = br.readLine()) != null)) {
System.out.println("debut de while");
if (((line.contains("alt=")) && line.contains("class=\"user-avatar-msg\""))) {
line = line.trim();
String[] temp = line.split("\"");
temporaire = temp[5] + " \r";
temp = temporaire.split("\"");
byte[] test = temporaire.getBytes();
if (!(temporaire.equalsIgnoreCase(""))) {
utilisateur utiltemp = new utilisateur(temporaire);
if (arbretemp.add(utiltemp)) {
} else {
arbretempString.add(temporaire);
}
}
}
}
System.out.println("fin de while");
} catch (MalformedURLException mue) {
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
String s = "page " + i + "/" + nbrePage;
}
System.out.println("fin de boucle");
Iterator it = arbretemp.iterator();
for (utilisateur a : arbretemp) {
// System.out.println(a.nom);
a.ajouterNbrePost(arbretempString);
}
TreeSet<utilisateur> salComp = new TreeSet<utilisateur>(new utilisateurComp());
salComp.addAll(arbretemp);
tempArray.addAll(arbretemp);
tempArray.sort(new utilisateurComp());
}
as you see it's in french but i hope you will understand anyway.
thanks for your reply and reading
I'm trying to adapt my code to Sonar and I don't get why I can't correct this blocker.
Its a 'Close this FileInputStream' with this starting code:
BufferedReader localBufferedReader = null;
try {
localBufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(inputFile),"UTF-8")); //THIS INPUTSTREAM!!
String line;
//
// Fill Hashmap
HashMap hmVar = (HashMap) this.context.getAttribute(this.hmVarName);
if (hmVar == null)
hmVar = new HashMap();
String[] props = new String[2];
while ((line = localBufferedReader.readLine()) != null) {
//Split line into key, value
if(line.startsWith("#"))
continue;
props = line.split("=");
hmVar.put(props[0], props[1]);
}
this.context.setAttribute(this.hmVarName, hmVar);
} catch (FileNotFoundException localException2) {
this.context.logError("Unable to find file: " + inputFile);
localException2.printStackTrace();
throw new WFException("Unable to find file: " + inputFile);
} catch (Exception localException4) {
this.context.logError("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
localException4.printStackTrace();
throw new WFException("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
} finally {
try {
if (localBufferedReader != null) {
localBufferedReader.close();
}
} catch (Exception localException5) {
}
}
So, ok. I declared apart the fileinputstream and go on, but it still doesn't like the way I close it.
BufferedReader localBufferedReader = null;
FileInputStream fis = null;
try {
//StringBuffer localStringBuffer = new StringBuffer();
fis = new FileInputStream(inputFile);
//localBufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(inputFile),"UTF-8")); SONAR correction FileInputStream needs to be closed
localBufferedReader = new BufferedReader( new InputStreamReader( fis,"UTF-8"));
String line;
//
// Fill Hashmap
HashMap hmVar = (HashMap) this.context.getAttribute(this.hmVarName);
if (hmVar == null)
hmVar = new HashMap();
String[] props = new String[2];
while ((line = localBufferedReader.readLine()) != null) {
//Split line into key, value
if(line.startsWith("#"))
continue;
props = line.split("=");
hmVar.put(props[0], props[1]);
}
this.context.setAttribute(this.hmVarName, hmVar);
} catch (FileNotFoundException localException2) {
this.context.logError("Unable to find file: " + inputFile);
localException2.printStackTrace();
throw new WFException("Unable to find file: " + inputFile);
} catch (Exception localException4) {
this.context.logError("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
localException4.printStackTrace();
throw new WFException("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
} finally {
try {
if (localBufferedReader != null) {
localBufferedReader.close();
}
if (fis != null) {
fis.close();
}
} catch (Exception localException5) {
}
}
Any ideas? I'm closing it the same way the BufferedReader that returns no issue.
Streams Readers and Writers implement the Closable interface. So you can get rid of the sonar warning if you use the try-with-resource construct:
try ( BufferedReader localBufferedReader = new BufferedReader( ...))
{
...
}
Any allocated Closable in the try (..) will be closed automatically if you leave the try block.
I have 2 almost identical methods. But 1 can see my file , another not.It skips the method body with line = null. Dont understand why.
This doesnt work
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFileUpload(#RequestParam("file") MultipartFile file, Model model) {
if (!file.isEmpty()) {
try {
String path = "d:/" + file.getOriginalFilename();
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(path)));
stream.write(bytes);
model.addAttribute("list", addRecordsToBD(path));
stream.close();
} catch (Exception e) {
log.error("Upload failed", e);
}
}
return "show";
}
private List addRecordsToBD(String path) {
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
while ((line = reader.readLine()) != null) {
String[] users = line.split(",");
repository.saveAndFlush(new User(users[0], users[1], users[2],
users[3], users[4]));
}
} catch (IOException e) {
System.out.println("Input problems");
}
System.out.println("Inserting done");
return repository.findAll();
}
But this work great
public void addRecordsToBD(String path) {
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
while ((line = reader.readLine()) != null) {
String[] users = line.split(",");
System.out.println("User [Name = " + users[0] +
", surname = " + users[1] +
" login = " + users[2] +
" mail = " + users[3] +
" phone = " + users[4] +
"]");
}
} catch (IOException e) {
System.out.println("Input problems");
}
System.out.println("Inserting done");
}
}
Can some one explain me, what's the problem and why this happened ?
I have this problem in my program:
When I do this:
double a = Double.parseDouble(zero);
double b = Double.parseDouble(data);
double c = a+b;
String d = Double.toString(c);
It returns me a String. For example, I have 0.0 in a and 4.0 in b and it returns 0.04.0.
Can someone explain to me why and what I should do instead?
EDITED: Here is my code:
private void WriteMedia(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("writemedia.txt", Context.MODE_PRIVATE));
String zero;
if(ReadMedia().isEmpty() == true)
{
zero = "0.0";
outputStreamWriter.write(zero);
}
else {zero = ReadMedia();}
double a = Double.parseDouble(zero);
double b = Double.parseDouble(data);
double c = a+b;
String d = Double.toString(c);
outputStreamWriter.write(d);
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
private String ReadMedia() {
String ret = "";
try {
InputStream inputStream = openFileInput("writemedia.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("estrela.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
WriteMedia(data);
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput("estrela.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
WriteMedia(ret);
return ret;
}
The data is a ratingbar, zero is "0" when I start the app and it should save the rating bar to "increment"(don't know the english word). I thought it would be easy but since is like a bug just for me i'll put the tag android studio.
Here is where I call the method:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rating = ratingbar.getRating();
writeToFile(Double.toString(rating));
writeMyArray(Double.parseDouble(readFromFile()));
writeMyArray(ratingbar.getRating());
button.setText(getText(R.string.obrigado) + "!" + readFromFile() + " " + media(arraydays) + " " + ReadMedia());
}
});
It returns me the "0.0" and the ratingbar together, for example: "0.05.0"
Your problem is not related to types (other than you guessed) and it is not in the code you originally posted.
Instead, you are simply writing 2 numbers to your output stream. The following code writes 0.0:
if(ReadMedia().isEmpty() == true)
{
zero = "0.0";
outputStreamWriter.write(zero);
}
And, in addition to that, the following code writes the result you actually want:
double a = Double.parseDouble(zero);
double b = Double.parseDouble(data);
double c = a+b;
String d = Double.toString(c);
outputStreamWriter.write(d);
client server chat system as server start the server and client connect to the server..when client tries to connect to server, it success but the array goes to lost connection condition.
catch (Exception ex) {
outputTextArea.append("Lost a connection. \n");
ex.printStackTrace();
clientOutputStreams.remove(client);
anyway to fix it?
Server side code
public ClientHandler(Socket clientSocket, PrintWriter user) {
// new inputStreamReader and then add it to a BufferedReader
client = user;
try {
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} // end try
catch (Exception ex) {
outputTextArea.append("Error beginning StreamReader. \n");
} // end catch
} // end ClientHandler()
public void run() {
String message, connect = "Connect", disconnect = "Disconnect", chat = "Chat" ;
String[] data;
try {
while ((message = reader.readLine()) != null) {
outputTextArea.append("Received: " + message + "\n");
data = message.split(":");
for (String token:data) {
outputTextArea.append(token + "..\n");
}
if (data[2].equals(connect)) {
tellEveryone((data[0] + ":" + data[1] + ":" + chat));
userAdd(data[0]);
} else if (data[2].equals(disconnect)) {
tellEveryone((data[0] + ":has disconnected." + ":" + chat));
userRemove(data[0]);
} else if (data[2].equals(chat)) {
tellEveryone(message);
} else {
outputTextArea.append("No Conditions were met. \n");
}
} // end while
} // end try
catch (Exception ex) {
outputTextArea.append("Lost a connection. \n");
ex.printStackTrace();
clientOutputStreams.remove(client);
} // end catch
} // end run()
public void userAdd (String data) {
String message, add = ": :Connect", done = "Server: :Done", name = data;
outputTextArea.append("Before " + name + " added. \n");
userlist.add(name);
outputTextArea.append("After " + name + " added. \n");
String[] tempList = new String[(userlist.size())];
userlist.toArray(tempList);
for (String token:tempList) {
message = (token + add);
tellEveryone(message);
}
tellEveryone(done);
}
public void userRemove (String data) {
String message, add = ": :Connect", done = "Server: :Done", name = data;
userlist.remove(name);
String[] tempList = new String[(userlist.size())];
userlist.toArray(tempList);
for (String token:tempList) {
message = (token + add);
tellEveryone(message);
}
tellEveryone(done);
}
public void tellEveryone(String message) {
// sends message to everyone connected to server
Iterator it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength());
} // end try
catch (Exception ex) {
outputTextArea.append("Error telling everyone. \n");
} // end catch
} // end while
} // end tellEveryone()
errors
java.lang.ArrayIndexOutOfBoundsException: 2
at chatsystemserver.ServerSide$ClientHandler.run(ServerSide.java:55)
at java.lang.Thread.run(Thread.java:745)
any idea how to fix the data[2]?
Check if the array has at least 3 elements. That is, change
if (data[2].equals(connect)) {
to
if (data.length > 2 && data[2].equals(connect)) {