【发布时间】:2014-10-19 21:13:59
【问题描述】:
我正在尝试读取从 arduino 服务器下载的 txt 文件。我可以下载文件并将其保存在内部存储中,但是当我想阅读它时,我看到奇怪的符号并且我看不到所有保存的文本,只有 6 或 7 个字符。我不知道会发生什么。
public void downloadFile (String fileDownloadPath, String saveFilePath){
try{
File SaveFile = new File(saveFilePath);
URL u= new URL ("http://169.254.0.1:44300/169.254.0.1");
URLConnection con=u.openConnection();
int lengthofContent=con.getContentLength();
DataInputStream DIStream = new DataInputStream(u.openStream());
byte [] buffer = new byte[2000];
DIStream.read(buffer);
DIStream.close();
DataOutputStream DOStream = new DataOutputStream(new FileOutputStream(SaveFile));
DOStream.write(buffer);
DOStream.flush();
DOStream.close();
System.out.println ("o");
hideProgressIndicator();
} catch (FileNotFoundException e){
hideProgressIndicator();
}
catch (IOException e){
hideProgressIndicator();
}
catch (Exception e){
}
}
当我想阅读它时,我会使用它:
private String readFile() {
String result = "", line;
try
{
BufferedReader br = new BufferedReader((new FileReader("/data/data/com.example.sensolog/files/LOL.txt")));
while((line = br.readLine()) != null)
{
result += line + "\n";
}
System.out.println (result);
br.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return result ;
}
结果如下:
08-26 15:38:11.498: I/System.out(30593): SERVIDORn���������������������������� ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ……
【问题讨论】:
-
您是否使用正确的编码阅读?
-
你知道文本文件原来的编码是什么吗?
-
(我也有类似的问题:stackoverflow.com/questions/24106734/…)