Home>

I want to keep typing until x is entered, and when I enter x, I want to overwrite the file with the characters entered so far.

Execution results
Please enter something.
234
Unable to write to file
Please enter something.</Code></pre>
<strong>Applicable source code</strong>
<pre><code>package ex07;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class LooplnputWrite {
public static void main (String args []) {
      while (true) {
    try {
      File file = new File ("C: //java//test01.txt");
      if (checkBeforeWritefile (file)) {
        @SuppressWarnings ("resource")
        FileWriter filewriter = new FileWriter (file);
          InputStreamReader is = new InputStreamReader (System.in);
        BufferedReader br = new BufferedReader (is);

          System.out.println ("Please enter something");
          String str = br.readLine ();
          filewriter.write (str + "\ r \ n");

        if (str.equals ("×")) {
            System.out.println ("Finished.");
            filewriter.close ();
            break;
      } else {
        System.out.println ("Cannot write to file");
      }
      }
    } catch (IOException e) {
      System.out.println (e);
    }
  }
  }

  private static boolean checkBeforeWritefile (File file) {
    if (file.exists ()) {
      if (file.isFile ()&&file.canWrite ()) {
        return true;
      }
    }
    return false;
  }
}
  • Answer # 1

      

    Exception in thread "main" java.lang.NullPointerException
          at ex07.LooplnputWrite.main (LooplnputWrite.java:29)

    It is said that it is

    , so if you look at the code around it, you should just say it should fall.

    ...
            filewriter.close ();
    // !!!!? ? ?
            Object inputValue = null;
            if (inputValue.equals ("×")) {
                System.out.println ("Finished.");
    ...

  • Answer # 2

    What do you want to do?

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class Main {
        public static void main (String args []) throws IOException {
            File file = new File ("C: //java//test01.txt");
            if (checkBeforeWritefile (file)) {
                FileWriter filewriter = new FileWriter (file);
                InputStreamReader is = new InputStreamReader (System.in);
                BufferedReader br = new BufferedReader (is);
                while (true) {
                    System.out.println ("Enter something. End with ×.");
                    String str = br.readLine ();
                    if (str.equals ("×")) {
                        System.out.println ("Finished.");
                        break;
                    }
                    filewriter.write (str + "\ r \ n");
                }
                filewriter.close ();
            } else {
                System.out.println ("Cannot write to file");
            }
        }
        private static boolean checkBeforeWritefile (File file) {
            if (file.exists ()) {
                if (file.isFile ()&&file.canWrite ()) {
                    return true;
                }
            }
            return false;
        }
    }

    Loops are less likely to be troublesome if they are kept to a minimum.

  • Answer # 3

    if (str.equals ("×")) {
                System.out.println ("Finished.");
                filewriter.close ();
                break;
          } else {
            System.out.println ("Cannot write to file");
          }


    If it is "x", it is finished, otherwise it is "cannot write to file".
    However, since the writing process itself is before this, it is almost meaningless.

Related articles