Rename a file using Java

Copied from http://exampledepot.8waytrips.com/egs/java.io/RenameFile.html // File (or directory) with old name File file = new File(“oldname”); // File (or directory) with new name File file2 = new File(“newname”); if (file2.exists()) throw new java.io.IOException(“file exists”); // Rename file (or directory) boolean success = file.renameTo(file2); if (!success) { // File was not successfully renamed } To append to the … Read more

Java can’t find file when running through Eclipse

The problem is most likely that your application is using a relative pathname. As @BalusC says, relative pathnames can be problematic. But IMO, he goes way too far when he says “[y]ou should never use relative paths in java.io stuff”. When an application opens a file using (for example) the FileInputStream(File) constructor, relative pathnames are … Read more

Why is “while ( !feof (file) )” always wrong

What is wrong with using feof() to control a read loop? For example: [c]#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *path = "stdin"; FILE *fp = argc > 1 ? fopen(path=argv[1], "r") : stdin; if( fp == NULL ){ perror(path); return EXIT_FAILURE; } while( !feof(fp) ){ /* THIS IS WRONG */ /* Read … Read more