簡體   English   中英

寫文件

[英]Writing in the file

我正在制作一個可以創建花絮的應用程序。 在該文件夾中,我正在創建一個文件並在其中寫入Radha。 但它顯示Hello World,CreateActivity的代碼是

創建.java

public class CreateActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Context ctx = getApplicationContext(); 

    try { 
        File myDir = new File(getFilesDir().getAbsolutePath()); 
        String s = ""; 

        FileWriter fw = new FileWriter(myDir + "/Test.txt"); 
        fw.write("Radha"); 
        fw.close(); 

        BufferedReader br = new BufferedReader(new FileReader(myDir + "/Test.txt")); 
        s = br.readLine(); 

        // Set TextView text here using tv.setText(s); 

    } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } 
} 

您寫入文件的內容與屏幕上的內容無關。 檢查布局xml(res / layout / main.xml)。 (您在調用setContentView(R.layout.main);時使用此方法。)布局可能引用了可在res / values / strings.xml中找到的字符串資源。 更改該字符串以更改屏幕上顯示的內容。

我建議您閱讀適用於Android的Hello World教程 ,以了解這些基礎知識。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM