文件保存问题

weixin_41545972 2018-01-01 07:49:20
我碰上了一个我解决不了的文件读取和保存方面的问题,请大家帮我看一下
代码
public void save__OnClike(View view) {
try {
FileOutputStream out = openFileOutput("xyxdata.txt", MODE_PRIVATE);
String content = TV.getText().toString();

// byte[] bytes = content.getBytes();

out.write(content.getBytes());
out.flush();
out.close();

} catch (IOException e) {

}
}

public void read_text() {
try {
FileInputStream inputStream = this.openFileInput("xyxdata.txt");
byte[] bytes = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while (inputStream.read(bytes) != -1) {
arrayOutputStream.write(bytes, 0, bytes.length);
}
inputStream.close();
arrayOutputStream.close();

String content = new String(arrayOutputStream.toByteArray());
TV.setText(content);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
...全文
1017 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2018-02-18
  • 打赏
  • 举报
回复
引用 3 楼 weixin_41545972 的回复:
兄弟们,没有用,乱码了,估计跟edittext有关
应该是你第一次按保存时输入多少字符,当你下次想要在其后继续输入时也应该相应地输入多少字符,否则就会有乱码
YXTS122 2018-02-18
  • 打赏
  • 举报
回复
应该是你第一次按保存时输入多少字符,当你下次想要在其后继续输入时也应该相应地输入多少字符,否则就会有乱码
495711824 2018-01-09
  • 打赏
  • 举报
回复
统一UTF8编码啊,大哥。
Yunc2 2018-01-09
  • 打赏
  • 举报
回复
以前好像遇到过,把1024改大试试,或者换个读写方式
weixin_41545972 2018-01-04
  • 打赏
  • 举报
回复
兄弟们,没有用,乱码了,估计跟edittext有关
YXTS122 2018-01-02
  • 打赏
  • 举报
回复
1楼正解,按照1楼的方法改改试试。。。。。。。。。
w22net 2018-01-01
  • 打赏
  • 举报
回复
将 String content = new String(arrayOutputStream.toByteArray()); 改为 String content = new String(arrayOutputStream.toByteArray(),"gb2312");试试看看

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧