2010-02-10 59 views

回答

1

否。將文件的其餘部分複製到另一個文件中,刪除舊文件並重命名新文件。

2

你能做到這一點

/** 
* Replaces the caracter at the {@code index} position with the {@code newChar} 
* character 
* @param f file to modify 
* @param index of the character to replace 
* @param newChar new character 
* @throws FileNotFoundException if file does not exist 
* @throws IOException if something bad happens 
*/ 
private static void replaceChar(File f, int index, char newChar) 
     throws FileNotFoundException, IOException { 

    int fileLength = (int) f.length(); 

    if (index < 0 || index > fileLength - 1) { 
     throw new IllegalArgumentException("Invalid index " + index); 
    } 

    byte[] bt = new byte[(int) fileLength]; 
    FileInputStream fis = new FileInputStream(f); 
    fis.read(bt); 
    StringBuffer sb = new StringBuffer(new String(bt)); 

    sb.setCharAt(index, newChar); 

    FileOutputStream fos = new FileOutputStream(f); 
    fos.write(sb.toString().getBytes()); 
    fos.close(); 
} 

但是請注意,它不會非常大的文件爲f.length()被鑄造爲int工作。對於那些你應該使用通常的方法來閱讀整個文件並將其扔到另一個文件上。