2016-06-30 15 views
-2

Test.txtをsdcardに作成し、それに文字列 "test example"を書き込みました。
その後、私はTest.txtの文字列 "test"を "etc"に置き換えます。
が、これは私のコードです:このコードでandroidは文字列を別の文字列で置き換えます。

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(); 
        } 

       } 
      }); 

、それはSDカード上のTest.txtを作成し、その上で「試験例」を書きました。
しかし、 "test"という文字列を "etc"に置き換えると、動作しません。
どのように修正するのですか? THIはあなたを助けることができる

+0

に任意の例外を試してみてください? –

+1

'String s =" "; s = s.replace(old_str、new_str) '<=それは何もしません...空の文字列で何かを置き換えようとしています – Selvin

+0

@selvin:それを修正する方法 – abcd1234

答えて

0

私はいつも私のために働いた、私のコードを与える:)

希望:DD

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

ファイル内のすべての文字列をクリアします。私はnullポインタを取得した – abcd1234

+0

java.lang.NullPointerExceptionを取得する?? –

関連する問題