2016-06-30 3 views
-2

Test.txt을 sdcard에 작성하고 문자열에 "test example"을 씁니다.
그 후에는 "Test"문자열을 Test.txt의 "etc"로 대체합니다.
이 내 코드 :이 코드android는 sdcard의 파일에서 문자열을 다른 문자열로 바꿉니다.

String origin_str, old_str , new_str; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_t2); 

      origin_str = "test example"; 
      old_str = "test"; 
      new_str = "etc"; 

      Button bt_create2 = (Button)findViewById(R.id.bt_createfileT2); 
      bt_create2.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        try { 
         File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder"); 
         if (!newFolder.exists()) { 
          newFolder.mkdir(); 
         } 

         File file = new File(newFolder, "Test" + ".txt"); 
         if (!file.exists()) { 
          file.createNewFile(); 
          FileOutputStream fOut = new FileOutputStream(file); 
          OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut); 
          myOutWriter.append(origin_str); 
          myOutWriter.close(); 
          fOut.close(); 
         } 
        } catch (Exception e) { 
         System.out.println("e: " + e); 
        } 
       } 
      }); 

      Button bt_replacefileT2 = (Button)findViewById(R.id.bt_replacefileT2); 
      bt_replacefileT2.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        try { 
         File file = new File(Environment.getExternalStorageDirectory() + "/TestFolder/Test.txt"); 
         FileInputStream in = new FileInputStream(file); 
         int len = 0; 
         byte[] data1 = new byte[1024]; 
         while (-1 != (len = in.read(data1))){ 
          if(new String(data1, 0, len).contains(old_str)){ 
           String s = ""; 
           s = s.replace(old_str, new_str); 
          } 
         } 

        } 
        catch (Exception e){ 
         e.printStackTrace(); 
        } 

       } 
      }); 

, 그것은 SDCARD에있는 test.txt를 만들고 페이지의 "테스트 예"를 작성했다.
하지만 "test"문자열을 "etc"로 바꾸면 작동하지 않습니다.
어떻게 고칠 수 있습니까? 나는 항상 나를 위해 일한 내 코드 :

희망 생은 당신을 도울 수있는 줄 것이다

+0

예외를 시도 DD를? –

+1

'String s = ""; s = s.replace (old_str, new_str)'<= 그냥 아무 것도하지 않습니다. 빈 문자열에있는 것을 대체하려고합니다. – Selvin

+0

@selvin : 어떻게 수정합니까 – abcd1234

답변

0

:

public void saveString(String text){ 
      if(this.isExternalStorageAvailable()){ 
       if(!this.isExternalStorageReadOnly()){ 
        try { 
         FileOutputStream fos = new FileOutputStream(
           new File(this.getExternalFilesDir("text"), "text.dat")); 
         ObjectOutputStream oos = new ObjectOutputStream(fos); 

         oos.writeBytes(text); 
         oos.close(); 
         fos.close(); 

        } catch (FileNotFoundException e) { 
         //Toast.makeText(main, "Eror opening file", Toast.LENGTH_SHORT).show(); 
        } catch (IOException e) { 
         //Toast.makeText(main, "Eror saving String", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      } 
     } 

    private static boolean isExternalStorageAvailable(){ 
      String estadoSD = Environment.getExternalStorageState(); 
      if(Environment.MEDIA_MOUNTED.equals(estadoSD)) 
       return true; 

      return false; 
     } 
     private static boolean isExternalStorageReadOnly(){ 
      String estadoSD = Environment.getExternalStorageState(); 
      if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(estadoSD)) 
       return true; 

      return false; 
     } 

public String getString(){ 
     FileInputStream fis = null; 
     ObjectInputStream ois = null; 

     if(this.isExternalStorageAvailable()) { 
      try { 
       fis = new FileInputStream(
         new File(this.getExternalFilesDir("text"), "text.dat")); 
       ois = new ObjectInputStream(fis); 

       String text = (String)ois.readObject(); 
       return familia; 


      } catch (FileNotFoundException e) { 
       //Toast.makeText(main, "The file text doesnt exist", Toast.LENGTH_SHORT).show(); 
      } catch (StreamCorruptedException e) { 
       //Toast.makeText(main, "Eror opening file", Toast.LENGTH_SHORT).show(); 
      } catch(EOFException e){ 
       try { 
        if(ois != null) 
         ois.close(); 

        if(fis != null) 
         fis.close(); 
       } catch (IOException e1) { 
        e1.printStackTrace(); 
       } 
      } catch (IOException e) { 
       //Toast.makeText(main, "eror reading file", Toast.LENGTH_SHORT).show(); 
      } catch (ClassNotFoundException e) { 
       //Toast.makeText(main, "String class doesnt exist", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     return null; 
    } 
-1

File file = new File(Environment.getExternalStorageDirectory() + "/TestFolder/Test.txt"); 
try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     line = line.replace(old,new); 
    } 
    br.close(); 

    FileOutputStream fOut = new FileOutputStream(file); 
    OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut); 
    myOutWriter.write(line); 
    myOutWriter.close(); 
    fOut.close(); 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 
+0

파일의 모든 문자열을 지우고 문자열을 대체하지 않습니다. java.lang.NullPointerException을 얻는다 – abcd1234

+0

널 포인터가있는 곳 ?? –

관련 문제