当前位置: 代码迷 >> 综合 >> new FileOutputStream(a.bmp) 报错java.io.FileNotFoundException: /a.bmp (Read-only file system)
  详细解决方案

new FileOutputStream(a.bmp) 报错java.io.FileNotFoundException: /a.bmp (Read-only file system)

热度:94   发布时间:2023-12-16 15:37:40.0
03-20 14:10:36.971 4270-4297/? W/System.err: java.io.FileNotFoundException: /a.bmp (Read-only file system)
03-20 14:10:36.981 4270-4297/? W/System.err:     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
03-20 14:10:36.981 4270-4297/? W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
03-20 14:10:36.981 4270-4297/? W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
03-20 14:10:36.981 4270-4297/? W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:144)
03-20 14:10:36.991 4270-4297/? W/System.err:     at tech.androidstudio.readfileshttpurlconnection.MainActivity.run(MainActivity.java:43)

03-20 14:10:36.991 4270-4297/? W/System.err:     at java.lang.Thread.run(Thread.java:1019)


原因分析:

注意报错里面的信息为,不能创建主目录/ 下面的a.bmp ,这是一个只读的文件夹。所以说文件的路径有问题,

我的代码如下,错误就是没有写路径

fos = new FileOutputStream("a.bmp");

解决方法:

将路径添加进去,例如添加到cache的文件夹里面

//下面存储到内部存储的私有的cache目录里面,注意了生成的文件名是cachea.bmp fos = new FileOutputStream(getCacheDir().getPath()+"a.bmp");

package tech.androidstudio.readfileshttpurlconnection;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;public class MainActivity extends AppCompatActivity implements Runnable {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Thread thread = new Thread(this);thread.start();}@Overridepublic void run() {try {//TODO 这里的ip 地址一定不能使localhost 一定要是电脑的或者是正式ip地址.//如果写成了localhost,那么就会报错java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused
//            URL url = new URL("http://localhost:8080/tomcat.png");URL url = new URL("http://192.168.1.106:8080/tomcat.png");HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setRequestMethod("GET");httpURLConnection.setConnectTimeout(5000);//Sets the flag indicating whether this URLConnection allows input. It cannot be set after the connection is established.httpURLConnection.setDoInput(true);InputStream in = null;FileOutputStream fos = null;if (httpURLConnection.getResponseCode() == 200) {in = httpURLConnection.getInputStream();//一定不能直接在FileOutputStream里面写文件名,需要添加路径//错误的写法:fos = new FileOutputStream("a.bmp");//下面存储到内部存储的私有的cache目录里面,注意了生成的文件名是cachea.bmpfos = new FileOutputStream(getCacheDir().getPath()+"a.bmp");byte[] arr = new byte[1024];int len = 0;while ((len = in.read(arr)) != -1) {fos.write(arr, 0, len);}in.close();fos.close();}} catch (MalformedURLException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (ProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
}



  相关解决方案