search
HomeJavaJavaBaseSolution to garbled code in java reading and writing html files

Solution to garbled code in java reading and writing html files

Dec 06, 2019 pm 04:16 PM
javaGarbled characters

Solution to garbled code in java reading and writing html files

1、JAVA读取文件,避免中文乱码。

/**
  * 读取文件内容
  *
  * @param filePathAndName
  *            String 如 c:\\1.txt 绝对路径
  * @return boolean
  */
public static String readFile(String filePathAndName) {
  String fileContent = "";
  try { 
   File f = new File(filePathAndName);
   if(f.isFile()&&f.exists()){
    InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");
    BufferedReader reader=new BufferedReader(read);
    String line;
    while ((line = reader.readLine()) != null) {
     fileContent += line+"\n";
    }  
    read.close();
   }
  } catch (Exception e) {
   System.out.println("读取文件内容操作出错");
   e.printStackTrace();
  }
  return fileContent;
}

2、JAVA写入文件,避免中文乱码。

public static void writeFile(String filePathAndName, String fileContent) {
  try {
   File f = new File(filePathAndName);
   if (!f.exists()) {
    f.createNewFile();
   }
   OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
   BufferedWriter writer=new BufferedWriter(write);  
   //PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePathAndName)));
   //PrintWriter writer = new PrintWriter(new FileWriter(filePathAndName));
   writer.write(fileContent);
   writer.close();
  } catch (Exception e) {
   System.out.println("写文件内容操作出错");
   e.printStackTrace();
  }
}

若写入的时候用 

1、PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePathAndName))); 

2、PrintWriter writer = new PrintWriter(new FileWriter(filePathAndName)); 

都会出现错误,不行。 

p.s. 我刚开始用上述方法的时候还是出先乱码,后来发现是因为我的html文件的编码方式是不是utf-8,改成utf-8即可。

查看一个文件的编码方式,一个简单的办法是:用记事本打开它,然后另存为一个副本文件,在“另存为”的页面下方,“保存”按钮前面,如下图所示,会出现原文件的编码方式。

如果不是UTF-8,把它改成UTF-8,保存即可。

更多java知识请关注java基础教程栏目。

The above is the detailed content of Solution to garbled code in java reading and writing html files. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use