前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android io流

android io流

原创
作者头像
八神太一
修改2021-08-25 11:40:09
5430
修改2021-08-25 11:40:09
举报
文章被收录于专栏:androud
  • FileOutputStream
代码语言:javascript
复制
 try {
            FileOutputStream fileOutputStream = new FileOutputStream(file.getPath());
            byte[] bytes = aa.getBytes();
            try {
                fileOutputStream.write(bytes);
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
  • FileInputStream
代码语言:javascript
复制
        try {
            FileInputStream fileInputStream = new FileInputStream(file.getPath());
            byte[] bytes = new byte[1024 * 8];
            int ch;
            while ((ch = fileInputStream.read(bytes))!=-1){
                System.out.println(new String(bytes,0,ch));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
  • FileWriter
代码语言:javascript
复制
 try {
            FileWriter fileWriter = new FileWriter(file.getPath() ,true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("222222222222222222222222");
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
  • FileReader
代码语言:javascript
复制
        try {
            FileReader fileReader = new FileReader(file.getPath());
            char[] bytes = new char[1024 * 8];
            int ch;
            while ((ch = fileReader.read(bytes))!=-1){
                System.out.println(new String(bytes,0,ch));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
  • RandomAccessFile
代码语言:javascript
复制
  File file =new File("E:\\Win10.ios");
        if (!file.exists()){
            file.exists();
        }
 new Thread(new Runnable() {
            RandomAccessFile rwd;
            int cv;
            @Override
            public void run() {
                try {
                    URL url = new URL("");
                    try {
                        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                        httpURLConnection.setRequestMethod("GET");
                        //从上次下载完成的地方下载
                        int start =(int) file.length();
                        //设置下载位置(从服务器上取要下载文件的某一段)
                        httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-" + getlength(url.toString());//设置下载范围
                        //设置文件写入位置
                        rwd = new RandomAccessFile(file, "rwd");
                        //从文件的某一位置开始写入
                        rwd.seek(start);
                        httpURLConnection.connect();
                        if (httpURLConnection.getResponseCode()==206){
                            InputStream inputStream = httpURLConnection.getInputStream();
                            byte[] bytes = new  byte[1024 * 8];
                            int ch;
                            while ((ch = inputStream.read(bytes)) != -1){
                                rwd.write(bytes,0,ch);
                                cv += ch;
                            }
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }
    public static int getlength(String url){
        int Length = 0;
        try {
            URL url1 = new URL(url);
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.connect();
                if (httpURLConnection.getResponseCode() ==HttpURLConnection.HTTP_OK){
                    Length = httpURLConnection.getContentLength();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return Length;
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档