android io流

2021-08-25 11:40:09 浏览数 (1)

  • 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;
    }

0 人点赞