我想我对这个问题有一个大概的了解,但并不完全确定。有一个类似的解决方案,但只有C的答案。
我正在为internal storage file
写一个字符串值列表。
OUTSTREAM = saveName.toString() + "," +
d3S.getText().toString() + "," + d3M.getText().toString() + "," + d3C.isChecked() + "," +
d4S.getText().toString() + "," + d4M.getText().toString() + "," + d4C.isChecked() + "," +
d6S.getText().toString() + "," + d6M.getText().toString() + "," + d6C.isChecked() + "," +
d8S.getText().toString() + "," + d8M.getText().toString() + "," + d8C.isChecked() + "," +
d10S.getText().toString() + "," + d10M.getText().toString() + "," + d10C.isChecked() + "," +
d12S.getText().toString() + "," + d12M.getText().toString() + "," + d12C.isChecked() + "," +
d20S.getText().toString() + "," + d20M.getText().toString() + "," + d20C.isChecked() + "," +
d100S.getText().toString() + "," + d100M.getText().toString() + "," + d100C.isChecked() + "," +
dCustS.getText().toString() + "," + dCustND.getText().toString() + "," + dCustM.getText().toString() + "," + dCustC.isChecked();
Log.d("OUTSTREAM::", "" + OUTSTREAM + ";");
Log.d("OUTSTREAM Bytes::", "" + OUTSTREAM.getBytes() + ";");
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_APPEND); //MODE_APPEND MODE_PRIVATE
fos.write(OUTSTREAM.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Save FileNotFoundException",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Save IOException",Toast.LENGTH_SHORT).show();
}
logcat的:
08-13 11:33:12.486: D/OUTSTREAM::(1547): NewSave,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,5,0,true;
08-13 11:33:12.486: D/OUTSTREAM Bytes::(1547): [B@52a6507c;
但是当我写入文件时,我的数据会在文件末尾添加额外的特殊字符。
try{
fIn = openFileInput("DiceSaves.txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(MainActivity.this, "Loaded: " + data, Toast.LENGTH_SHORT).show();
List<String> lines = Arrays.asList(data.split("\n"));
Object[] aLinesArray = lines.toArray();
String[] SaveNames = new String[10];
int n = 0;
for(int i = 0; i < aLinesArray.length ; i++){
String line = aLinesArray[i].toString();
Log.d("i and Line::", "" + i + "," + line + ";");
logcat的:
08-13 11:35:08.766: D/i and Line::(1547): 0,NewSave,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,0,true,1,5,0,true??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????;
我不确定是否需要添加"\n", "\r\n"
或保留,因为我是appending
文件。如果我不需要新行,那么我需要null
使用"\0"
的最后一个enrty文件的结尾...
这是建立在新的Geny Mototion AVD上,每次执行时都有SGS4 4.4.2,以确保文件是新制作的。
答案 0 :(得分:0)
不会将不需要的字符添加到文件中,问题在于您重新读取数据的方式。您没有使用isr.read(inputBuffer)
返回的长度。
您需要做的就是改变
isr.read(inputBuffer);
data = new String(inputBuffer);
到
int length = isr.read(inputBuffer);
data = new String(inputBuffer, 0, length);
它不会破坏任何东西,但我也不确定为什么要将字符串数组转换为列表然后返回到数组:
List<String> lines = Arrays.asList(data.split("\n"));
Object[] aLinesArray = lines.toArray();
你可以这样做:
String[] aLinesArray = data.split("\n");