Overview: There are a number of ways in which a program will sometimes need to read a string from a standard input as a file name. The first time I tried to write this program encountered a "garbled problem", and later used a new way to solve the garbled problem.
/*The code that you write in this way can cause a "garbled problem"
Garbled reason analysis: The data read from the standard input is stored in byte[], and then the byte array into a string, there will be a problem of encoding inconsistencies in the process
such as the standard input "file1", but the result of the final filename=buffer.tostring () is "@Bhjhj78i"
*/String FileName;//The frame data for the specific gesture is stored in the file named with the gesture byte[] buffer=New byte[50];//buffer, the file name (also known as the gesture name) that is used to store the standard input path inputSystem.out.println ("Enter the gesture name, the frame data corresponding to the gesture will be stored in the file named after the gesture name: for example, when the input is gesture1, the data will be stored in Gesture1.txt Pieces in the "); System.in.read (buffer); FileName=buffer.tostring () + ". txt";//Change to, change the program to the following form, no longer garbled problemSystem.out.println ("Enter the gesture name, the frame data corresponding to the gesture will be stored in the file named after the gesture name: for example, when the input is gesture1, the data will be stored in Gesture1.txt Pieces in the "); BufferedReader buf=NewBufferedReader (NewInputStreamReader (system.in)); FileName= Buf.readline () + ". txt";
Java Fundamentals---> File---> read string from standard input as file name----> fix "file name garbled" problem