2013-10-23 4 views
2

미리 할당 된 크기의 RandomAccessFile을 사용하여 파일을 만들었습니다. 그러나 내가 FileOutputStream을 쓰고있을 때 파일의 크기가 바뀌고 있습니다. 이 모드를 만들에 파일을 여는 있기 때문에 파일 크기를 변경하는 것처럼 FileOutputStreamFileOutputStream은 파일 크기를 0으로 줄입니다.

import java.io.File; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.ObjectOutputStream; 
    import java.io.RandomAccessFile; 
    import java.io.Serializable; 

    public class testFileSize { 

     public static class Status implements Serializable { 

     } 

     public static void preAllocate(String path, long maxSize, boolean preAllocate) 
      throws IOException { 
     RandomAccessFile raf = new RandomAccessFile(path, "rw"); 
     try { 
      raf.setLength(maxSize); 
     } finally { 
      raf.close(); 
     } 
     } 

     /** 
     * @param args 
     * @throws IOException 
     */ 
     public static void main(String[] args) throws IOException { 
     FileOutputStream fileOutput = null; 
     ObjectOutputStream objectOutput = null; 
     try { 
      final File f = new File("/tmp/test.bin");  
      preBlow(f.getAbsolutePath(), 2048, false); 
      Status s = new Status(); 
      fileOutput = new FileOutputStream(f); 
      objectOutput = new ObjectOutputStream(fileOutput); 
      objectOutput.writeObject(new Status()); 
      objectOutput.flush(); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      objectOutput.close(); 
      fileOutput.close(); 
     } 

     } 

    } 
+3

힌트를 구축 할 경우에 추가 boolean 플래그를 사용하여, append 모드에서 파일 : fileou를위한 JavaDoc를 읽어 tputstream 및 사용 가능한 생성자를 살펴 봅니다. –

답변

2

이 보이는 사용이 중지 할 수있는 방법이 때문에 이전 내용은

fileOutput = new FileOutputStream(f); 

을 잃게됩니다 시도 개방하여 당신의 FileOutputStream

fileOutput = new FileOutputStream(f, true); 
+0

고마워,하지만 추가하고 싶지 않다면, 덧붙여서 피할 수있는 방법이있다 – Avinash

+0

다음과 같이하면 {{{RandomAccessFile raFile = new RandomAccessFile (f.getAbsolutePath(), "rw"); fileOutput = 새 FileOutputStream (raFile.getFD());}}} – Avinash

관련 문제