【发布时间】:2021-05-09 02:35:00
【问题描述】:
我正在制作一个应用程序,它将获取服务器日志并将其存储在最终用户的安卓手机中。我正在使用 InputStream 和 FileOutputStream 读取和写入文件,它们在模拟器的 data/data/
try{
Session session = new JSch().getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
InputStream inputStream = sftpChannel.get(remoteFile);
try (Scanner scanner = new Scanner(new InputStreamReader(inputStream))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
logs.append(line);
}
try {
FileOutputStream fos = null;
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(logs.getBytes());
Toast.makeText(this, "File Saved", Toast.LENGTH_SHORT).show();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch (JSchException | SftpException e) {
e.printStackTrace();
}
有什么办法,我可以在我的手机中查看这个文件而无需生根并给予一些权限或任何更好的选择?非常感谢您的投入。
【问题讨论】:
-
您将文件存储在应用专用存储中。因此它们是......仅对您的应用程序私有。
标签: android file android-emulator inputstream fileoutputstream