代码之家  ›  专栏  ›  技术社区  ›  user3662019

Java文件到upperCase和lowerUpper

  •  1
  • user3662019  · 技术社区  · 10 年前

    我正在尝试将目录中的所有文件名更改为不同大小写组合的小写和大写示例:TextFile==TextFile,在更改名称后,我想打开文件并使其中的所有文本与上面的大小写相同,但我不知道如何操作。我有将大小写更改为所需大小写的代码,但它使事情加倍,例如:TextFile==TetExtXtFifIlELe。。。我该怎么做呢?这是我迄今为止所做的代码

    import java.io.File;
    
    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() {
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    String name = null;
                    int value = 1;
                    // this for should loop trough current file name and change letters 
                    for (int j = 0; j < listOfFiles[i].getName().length(); j++) {
                        if (value == 1) {
                            name += Character.toLowerCase(listOfFiles[i].getName().charAt(j));
                            value = 2;
                        }
                        if (value == 2) {
                            name += Character.toUpperCase(listOfFiles[i].getName().charAt(j));
                            value = 1;
                        }
                    }
                    if (listOfFiles[i].renameTo(new File(folder, name))) {
                        // Here it should go into the file and make the content to the same type case as the name, But how can I open the file and do the same thing with the content as I do in the name?
                        System.out.println("Done");
                    } else {
                        System.out.println("Nope");
                    }
                }
            }
        }
    }
    
    2 回复  |  直到 10 年前
        1
  •  1
  •   Elliott Frisch    10 年前

    我建议您从创建一个助手方法开始,以混合示例1 String 一次-

    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 main(String[] args) {
        System.out.println(mixCase("TextFile"));
        System.out.println(mixCase("reverse"));
    }
    

    输出为

    tExTfIlE
    ReVeRsE
    
        2
  •  1
  •   Florent Bayle    10 年前

    代码存在一些问题:

    String name = null;
    

    不要从 null String ,否则,当您第一次将某个文件连接到它时,文件名将以“null”开头 一串 :

    String name = "";
    

    但是,你的主要问题是:

    if (value == 1) {
        // ...
        value = 2;
    }
    if (value == 2) {
        // ...
        value = 1;
    }
    

    您将始终输入这两个 if 当值为 1 ,因为在第一个 如果 块,将值更改为 2 ,并测试值是否为 2. 在第二块的开始处。如果你想这样做,你必须添加一个 else :

    if (value == 1) {
        // ...
        value = 2;
    } else if (value == 2) {
        // ...
        value = 1;
    }