2013-09-01 15 views
5

Ben google alot, read javadoc yanı sıra bu okuma dahil olmak üzere farklı forumlarda arama var ama benim soru doğru cevap bulunamadı. Aşağıdaki kod pasajı iyi çalışıyor ancak tam olarak androidde okuma/yazma dosyası için tam olarak hangi işlevi kullanacağını bilmek istiyorum. Birisi OutputStream, FileOutputSteam.write() kullanarak dahili depolamaya yazabilir, Diğer OutputStreamWriter (FileOutputSteam) .write(), ayrıca BufferedWriter (OutputStreamWriter) .write() ve son olarak PrintWriter.write() kullanmaktır.Dahili/harici depolama cihazına bir dosya okurken/yazarken hangi sınıfı ve yöntemi kullanmalıyım?

InputStream, FileInputSteam.read(), InputSreamReader (FileInputStream) .read(), BufferedReader (InputStreamReader) öğelerinin kullanılıp kullanılmayacağı da aynı şekilde InputStream örneğine gider.

Bunu yapmak için en uygun yolun hangisi olduğunu bilmek istiyorum. Lütfen bununla tamamen kafamda olduğu gibi bana yardım edin.

public class MainActivity extends Activity { 

private static final String TAG = MainActivity.class.getName(); 
private static final String FILENAME = "students.txt"; 
private EditText stdId; 
private Button Insert; 
private Button Search; 
private Button Update; 
private Button Delete; 

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

    final String TAG = MainActivity.class.getName(); //output: com.fyp2.testapp.MainActivity 

    //value used for insert/delete/search 
    stdId = (EditText) findViewById(R.id.editTxtId); 

    //insert value in application sandbox file 
    Insert = (Button) findViewById(R.id.btnInsert); 
    Insert.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      String IdToInsert = stdId.getText().toString().trim(); 
      if(IdToInsert.length() > 0) { 
       myInsertFunc(IdToInsert); 
      } 
      else { 
       Toast.makeText(getApplicationContext(), "Id cannot be null!", Toast.LENGTH_SHORT).show(); 
       stdId.requestFocus(); 
      } 
     } 
    }); 

    //search value from application sandbox file 
    Search = (Button) findViewById(R.id.btnSearch); 
    Search.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      String IdToSearch = stdId.getText().toString().trim(); 
      if(IdToSearch.length() > 0) { 
       mySearchFunc(IdToSearch); 
      } 
      else { 
       Toast.makeText(getApplicationContext(), "Id cannot be null!", Toast.LENGTH_SHORT).show(); 
       stdId.requestFocus(); 
      } 
     } 
    }); 


    //delete value from application sandbox file 
    Delete = (Button) findViewById(R.id.btnDelete); 
    Delete.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      String IdToDelete = stdId.getText().toString().trim(); 
      if(IdToDelete.length() > 0) { 
       myDeleteFunc(IdToDelete); 
      } 
      else { 
       Toast.makeText(getApplicationContext(), "Id cannot be null!", Toast.LENGTH_SHORT).show(); 
       stdId.requestFocus(); 
      } 
     } 
    }); 
} 

//function to insert 
private void myInsertFunc(String data) { 
    //If student id already exist don't write it again -> Not handled at the moment 
    //Other exceptions may not have been handled properly 
    try { 
     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_APPEND)); 
     BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); 
     bufferedWriter.append(data); 
     bufferedWriter.newLine(); 

     Toast.makeText(getApplicationContext(), "Student ID: " + data + " Inserted Successfully!", Toast.LENGTH_SHORT).show(); 

     bufferedWriter.close(); 
     outputStreamWriter.close(); 
    } 
    catch (IOException e) { 
     Log.e(TAG, "File write failed: " + e.toString()); 
    } 
} 

//function to search 
private void mySearchFunc(String data) { 
    //Id id not found show toast to user -> Not handled at the moment 
    try { 
     InputStream inputStream = openFileInput(FILENAME); 

     if (inputStream != null) { 
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
      String receiveString = ""; 
      while ((receiveString = bufferedReader.readLine()) != null) { 
       if(receiveString.contains(data)) 
       { 
        Toast.makeText(getApplicationContext(), "Found Student ID: " + data , Toast.LENGTH_SHORT).show(); 
        break; 
       } 
      } 

      bufferedReader.close(); 
      inputStream.close(); 
     } 
    } 
    catch (FileNotFoundException e) { 
     Log.e(TAG, "File not found: " + e.toString()); 
    } catch (IOException e) { 
     Log.e(TAG, "Can not read file: " + e.toString()); 
    } 
} 

private void myDeleteFunc(String data) { 
    /* I have found a solution to delete a specific line from the file. 
    * But the problem is that it needs to scan the whole file line by line and copy the file contents that not matches the string to temp file. 
    * This solution can reduce the effeciency. Consider search 20,000 records in a file. 
    * Need to work around on it. 
    */ 
} 

private void myUpdateFunc(String data) { 
    /* Same goes to the update process... 
    * Need to scan all records and copy content in temp file and put the updated data in that file. 
    * Need to work around on this issue too... 
    */ 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

cevap

0

Hepsi bir amaca hizmet. En uygun yol var, çünkü her yazar bir sebepten dolayı yapıldı. Genellikle okuma/yazma/yıkama özelliklerine sahipseniz, arabelleğe alınmış olan her şey daha verimli olur.

Hem Android hem de Java dokümanları yardımcı olabilir ve java belgeleri bu yazarlar için size daha fazla açıklama sunar. Bu belgelere göz atın.

Android_FileWriter

Java_FileWriter

Android_OutputStreamWriter

Java_OutputStreamWriter

Android_BufferedWriter

Java_BufferedWriter

: daha detaylı javase6 olanları okumaya çalışın
İlgili konular