2012-02-28 2 views
0

텍스트 파일을 관리하는 클래스를 만듭니다. 내가 파일을 만들 때"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(); 
} 
+0

시도해보십시오. osw.write (content.trim()); –

+0

Thx 당신, 작동합니다! – korax

답변

0

텍스트 편집기를 사용하여 파일을 만드는 경우 편집기는 파일 크기를 채우기 위해 일부 빈 줄을 추가 할 수 있습니다. 프로그래밍 방식으로 새로운 (빈) 파일을 만들기 위해 MODE_APPEND 플래그없이 openFileOutput을 호출하여 텍스트 편집기를 피할 수 있습니다.

그렇지 않으면 trim()을 사용하는 appserv 님의 제안에 따라 문자열을 정리하는 것이 좋습니다.

+0

Thx 너, 나 trim()을 사용하고 작동합니다! – korax

관련 문제