如何在我的 try 和 catch 块中添加 'END'?

How to add an 'END' in my try and catch block?

我第一次在这里发帖。我目前在 Java 和学习方面经验不多。所以说到点子上。程序描述是:当用户键入 "END" 时,程序应该完成并将输入保存在 .txt 文件中。我的问题是我不知道如何添加 'End'。例如,我尝试使用 'while' 循环但出现错误,例如我的循环是无限的

while (mycontent != "END" || mycontent != "End") {
}

最好的方法是什么?块应该放在什么位置?谢谢你的时间

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Me {
public static void main(String[] args) {

   Scanner input = new Scanner(System.in);

   System.out.println("Please type your text and type 'END' to exit: ");

  BufferedWriter bw = null;

  try {

 String mycontent = input.nextLine();

 File file = new File("my.txt");


  if (!file.exists()) {
     file.createNewFile();
  }


  FileWriter fw = new FileWriter(file);
  bw = new BufferedWriter(fw);
  bw.write(mycontent);
      System.out.println("File written Successfully");

} catch (IOException ioe) {
   ioe.printStackTrace();
} 

finally
{ 
   try{
      if(bw!=null)
     bw.close();
   }catch(Exception ex){
       System.out.println("Error in closing the BufferedWriter"+ex);
    }
}
}
}

请使用.equals()方法检查两个字符串是否具有相同的值。

while(myContent!=null && myContent.equals("END")) {
    // Do something
}

在导入 org.apache.commons.lang.StringUtils 库后,您甚至可以使用 StringUtils 函数 IsEmpty 进行上述检查。