Good afternoon, I send the file and I want to realize deleting it exactly for the number of bytes that have already been sent (it is clear that the bytes should be deleted from the beginning of the file) and so on every time you connect to the network. I stopped at the fact that I get the number of bytes to which it should be deleted, but I don’t know what to do next with this number?

How to delete a file for a specified number of bytes?

int countFiles = list.size();//создаём переменную countFiles (подсчёт файлов) DataOutputStream outD; // переменная потока отправляемых данных try { outD = new DataOutputStream(client.getOutputStream()); outD.writeInt(countFiles);//отсылаем количество файлов for (int i = 0; i < countFiles; i++) { File f = new File(list.get(i)); outD.writeLong(f.length());//отсылаем размер файла outD.writeUTF(f.getName());//отсылаем имя файла FileInputStream in = new FileInputStream(f); byte[] buffer = new byte[64 * 1024]; int count;//колличество отправленых байт while ((count = in.read(buffer)) != -1) { outD.write(buffer, 0, count); delFile(count,f); // вызываем метод удаления из отправляемого файла переданых байт } outD.flush(); in.close(); } client.close(); } catch (IOException e) { e.printStackTrace(); } } private void delFile(int count, File f) {//метод удаление байт из файла count1 = count;//колличество отправленых байт File folder = new File("/storage/emulated/0/Pictures/Photo_and_Video");// доступ к папке с файлами File[] listOfFiles = folder.listFiles();// получаем список файлов for (File f1 : listOfFiles) { if(f1.getName().equals(f.getName())){// сравниваем имена файлов отправленного и оставшегося delReadByte = count1 - f1.length();//получаем количество байт которое надо удалить из файла } } } 

    1 answer 1

    You cannot delete a fragment with regular files in any language.

    Therefore, simply open a new file, copy the beginning there (to the point of deletion or insertion), then either skip the fragment, or alternatively add it. Next, delete the old file and rename the new one. This is all done by standard means.