2010-02-10 6 views

Répondre

1

Non. Copiez le reste du fichier dans un autre fichier, supprimez l'ancien fichier et renommez le nouveau fichier.

2

Vous pouvez le faire

/** 
* 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(); 
} 

Mais remarquez qu'il ne fonctionnera pas pour est casté en int très gros fichiers comme f.length(). Pour ceux que vous devriez utiliser l'approche habituelle de lire le fichier entier et de le jeter sur un autre.

Questions connexes