android 文件读取错误,Android源文件从SD卡读取错误问题,怎么处理
/**
* 保存文件
* @param toSaveString
* @param filePath
*/
public static void saveFile(String toSaveString, String filePath)
{
try
{
File saveFile = new File(filePath);
if (!saveFile.exists())
{
File dir = new File(saveFile.getParent());
dir.mkdirs();
saveFile.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(saveFile);
outStream.write(toSaveString.getBytes());
outStream.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 讀取文件內容
* @param filePath
* @return 文件內容
*/
public static String readFile(String filePath)
{
String str = "";
try
{
File readFile = new File(filePath);
if(!readFile.exists())
{
return null;
}
FileInputStream inStream = new FileInputStream(readFile);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
while ((length = inStream.read(buffer)) != -1)
{
stream.write(buffer, 0, length);
}
str = stream.toString();
stream.close();
inStream.close();
return str;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
取消
評論
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的android 文件读取错误,Android源文件从SD卡读取错误问题,怎么处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 相互宝赔付30万到账多少
- 下一篇: android webview load