텍스트 파일을 관리하는 클래스를 만듭니다. 내가 파일을 만들 때"Resolved"끝에 공백이있는 파일 작성
public static void writeFiles(Context context, String nomFichier, String content, char mode) {
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try {
if (mode == 'd') {
context.deleteFile(nomFichier);
} else {
fOut = context.openFileOutput(nomFichier, Context.MODE_APPEND);
osw = new OutputStreamWriter(fOut);
osw.write(content);
osw.flush();
}
} catch (Exception e) {
Toast.makeText(context, "Message not saved",Toast.LENGTH_SHORT).show();
} finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
Toast.makeText(context, "Message not saved",Toast.LENGTH_SHORT).show();
}
}
}
, 그것은 몇 빈 라인으로 가득 : 내가 작성하는 방법 및 내 파일을 읽을 타있다. 내 파일의 내용을 EditText로 설정하여 공백을 원하지 않습니다. 공백없이 파일을 만들려면 어떻게해야합니까?
Thx, korax.
는 편집 :
는나는의 appserv 및 ACJ에 의해 제안) (트림 사용하지만, 대신에 쓰기 기능의 읽기 기능입니다. 그것은 잘 작동합니다.
public static String readFile(Context context, String fileName) {
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String content = null;
try {
fIn = context.openFileInput(fileName);
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
content = new String(inputBuffer);
} catch (Exception e) {
//Toast.makeText(context, "Message not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
//Toast.makeText(context, "Message not read",Toast.LENGTH_SHORT).show();
}
}
return content.trim();
}
시도해보십시오. osw.write (content.trim()); –
Thx 당신, 작동합니다! – korax