Search code examples
javafile-iofile-extensionmixed-case

Java I/O writing, mixingcase and getExtension problems


I am changing names of all files in directory and if it's text file I am changing the content but it doesn't seem to work the name of the file is changed right but content is blank/gone heres is my code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.io.FilenameUtils;

public class FileOps {
    public static File folder = new File(
            "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files");
    public static File[] listOfFiles = folder.listFiles();

    public static void main(String[] argv) throws IOException {
        toUpperCase();
    }

    public static void toUpperCase() throws FileNotFoundException {
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                String newname = mixCase(listOfFiles[i].getName());

                if (listOfFiles[i].renameTo(new File(folder, newname))) {
                    String extension = FilenameUtils
                            .getExtension(listOfFiles[i].getName());
                    if (extension.equals("txt") || extension.equals("pdf")
                            || extension.equals("docx")
                            || extension.equals("log")) {
                        rewrite(listOfFiles[i]);

                        System.out.println("Done");
                    }

                }
            } else {
                System.out.println("Nope");
            }
        }
    }

    public static String mixCase(String in) {
        StringBuilder sb = new StringBuilder();
        if (in != null) {
            char[] arr = in.toCharArray();
            if (arr.length > 0) {
                char f = arr[0];
                boolean first = Character.isUpperCase(f);
                for (int i = 0; i < arr.length; i++) {
                    sb.append((first) ? Character.toLowerCase(arr[i])
                            : Character.toUpperCase(arr[i]));
                    first = !first;
                }
            }
        }
        return sb.toString();
    }

    public static void rewrite(File file) throws FileNotFoundException {
        FileReader reader = new FileReader(file.getAbsolutePath());
        BufferedReader inFile = new BufferedReader(reader);

        try {
            FileWriter fwriter = new FileWriter(file.getAbsolutePath());
            BufferedWriter outw = new BufferedWriter(fwriter);
            while (inFile.readLine() != null) {
                String line = mixCase(inFile.readLine());
                outw.write(line);
            }
            inFile.close();
            outw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Solution

  • There is several issue with your code:

    1. Your rewrite function is perform on old name File. It should be done on the renamed File:

          String newname = mixCase(listOfFiles[i].getName());
          File renamedFile = new File(folder, newname);
          if (listOfFiles[i].renameTo(renamedFile )) {
              String extension = FilenameUtils
                      .getExtension(listOfFiles[i].getName());
              if (extension.equals("txt") || extension.equals("pdf")
                      || extension.equals("docx")
                      || extension.equals("log")) {
                  rewrite(renamedFile);
      
                  System.out.println("Done");
              }
      
          }
      
    2. you are trying to read docx and pdf file like regular text file. This cannot work. You will have to use external library like POI and pdf Box

    3. Your rewrite function is not safe. You must unsure to close the ressources:

       FileReader reader = null;
       BufferedReader inFile = null;
      
      try {
      reader = new FileReader(file.getAbsolutePath());
      inFile = new BufferedReader(reader);
      FileWriter fwriter = new FileWriter(file.getAbsolutePath());
      BufferedWriter outw = new BufferedWriter(fwriter);
      
      while (inFile.readLine() != null) {
          String line = mixCase(inFile.readLine());
          outw.write(line);
      }
      
      } catch (IOException e) {
      e.printStackTrace();
      }finally
      {
         if(infile != null)
         inFile.close();
      
         if(reader != null)
         reader .close();
      }