2014-01-11 17 views

回答

0

您可以使用RandomAccessFile。將文件指針移至所需的位置並重寫

+0

我已經找到它了。不管怎樣,謝謝你。 –

0

將它讀入內存,轉換,做你想做的任何事情,組裝一個完整的文件,並寫入它的磁盤中,替換舊的。

1

是的,您可以這樣做,只要某些文件可能會損壞。在示例中,我只複製實際文件中的一半字節,並將其寫入一個新文件,該文件將部分圖像寫入文件。

public static void main(String[] args) throws IOException { 
     File file = new File("D:\\Penguins.jpg"); 
     byte[] bFile = new byte[(int) file.length()]; 
     FileInputStream fileInputStream = new FileInputStream(file); 
     fileInputStream.read(bFile); 
     fileInputStream.close(); 

     byte[] newArray = Arrays.copyOf(bFile, (int) file.length()/2); 
     FileOutputStream out = new FileOutputStream("D:\\partialPenguins.jpg"); 
     out.write(newArray); 
     out.close(); 
    } 
相關問題