2017-06-19 47 views
0

我想寫一個字符串到文件abc.txt和稍後讀取相同並與輸入字符串進行比較。但它不能按需要工作。 當我通過在netbeans中創建一個項目來做同樣的事情時,它像一個魅力一樣工作,但在這裏它沒有。 代碼寫作:寫作和閱讀相同的文件是不適用於android應用程序

try{ 
     writer = new BufferedWriter(new FileWriter("abc.txt")); 
     writer.write("12345"); 
     writer.close(); 
     Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_LONG).show(); 

    }catch(IOException i){ 
     i.printStackTrace(); 
    } 

代碼閱讀:

String ownerPass= null; 
    StringBuilder s = new StringBuilder(); 

    try{ 

     FileReader fr=new FileReader("abc.txt"); 
     BufferedReader br=new BufferedReader(fr); 
     int i; 
     while((i=br.read())!=-1){ 


      s.append((char)i); 
     } 
     br.close(); 
     fr.close(); 
     ownerPass = s.toString(); 

    }catch(IOException i){ 
     i.printStackTrace(); 
    } 

的情況是相同的我是否比較小號ownerPass

我不明白究竟是無法寫入文件或讀取文件。 也請告訴我如何在計算機中手動檢查文件abc.txt。

這是我的清單文件:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="NORIS" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 


     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (!Settings.System.canWrite(this)) { 
     requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, 
     Manifest.permission.READ_EXTERNAL_STORAGE}, 2909); 
     } else { 
     // continue with your code 
     } 
     } else { 
     // continue with your code 
     } 

     @Override 
     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
     case 2909: { 
     if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     Log.e("Permission", "Granted"); 
     } else { 
     Log.e("Permission", "Denied"); 
     } 
     return; 
     } 
     } 
     } 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".userBase" /> 
    <activity android:name=".ownerBase" /> 
    <activity android:name=".ownerPage" /> 
    <activity 
     android:name=".SettingsActivity" 
     android:label="@string/title_activity_settings" /> 
    <activity android:name=".ownerBrowse" /> 
    <activity android:name=".ownerEdit"></activity> 
</application> 

這是我的java啓動活動:

public class MainActivity extends AppCompatActivity { 



SharedPreferences prefs = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    prefs = getSharedPreferences("com.example.srijankumar.myapp1", MODE_PRIVATE); 


    Button btnUser = (Button) findViewById(R.id.btnUser); 
    btnUser.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      callUserBase(); 
     } 

    }); 

    Button btnOwner = (Button) findViewById(R.id.btnOwner); 
    btnOwner.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      callOwnerBase(); 
     } 
    }); 


    BufferedWriter writer = null; 

    try{ 
     writer = new BufferedWriter(new FileWriter("abc.txt")); 
     writer.write("12345"); 
     writer.close(); 
     Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_LONG).show(); 



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


    } 


private void callOwnerBase() { 
    Intent i = new Intent(MainActivity.this,ownerBase.class); 
    startActivity(i); 
} 

private void callUserBase() { 
    Intent i = new Intent(MainActivity.this,userBase.class); 
    startActivity(i); 
} 

}

+0

它不工作究竟如何?你有例外嗎?或內容不匹配?或者是什麼? –

+0

當我比較字符串「12345」與我通過EditText給出的輸入時,它不匹配。 @VladMatvienko –

+0

好吧,你有沒有試過檢查,它比較什麼? –

回答

-1

您是否添加了清單權限和運行時權限?

添加此行清單

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

在發射活動添加此運行時權限:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
    if (!Settings.System.canWrite(this)) { 
     requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, 
      Manifest.permission.READ_EXTERNAL_STORAGE}, 2909); 
    } else { 
     // continue with your code 
    } 
} else { 
    // continue with your code 
} 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
      case 2909: { 
       if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Log.e("Permission", "Granted"); 
       } else { 
        Log.e("Permission", "Denied"); 
       } 
       return; 
      } 
     } 
    } 
+0

這些是什麼?請詳細說明,我是android新手。 –

+0

這是存儲權限,當我寫入應用程序數據文件夾時,我們必須添加強制 – NareshRavva

+0

..我沒有使用這個? –

相關問題