Reputation: 61
I'm trying to deserialize an XML file into an Object. My code fails on the line:
new FileInputStream(mySearch)
with a java.io.FileNotFoundException
.
So, I added the: if (exists) block to see if the file could be found another way.
I've googled the error and read other threads of discussion. Based on those discussions, I've tried FIVE different ways of specifying the file path and name. (See the commented out attempts)
The desired behavior is that I figure out what is wrong with my syntax for the path and file name, so that the file will be found and deserialized.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 5/20/2018 T. Pfaff - Added XMLDecoder Deserialization of XML file into EquationLib/LibSets/EquationSet
//===========================================================================================
//Below are the FIVE different ways that I've tried to get it to find the file
String myFileStr = "C:\\Users\\Terence\\Documents\\Meditate-Lemniscate\\Test_Single.xml";
//String myFileStr = "c:\\users\\terence\\documents\\meditate-lemniscate\\test_single.xml";
//String myFileStr = "C:/Users/Terence/Documents/Meditate-Lemniscate/Test_Single.xml";
//String myFileStr = "c:/users/terence/documents/meditate-lemniscate/test_single.xml";
//String myFileStr = "Test_Single.xml";
//===========================================================================================
String myEqSetName = "";
Integer myCnt = 0;
EquationLib equationLib = new EquationLib();
TextView myTV = (TextView) findViewById(R.id.myTextView);
//=============================================================================
//This block of code ALWAYS results in "Does NOT Exist"
//no matter which value I use for myFileStr
File mySearch = new File(myFileStr);
boolean exists = mySearch.exists();
if (exists) {
myTV.setText("YES EXISTS: " + myFileStr);
} else {
myTV.setText("Does NOT Exist: " + myFileStr);
}
//==============================================================================
XMLDecoder decoder=null;
// The first statement in the try block throws the exception:
//java.io.FileNotFoundException: C:\Users\Terence\Documents\Meditate-Lemniscate\Test_Single.xml (No such file or directory)
try{
FileInputStream myFileIS = new FileInputStream(mySearch);
decoder = new XMLDecoder(new BufferedInputStream(myFileIS));
equationLib = (EquationLib) decoder.readObject();
myCnt = equationLib.libSets.size();
myEqSetName = equationLib.libSets.get(0).equationSet.name;
} catch (Exception e){
e.printStackTrace();
}
//myTV.setText(myCnt.toString());
}
}
Upvotes: 0
Views: 276
Reputation: 51914
The Android emulator doesn't have access to the host's file system. You can bundle the XML file into your project_root\res\raw\
folder and then open it as a resource:
// Open project_root\res\raw\my_file.xml
InputStream stream = getResources().openRawResource(R.raw.my_file);
Or copy it onto the virtual device's storage and then open the local path:
adb push C:\Temp\my_file.xml /sdcard/my_file.xml
Upvotes: 2