При загрузке файла .csv с помощью ftp на Android я получаю ошибку FileNotFoundException при первом выполнении.(Без проблем со второго)
Я думаю, что ошибка при попытке открыть файл перед его полной загрузкой.Но я не знаю, как решить проблему.Как я могу это исправить?ToT
Используемая библиотека: commons-net-3.6.jar (https://commons.apache.org/proper/commons-net/download_net.cgi)
LoadingActivity.java
public class LoadingActivity extends AppCompatActivity {
static int[] imageId;
static int[] allImageId;
static JSONObject object; // final obj
JSONArray array; // datainfo > json
JSONObject dataInfo; //
ArrayList<Data> dataList;
static JSONObject allObject; // final obj
JSONArray allArray; // datainfo > json
JSONObject allDataInfo; //
ArrayList<AllData> allDataList;
Intent intent;
String[] FileName = {"first.csv", "second.csv"};
String FileExtend = ".csv";
String fileURL = "http://ipaddress/ftpaccount/ftp/files/";
String SavePath;
String SaveFolder = "/files";
DownloadThread mThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
SavePath = getFilesDir().getPath() + SaveFolder;
setImageResource();
SharedPreferences pref = getSharedPreferences("isFirst", Activity.MODE_PRIVATE);
boolean isFirst = pref.getBoolean("isFirst", false);
if (isFirst == false) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isFirst", true);
editor.commit();
// first run
for (int i = 0; i < FileName.length; i++) {
File dir = new File(SavePath);
if (!dir.exists()) {
dir.mkdir();
}
mThread = null;
if (new File(SavePath + "/" + FileName[i]).exists() == false) {
mThread = new DownloadThread(fileURL + "/" + FileName[i], SavePath + "/" + FileName[i]);
mThread.start();
}
}
}
readData();
readAllData();
intent = new Intent(this, MainActivity.class);
intent.putExtra("cardData", dataList);
intent.putExtra("cardAllData", allDataList);
// startActivity(intent);
// finish();
}
public void readData() {
object = new JSONObject();
array = new JSONArray();
dataList = new ArrayList<>();
try {
FileInputStream is = new FileInputStream(SavePath + "/" + FileName[0]);
while(is != null) {
}
InputStreamReader isr = new InputStreamReader(is);
CSVReader reader = new CSVReader(isr, ',', '"', 0);
String[] data;
int i = 0;
while ((data = reader.readNext()) != null) {
dataInfo = new JSONObject();
dataInfo.put("cardNo", data[0]);
dataInfo.put("cardName", data[1]);
dataInfo.put("cardEngName", data[2]);
dataInfo.put("keyword", data[3]);
dataInfo.put("comment1", data[4]);
dataInfo.put("comment2", data[5]);
dataInfo.put("comment3", data[6]);
dataInfo.put("comment4", data[7]);
dataInfo.put("comment5", data[8]);
dataInfo.put("comment1_rev", data[9]);
dataInfo.put("comment2_rev", data[10]);
dataInfo.put("comment3_rev", data[11]);
dataInfo.put("comment4_rev", data[12]);
dataInfo.put("comment5_rev", data[13]);
Data cardData = new Data(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8],
data[9], data[10], data[11], data[12], data[13]);
dataList.add(cardData);
array.put(i, dataInfo);
i++;
}
object.put("data", array);
if (reader != null)
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
class DownloadThread extends Thread {
String ServerUrl;
String LocalPath;
DownloadThread(String serverPath, String localPath) {
ServerUrl = serverPath;
LocalPath = localPath;
}
@Override
public void run() {
URL url;
int Read;
try {
url = new URL(ServerUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int len = conn.getContentLength();
byte[] tempByte = new byte[len];
InputStream is = conn.getInputStream();
File file = new File(LocalPath);
FileOutputStream os = new FileOutputStream(file);
for (; ; ) {
Read = is.read(tempByte);
if (Read <= 0) {
break;
}
os.write(tempByte, 0, Read);
}
is.close();
os.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
afterDown.sendEmptyMessage(0);
}
Handler afterDown = new Handler() {
@Override
public void handleMessage(Message msg) {
// progress
}
};
}
public void setImageResource() {
imageId = new int[]{R.drawable. ... };
allImageId = new int[]{R.drawable. ... };
}
и журнал ошибок Logcat
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: java.io.FileNotFoundException: /data/user/0/com.example.testapp/files/files/first.csv (No such file or directory)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at java.io.FileInputStream.open(Native Method)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:146)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:99)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at com.example.tarot.LoadingActivity.readData(LoadingActivity.java:130)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at com.example.tarot.LoadingActivity.onCreate(LoadingActivity.java:116)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.app.Activity.performCreate(Activity.java:6679)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.os.Looper.loop(Looper.java:154)
2019-03-27 17:57:18.884 16755-16755/com.example.testapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
2019-03-27 17:57:18.885 16755-16755/com.example.testapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-03-27 17:57:18.885 16755-16755/com.example.testapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
2019-03-27 17:57:18.885 16755-16755/com.example.testapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)