0

我只是想问一下我的代码是否正确,因为我无法在我的name.txt文件中加载文本。
另外我只想知道如何将输入文本保存在我的name.txt文件中。

load.setOnClickListener(new 

View.OnClickListener() {

        @Override
        public void onClick

(View arg0) {
            try{


StringBuffer sb=new StringBuffer();


FileInputStream fis=con.openFileInput("name.txt");


DataInputStream dis=new DataInputStream(fis);


String text=null;


while((text=dis.readLine())!=null)


sb.append(text+"\n");


dis.close();


et1.setText(sb.toString()); 

            }catch 

(IOException e){


Toast.makeText(con, "Could not find", Toast.LENGTH_LONG).show();
            }

        }
    });
4

2 回答 2

0

如果要将数据写入文件

static protected void writeToFile(String strToWrite)
{
    try 
    {
        BufferedWriter out = new BufferedWriter(new FileWriter("name.txt", true));
        out.write(strToWrite);
        out.close();
    } 
    catch (IOException e) { e.printStackTrace();}
}

您编写的代码是从现有文件中读取数据而不是写入文件中。

于 2013-10-06T09:29:44.817 回答
0
try 
{
    File file1 = new File(Environment.getExternalStorageDirectory() + "/dir/", "filename.extension");
    FileWriter fw1 = new FileWriter(file1.getAbsoluteFile());
    BufferedWriter bw1 = new BufferedWriter(fw1);
    bw1.write("Stringtowritetofile");
    bw1.close();
}
于 2013-10-06T11:39:11.907 回答