Eclipse java字符大小写

Eclipse java字符大小写,eclipse,Eclipse,每个字符应在大写和小写之间切换。我的问题是我不能让它正常工作。这就是我到目前为止所做的: oneLine = br.readLine(); while (oneLine != null){ // Until the line is not empty (will be when you reach End of file) System.out.println (oneLine); // Print it in screen

每个字符应在大写和小写之间切换。我的问题是我不能让它正常工作。这就是我到目前为止所做的:

        oneLine = br.readLine();
        while (oneLine != null){  // Until the line is not empty (will be when you reach End of file)
            System.out.println (oneLine);    // Print it in screen
            bw.write(oneLine); // Write the line in the output file 
            oneLine = br.readLine(); // read the next line
        }
        int ch;
        while ((ch = br.read()) != -1){

            if (Character.isUpperCase(ch)){
                Character.toLowerCase(ch);

            }
            bw.write(ch);


        }

给你。您遇到了一些问题:

  • 您从未实际存储字符大小写切换的结果
  • 您需要将换行符与每行一起保存
  • 为了便于阅读,我打开了箱子开关
  • 以下是修改后的代码:

      public static void main(String args[]) {
        String inputfileName = "input.txt"; // A file with some text in it
        String outputfileName = "output.txt"; // File created by this program
        String oneLine;
    
        try {
          // Open the input file
          FileReader fr = new FileReader(inputfileName);
          BufferedReader br = new BufferedReader(fr);
    
          // Create the output file
          FileWriter fw = new FileWriter(outputfileName);
          BufferedWriter bw = new BufferedWriter(fw);
    
          // Read the first line
          oneLine = br.readLine();
          while (oneLine != null) { // Until the line is not empty (will be when you reach End of file)
            String switched = switchCase(oneLine); //switch case
            System.out.println(oneLine + " > "+switched); // Print it in screen
            bw.write(switched+"\n"); // Write the line in the output file
            oneLine = br.readLine(); // read the next line
          }
    
          // Close the streams
          br.close();
          bw.close();
        } catch (Exception e) {
          System.err.println("Error: " + e.getMessage());
        }
      }
    
      public static String switchCase(String string) {
        String r = "";
        for (char c : string.toCharArray()) {
          if (Character.isUpperCase(c)) {
            r += Character.toLowerCase(c);
          } else {
            r += Character.toUpperCase(c);
          }
        }
        return r;
      }
    

    这段代码是否有效(即编译和运行时没有运行时错误)?它有什么作用?你认为它为什么这样做?
    $tr'A-Za-z'A-Za-z'output.txt