Read * .properties files. Read only once. After reading the file, work with it through the console until you press the ESC key.

I know that it is slightly clumsy written, but so far all I can ...

Example code main class:

import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import java.util.Scanner; public class main { public static void main(String[] args) { boolean bError = true; Mykey a = new Mykey(); boolean enabled = a.keyPressed(); //ΠΊΠ°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ Ρ‚ΡƒΡ‚ Π½Π°Π΄ΠΎ взаимодСйствиС ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Properties prop; do { System.out.println("Enter name file: "); Scanner config = new Scanner(System.in); String c = config.nextLine(); final String PATH_TO_PROPERTIES = "src/" + c + ".properties"; FileInputStream fileInputStream; prop = new Properties(); try { fileInputStream = new FileInputStream(PATH_TO_PROPERTIES); bError = false; prop.load(fileInputStream); } catch (IOException e) { System.out.println("Ошибка Π² ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ΅: Ρ„Π°ΠΉΠ» " + PATH_TO_PROPERTIES + " Π½Π΅ ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠ΅Π½ΠΎ"); e.printStackTrace(); } } while (bError); while (enabled) { //Ρ†ΠΈΠΊΠ» связанный с KeyEvent (ΠΏΠΎΠΊΠ° Π½Π΅ true ΠΈΠ»ΠΈ false - ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΡΡ‚ΡŒ System.out.println("Enter key1, key2: "); Scanner key1 = new Scanner(System.in); Scanner key2 = new Scanner(System.in); String k1 = prop.getProperty(key1.nextLine()); String k2 = prop.getProperty(key2.nextLine()); if (k1 == null) { System.out.println("ΠŸΠ΅Ρ€Π²Ρ‹ΠΉ ΠΊΠ»ΡŽΡ‡ Π·Π°Π΄Π°Π½ Π½Π΅ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ\n"); } if (k2 == null) { System.out.println("Π’Ρ‚ΠΎΡ€ΠΎΠΉ ΠΊΠ»ΡŽΡ‡ Π·Π°Π΄Π°Π½ Π½Π΅ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ\n"); } System.out.println("key1: " + k1 + "\nkey2: " + k2); } // System.exit(0); } } 

Example of key handler code:

 import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Mykey implements KeyListener { boolean enabled = false; @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { enabled = true; //return; } } @Override public void keyReleased(KeyEvent e) { } } 
  • one
    As far as I remember, the console cannot catch esc. You need a different approach with the gui garter - JVic
  • When working through the console, the user will need to type esc, and you will have to catch and exit. - ezhov_da
  • That is, there is an opportunity only through the cycle and use of the "Scanner" ... I just thought suddenly there was without entering three characters and reading, but I pressed the key and she herself was considered ... - Victor

1 answer 1

As JVic correctly noted, the console java program will not be able to react to pressing the escape key. But the program could react to C. The bridge to this opportunity can be prokinut through JNI . I do not think that this is a suitable solution to this problem , but I will nevertheless cite it for educational purposes and indulgence for the sake of it. First of all, we need java-code itself. Put it in ExitOnEscape.java :

 public class ExitOnEscape implements Runnable { private volatile boolean doTheStuff = true; // Π€Π»Π°Π³ активности Ρ€Π°Π±ΠΎΡ‡Π΅Π³ΠΎ ΠΏΠΎΡ‚ΠΎΠΊΠ° public native boolean readConsole(); public void run() { // Π Π°Π±ΠΎΡ‡ΠΈΠΉ ΠΏΠΎΡ‚ΠΎΠΊ while (doTheStuff) { // Π Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ ΠΏΠΎΠΊΠ° установлСн Ρ„Π»Π°Π³ System.out.println("Doing something..."); // Основная Π»ΠΎΠ³ΠΈΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ try { Thread.sleep(500); } catch (InterruptedException exc) {} } } public void doSomething() { new Thread(this).start(); // ЗапускаСм Ρ€Π°Π±ΠΎΡ‡ΠΈΠΉ ΠΏΠΎΡ‚ΠΎΠΊ while(true) { // Π’ Π³Π»Π°Π²Π½ΠΎΠΌ ΠΏΠΎΡ‚ΠΎΠΊΠ΅ Ρ‡ΠΈΡ‚Π°Π΅ΠΌ консоль Π½Π°Ρ‚ΠΈΠ²Π½ΠΎΠΉ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ Π² бСсконСчном Ρ†ΠΈΠΊΠ»Π΅ if(readConsole()) { // Если Π½Π°ΠΆΠ°Ρ‚ escape doTheStuff = false; // БбрасываСм Ρ„Π»Π°Π³ активности break; // И Π²Ρ‹Ρ…ΠΎΠ΄ΠΈΠΌ ΠΈΠ· Ρ†ΠΈΠΊΠ»Π° } } } public static void main(String[] args) { System.loadLibrary("ExitOnEscape"); ExitOnEscape exitOnEscape = new ExitOnEscape(); exitOnEscape.doSomething(); } } 

As you can see, there are only two lines in the code that differ from what you have to face daily. This is the declaration of the native method.

 public native boolean readConsole(); 

and loading the library

 System.loadLibrary("ExitOnEscape"); 

Compile it:

 javac -h . ExitOnEscape.java 

The -h switch indicates which directory to generate the header file for our future library. The ExitOnEscape.h file should appear in the working directory with the following contents:

 /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class ExitOnEscape */ #ifndef _Included_ExitOnEscape #define _Included_ExitOnEscape #ifdef __cplusplus extern "C" { #endif /* * Class: ExitOnEscape * Method: readConsole * Signature: ()Z */ JNIEXPORT jboolean JNICALL Java_ExitOnEscape_readConsole (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif 

It is only interesting to declare a function that looks like Java_<имя класса>_<имя ΠΌΠ΅Ρ‚ΠΎΠ΄Π°> . The first parameter of the JNIEnv function is a pointer to a table of functions of the JNI mechanism, which are used to provide interaction between java- and c-code. The second parameter is of type jobject and accepts an instance of the ExitOnEscape class. Returns the jboolean function corresponding to the boolean type in java. JNIEXPORT and JNICALL are compiler-dependent macro definitions for exporting functions; they do not deserve special attention.

And now the most interesting part is the implementation of this function. We describe it in the ExitOnEscape.c file:

 #include <stdio.h> #include <conio.h> #include "ExitOnEscape.h" JNIEXPORT jboolean JNICALL Java_ExitOnEscape_readConsole(JNIEnv* env, jobject obj) { char c = getch(); // Π‘Ρ‡ΠΈΡ‚Ρ‹Π²Π°Π΅ΠΌ Π²Π²Π΅Π΄Ρ‘Π½Π½Ρ‹ΠΉ символ if (c == 27) // Если это escape return JNI_TRUE; // Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ true else return JNI_FALSE; // Π˜Π½Π°Ρ‡Π΅ false } 

To compile on Windows, I used MinGW-w64 :

 gcc -Wl,--add-stdcall-alias ^ -I"%JAVA_HOME%\include" ^ -I"%JAVA_HOME%\include\win32" ^ -shared -o ExitOnEscape.dll ExitOnEscape.c 

The ExitOnEscape.dll file should appear in the working directory. After that, you can run the java-program with the command java ExitOnEscape

  • I am a mimocrocodile, but it is a tin, like shooting a fly from a BFG :) - goldstar_labs
  • The solution is very interesting) Alas, to use a bunch of two languages ​​in my case is not an option - I study java and the implementation is only on it - Victor
  • therefore ... Yes, and let me tell you the truth - it is a little hard for me and I would have thought of this only very well and sat for a long time over the mat. part - the teacher will immediately see through. I took a much simpler solution with checking the entered phrase, although I thought that "KeyEvent" is such a panacea and works everywhere) - Victor
  • @goldstar_labs I came to java-development through JNI and nostalgia often attacks me :) - Sergey Gornostaev
  • @Victor, you now know about this feature of Java and can casually mention it in front of the teacher. Perhaps he will appreciate :) And maybe just sometime come in handy. - Sergey Gornostaev