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

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public 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.bmp
                fos = 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();
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值