Java writing files is garbled
When we read and write file streams, we often encounter Of course, the cause of garbled codes cannot be the same. Here we mainly introduce the problem of garbled codes caused by the file encoding format. First, let’s clarify the concepts and differences between text files and binary files.
Text files are files based on character encoding. Common encodings include ASCII encoding, UNICODE encoding, ANSI encoding, etc. Binary files are files based on value encoding. You can specify what a certain value means according to the specific application (such a process can be regarded as custom encoding.)
So it can be seen that text files are basically It is fixed-length encoding (there are also non-fixed-length encodings such as UTF-8). Binary files can be regarded as variable-length encoding, because it is value encoding. How many bits represent a value is entirely up to you.
The specific operations are as follows:
Write the file in the format specified by the file
/** * 按照指定的路径和编码格式保存文件内容,这个方法因为用到了字符串作为载体,为了正确写入文件(不乱码),只能写入文本内容,安全方法 * * @param data * 将要写入到文件中的字节数据 * @param path * 文件路径,包含文件名 * @return boolean * 当写入完毕时返回true; */ public static boolean writeFile(byte data[], String path , String code){ boolean flag = true; OutputStreamWriter osw = null; try{ File file = new File(path); if(!file.exists()){ file = new File(file.getParent()); if(!file.exists()){ file.mkdirs(); } } if("asci".equals(code)){ code = "GBK"; } osw = new OutputStreamWriter(new FileOutputStream(path),code); osw.write(new String(data,code)); osw.flush(); }catch(Exception e){ e.printStackTrace(); log.info("toFile IO Exception:"+e.getMessage()); flag = false; }finally{ try{ if(osw != null){ osw.close(); } }catch(IOException e){ e.printStackTrace(); log.info("toFile IO Exception:"+e.getMessage()); flag = false; } } return flag; }
php Chinese website, a large number of free Java introductory tutorials, welcome online study!
The above is the detailed content of Java writes files that are garbled. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
