I’m trying to reference a file stored in my working directory, so it can be read in and parsed using the open source routines ‘opencsv’.
The following line gives a FileNotFoundException error:
CSVReader reader = new CSVReader(new FileReader("file.csv"), '\t');
I’ve checked the working directory using the following:
String curDir = System.getProperty("user.dir");
This returns the value ‘\’, so I have placed the file ‘file.csv’ in the root directory of my project. I am using Eclipse. The file in question is not read only, is set to ‘Archive’ and is not labeled ‘Derived’.
I am, probably quite obviously, new to Java. Searching for this issue on Google suggests I need to change my file permissions, but I wonder whether I’m missing something else. I know the solution is probably painfully obvious :S
Here is the code in full:
package mjd.listview.test;
import java.io.FileReader;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import au.com.bytecode.opencsv.CSVReader;
public class ListProjectActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CSVReader reader = new CSVReader(new FileReader("file.csv"), '\t');
List<String[]> myEntries = reader.readAll();
String[] terms = getResources().getStringArray(R.array.terms_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, terms));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
Don’t assume the root directory of your project is the equivalent of
\. I’d be surprised if it were. What’s the actual path reported in theFileNotFoundException? That’ll tell you where Java is looking for the file.I use IntelliJ and not Eclipse, but in IntelliJ there’s a run configuration screen where you set the JVM parameters, the command-line arguments, and the working directory. I assume Eclipse has something similar. See what it’s set to. Then change it to wherever you actually have the file or move the file to what it’s set to.