我想从服务器上的私人文件夹中下载文本文件,该文件不应为用户所访问。简而言之,我只想将该文件存储在“应用程序文件夹”中,而不是存储在存储卡/内部存储器中。下载完成后,我想在文本视图中显示文本文件。如果文件已经下载或存在于文件夹中,则只想显示该文件内容。有谁可以帮助我吗?预先感谢。
这是代码-
url = new URL("https://tmpfiles.org/dl/11433/baby_shark_dodo.txt");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.connect();
connection.setRequestMethod("GET");
try {
File mydir = getApplicationContext().getDir("Songs", Context.MODE_PRIVATE);
if(!mydir.exists()){
mydir.mkdir();
}
final File file = new File(mydir,"Test.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
connection.disconnect();
} catch (FileNotFoundException e) {
e.printStackTrace();
}