I am getting the occassional error message when I try to read a serialized object from a file. It works fine 9 times out of 10, but for some reason I get lots of these error message sin the catlog:
06-01 23:57:50.824: ERROR/MemoryFile(16077): MemoryFile.finalize() called while ashmem
still open
and
06-01 23:57:57.664: ERROR/MemoryFile(16077): java.io.IOException: munmap failed
The second message comes with no indication where the exception is caused. (Clearly when I'm loading the file, but I already have a try/catch around it.)
My loadfile method looks like this:
public TGame loadSavedGame(){
TGame g=null;
InputStream instream = null;
BufferedReader br=null;
InputStreamReader inputreader=null;
try {
File sdCard = Environment.getExternalStorageDirectory();
instream = new
FileInputStream(sdCard.getAbsolutePath()+"/egyptica/serializationtest");
// inputreader = new InputStreamReader(instream);
// br= new BufferedReader(inputreader);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
try {
ObjectInputStream ois = new ObjectInputStream(instream);
try {
g= (TGame) ois.readObject();
try {
instream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return g;
} catch (ClassNotFoundException ex) {
// TODO Auto-generated catch block
android.util.Log.e("DESERIALIZATION FAILED (CLASS NOT
FOUND):"+ex.getMessage(), "ex");
ex.printStackTrace();
return null;
}
} catch (StreamCorruptedException ex) {
android.util.Log.e("DESERIALIZATION FAILED (CORRUPT):"+ex.getMessage(),
"ex");
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
} catch (IOException ex) {
android.util.Log.e("DESERIALIZATION FAILED (IO
EXCEPTION):"+ex.getMessage(), "ex");
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
}
}
One possibility I have thought of is using a BufferedReader to rea the file. However I'm not sure how to go about doing this. Any help would be appreciated.
Try to put finally block after try and put there closing statements for your streams and also useful thing is to use:
FileInputStream.getFD().sync();
It makes sure that file really received your close/flush
Related
I am writing a program in which I am taking input from user using BufferedReader object and then catching the IOException thrown by BufferedReader object in catch block but still when I am closing the resources in finally block then why is it saying to surround with try/catch or declare IOException
Here is the program
public class BufferedReader3 {
public static void main(String[] args){
// TODO Auto-generated method stub
InputStreamReader r=null;
BufferedReader br=null;
String name="";
try
{
r =new InputStreamReader(System.in);
br =new BufferedReader(r);
while(!name.equals("stop"))
{
System.out.println("Enter data: ");
name=br.readLine();
}
System.out.println("data is: "+name);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
br.close();
r.close();
}
}
}
The close method can throw an IOException for the following reason (source):
If an I/O error occurs
Which is why the IOException which the close method might throw also needs to be caught, since it's in the finally block it's not within your already existing try catch block:
finally {
try {
br.close();
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I'm trying to modify a doc with Apache POI in Java.
At first the test.doc cannot be read with a exception raised up :
"org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x6576206C6D783F3C, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
"
So I saved the doc as "word 97 - 03" format,and then POI can read the doc properly.
But when I try to rewrite the content to a new file with nothing changed, the file output.doc cannot be opened by MS Office.
When I make a new doc myself with MS Office, the POI works well, everything goes right.
So the problem is "test.doc".
The test.doc is generated by some sort of a program which I can't access the code,so I don't know what goes wrong.
My question is :
1.As test.doc can be read by MS Office why can't POI without saving as a new format doc?
2.As the POI can read the doc, why it cannot write back to a new file(MS Office can't open)?
Here is my code:
FileInputStream fis = null;
try {
fis = new FileInputStream("test.doc");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
POIFSFileSystem pfs = null;
try {
pfs = new POIFSFileSystem(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HWPFDocument hwdf = null;
try {
hwdf = new HWPFDocument(pfs);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("output.doc"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
hwdf.write(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pfs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The HEX stuff read as ASCII and read little-endian converts to <?xml ve, which indicates that test.doc is some other format than actually .doc/.docx.
Word will open other data-formats gracefully sometimes, upon saving it will be saved correctly in the Word-Format.
Therefore you will need to use a hex-editor to take a look at the contents of test.doc and if it is really in some broken format you need to find out where it is coming from and how the creation of that file can be fixed.
I want to write a matrix to a file in java by using jama library.However, only a empty file is produced. I am using the code below. What can be wrong?
PrintWriter writer = null;
try {
writer = new PrintWriter("deneme.txt", "UTF-8");
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m3.print( writer,2,2);
writer could be null when it comes to this line of code. Move the m3.print(writer,2,2); inside of the try block.
PrintWriter writer = null;
try {
writer = new PrintWriter("deneme.txt", "UTF-8");
m3.print(writer, 2, 2);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am trying to convert large 3gp file(>than 25mb) to byte array but it gives outofmemory exception.i am able to convert less than 25 mb 3gp file to bytearray.
File file1 = new File (Environment.getExternalStorageDirectory() + "1.3gp");
FileInputStream fis = null;
try {
fis = new FileInputStream(file1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while (fis.available() > 0) {
bos.write(fis.read());
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bytes = bos.toByteArray();
File someFile = new File (Environment.getExternalStorageDirectory()+ "/output.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(someFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
////
how to convert the large 3gp file into bytearray.
give a proper example or method.
Set the -Xmx option on the virtual machine being used to run the program to a larger value to give it more memory to work with.
You can do that as a command line option if running the program directly, or as a setting on the project in your IDE if running it from an IDE.
This question already has answers here:
Problem with "scopes" of variables in try catch blocks in Java
(3 answers)
Closed last year.
I have omitted irrelevant parts of the code:
[...]
try {
URL url = new URL(updateUrl);
BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
} finally {
input.close();
}
[...]
The problem is that on the finally "input.close()" Eclipse says that "input cannot be resolved".
I think it may be an scope problem, but I have seen code from other guys and it has usually this same form, so I do not know why it is not working here.
Any hints?
Thanks a lot in advance,
It is indeed a scope error.
Your input is declared inside the try block, so it can't be seen inside the finally block. Declare it outside, so that it is visible to both, and you should be fine:
[...]
BufferedReader input = null;
try {
URL url = new URL(updateUrl);
input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
} finally {
if (input != null)
{
try {
input.close();
}
catch (IOException exc)
{
exc.printStackTrace();
}
}
}
[...]
declare BufferedReader input instance globally or outside first try/catch block as:
[...]
BufferedReader input;
try {
URL url = new URL(updateUrl);
input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
} finally {
input.close();
}
[...]
You're right, it is a scope problem. Java uses block scope, which means local variables declared in one scope are invisible in any scope that is not contained within it. try blocks and finally blocks are not exceptions to this rule.
BufferedReader input;
try {
URL url = new URL(updateUrl);
input = new BufferedReader(new InputStreamReader(url.openStream()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
// Log or ignore this
}
}
}