Java Fundamentals---> File---> read string from standard input as file name----> fix "file name garbled" problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.