ورود

View Full Version : ایجاد فایل txt در اندروید



miladheydari
پنج شنبه 17 اردیبهشت 1394, 11:37 صبح
با سلام میخواستم داخل یه پوشه یه فایل myData.txt تولید کنم و یه چیزای توش بنویسم اما file not find exception بهم میده

File root = android.os.Environment.getExternalStorageDirectory ();
File dir = new File (root.getAbsolutePath() + "/hash");
if(!dir.exists()) {
dir.mkdirs(); // build directory
Toast.makeText(MainActivity.this, dir.getAbsolutePath()+"-1",Toast.LENGTH_LONG).show();

}
File file = new File(dir, "myData.txt");

try {
OutputStream f = new FileOutputStream(file);
Toast.makeText(MainActivity.this, file.getAbsolutePath()+"0",Toast.LENGTH_LONG).show();
PrintWriter pw = new PrintWriter(f);
pw.println("125");
pw.println("432");
pw.flush();
pw.close();
f.close();

} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, file.getAbsolutePath()+"1",Toast.LENGTH_LONG).show();

e.printStackTrace();
} catch (IOException e) {
Toast.makeText(MainActivity.this, dir.getAbsolutePath()+"2",Toast.LENGTH_LONG).show();

e.printStackTrace();
}