Android のプライベート ディレクトリに JSON ファイルを保存しています。問題は、ファイルを読むと、テキストの最後に奇妙な文字が埋め込まれており、なぜこれが起こっているのかわかりません。
ログ:
E/WRITTEN(25254): {"sex":"MALE","activity_factor":1.2,"weight":0,"height":180.0,"weight_loss_goal":0,"age":30}
E/StartActivity(25254): 再起動 07-03
E/READ(25254): {"sex":"MALE","activity_factor":1.2,"weight":0,"height":180.0,"weight_loss_goal":0,"age":30}???? ?????????????????????????????????????????????????????? ??????????????????????????????????????..(これは延々と続く)
FileWrite 入出力コード:
public class FileUtil
{
public static void writeToFile ( Context context, String filename, String text, int mode ) throws IOException
{
FileOutputStream fos = null;
try
{
fos = context.openFileOutput ( filename, mode );
fos.write ( text.getBytes () );
Log.e("WRITTEN",text);
}
catch ( FileNotFoundException e )
{
throw e;
}
catch ( IOException e )
{
throw e;
}
finally
{
if ( fos != null )
{
try
{
fos.close ();
}
catch ( IOException e )
{
}
}
}
}
public static String readFromFile ( Context context, String fileName ) throws IOException
{
FileInputStream fis = null;
StringBuilder content = new StringBuilder ( "" );
try
{
byte [] buffer = new byte [1024];
fis = context.openFileInput ( fileName );
while ( fis.read ( buffer ) != -1 )
{
content.append ( new String ( buffer ) );
}
}
catch ( FileNotFoundException e )
{
throw e;
}
catch ( IOException e )
{
throw e;
}
finally
{
if ( fis != null )
{
try
{
fis.close ();
}
catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
}
Log.e("READ",content.toString ());
return content.toString ();
}
}