Java 如何在android上创建和/或附加文件?
以下是我正在做的:Java 如何在android上创建和/或附加文件?,java,android,file,file-io,filewriter,Java,Android,File,File Io,Filewriter,以下是我正在做的: static void writeToFile(String text) { try { synchronized (fileLock) { File external = Environment.getExternalStorageDirectory(); String sdcardPath = external.getPath(); FileWriter filewri
static void writeToFile(String text) {
try {
synchronized (fileLock) {
File external = Environment.getExternalStorageDirectory();
String sdcardPath = external.getPath();
FileWriter filewriter = new FileWriter(sdcardPath
+ "/Documents/myfile.txt", true);
BufferedWriter out = new BufferedWriter(filewriter);
out.write(text);
out.close();
filewriter.close();
}
} catch (Exception e) {
android.util.Log.d("failed to save file", e.toString());
}
}
打开文件时,我遇到以下异常:
java.io.FileNotFoundException: /mnt/sdcard/Documents/myfile.txt:
open failed: ENOENT (No such file or directory)
/mnt/sdcard/Documents/
根据根浏览器存在
myfile.txt
不存在,我希望创建或附加它<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您可以尝试以下方法:
static void writeToFile(String text) {
try {
synchronized (fileLock) {
File external = Environment.getExternalStorageDirectory();
String sdcardPath = external.getPath();
File file = new File(sdcardPath + "/Documents/myfile.txt");
file.createNewFile();
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
out.write(text);
out.close();
filewriter.close();
}
} catch (Exception e) {
android.util.Log.d("failed to save file", e.toString());
}
}
您可以尝试类似的方法:
static void writeToFile(String text) {
try {
synchronized (fileLock) {
File external = Environment.getExternalStorageDirectory();
String sdcardPath = external.getPath();
File file = new File(sdcardPath + "/Documents/myfile.txt");
file.createNewFile();
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
out.write(text);
out.close();
filewriter.close();
}
} catch (Exception e) {
android.util.Log.d("failed to save file", e.toString());
}
}
首先,您必须创建文件,然后将其附加到文件中。这是创建文件的代码
sFileName = "Android.txt";
File root=Environment.getExternalStorageDirectory();
File textfile = new File(root, sFileName);
textfile.createNewFile();
许可:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
首先您必须创建文件,然后附加到文件中这是创建文件的代码
sFileName = "Android.txt";
File root=Environment.getExternalStorageDirectory();
File textfile = new File(root, sFileName);
textfile.createNewFile();
许可:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
试试这个:
File storagePath = new File(getExternalCacheDir() + "/dirname/");
storagePath.mkdirs();
File myFile = new File(storagePath, fileName);
OutputStream out = new BufferedOutputStream(new FileOutputStream(
myFile));
try {
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} finally {
try {
out.close();
} catch (IOException e) {
}
try {
inputStream.close();
} catch (IOException e) {
}
}
试试这个:
File storagePath = new File(getExternalCacheDir() + "/dirname/");
storagePath.mkdirs();
File myFile = new File(storagePath, fileName);
OutputStream out = new BufferedOutputStream(new FileOutputStream(
myFile));
try {
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} finally {
try {
out.close();
} catch (IOException e) {
}
try {
inputStream.close();
} catch (IOException e) {
}
}
这样使用:
File root = android.os.Environment.getExternalStorageDirectory();
// See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, "myData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
或:
File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/");
File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/myFile.txt");
if(!direct.exists())
{
direct.mkdir();
}
if (!file.exists()) {
try {
file.createNewFile();
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
}}
希望这对您有所帮助。像这样使用:
File root = android.os.Environment.getExternalStorageDirectory();
// See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, "myData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
或:
File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/");
File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/myFile.txt");
if(!direct.exists())
{
direct.mkdir();
}
if (!file.exists()) {
try {
file.createNewFile();
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
}}
希望这会对您有所帮助。当您打印
sdcardPath
时,它是否会显示/mnt/sdcard/
?当您打印sdcardPath
时,它是否会显示/mnt/sdcard/
?我在方法file.createNewFile()
中遇到异常,因为我没有Documents
文件夹。您可以尝试直接在SD卡的根目录下写入myfile.txt
吗?我在方法file.createNewFile()
中遇到了异常,因为我没有Documents
文件夹,所以出现了相同的异常。你能试着直接在SD卡的根目录下写入myfile.txt
吗?好的,这是可行的,但是我以后在我的文件系统中找不到该文件。我本应该创建/mnt/sdcard/myfile.txt
,但它不是这样的,它可以工作,但是我以后在我的文件系统中找不到该文件。我本应该创建/mnt/sdcard/myfile.txt
,但它不在那里代码执行时没有异常,但是这些文件不会出现在filesystem@ArsenZahray它对我有效。您是否签入emulator或设备?要在emulator中查看文件,请转到文件浏览-->mnt-->sdcard-->下载-->myData.txt。该应用程序表示该文件已创建,并且目录存在,但根浏览器、文件浏览器或控制台emulator找不到这些文件。很奇怪((代码执行时没有异常,但文件不会出现在filesystem@ArsenZahray它对我有效。您是否签入emulator或设备?要在emulator中查看该文件,请转到文件浏览-->mnt-->sdcard-->下载-->myData.txt。应用程序显示该文件已创建,目录已存在,但根浏览器或文件资源管理器或控制台模拟器找不到这些。非常奇怪((