服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java实现文件编码转换的方法

java实现文件编码转换的方法

2021-04-29 14:36MikanMu Java教程

这篇文章主要为大家详细介绍了java实现文件编码转换的方法,分享一个文件编码转换的工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在开发过程中,可能会遇到文件编码的转换,虽然说开发工具eclipse可以转换编码,但是有的情况却很不方便。比如,原来文件本身的编码是gbk,现在要转换成utf-8,如果直接在eclipse中把文件编码修改成utf-8,恭喜你,是乱码,因为不能直接从gbk到utf-8进行转换,这时就需要我们手动的来转换编码。下面是一个文件编码转换的工具类。

java" id="highlighter_86866">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.mikan.stuff;
 
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.filenamefilter;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.io.outputstreamwriter;
import java.nio.charset.charset;
import java.nio.charset.unsupportedcharsetexception;
 
public class filecharsetconverter {
 
  public static void main(string[] args) throws exception {
    convert("d:\\stuff\\src\\main\\java\\com\\mikan\\stuff\\test.txt",
        "gbk", "utf-8", new filenamefilter() {
          @override
          public boolean accept(file dir, string name) {
            return name.endswith("txt");
          }
        });
  }
 
  /**
   * 把指定文件或目录转换成指定的编码
   *
   * @param filename
   *      要转换的文件
   * @param fromcharsetname
   *      源文件的编码
   * @param tocharsetname
   *      要转换的编码
   * @throws exception
   */
  public static void convert(string filename, string fromcharsetname,
      string tocharsetname) throws exception {
    convert(new file(filename), fromcharsetname, tocharsetname, null);
  }
 
  /**
   * 把指定文件或目录转换成指定的编码
   *
   * @param file
   *      要转换的文件或目录
   * @param fromcharsetname
   *      源文件的编码
   * @param tocharsetname
   *      要转换的编码
   * @throws exception
   */
  public static void convert(file file, string fromcharsetname,
      string tocharsetname) throws exception {
    convert(file, fromcharsetname, tocharsetname, null);
  }
 
  /**
   * 把指定文件或目录转换成指定的编码
   *
   * @param file
   *      要转换的文件或目录
   * @param fromcharsetname
   *      源文件的编码
   * @param tocharsetname
   *      要转换的编码
   * @param filter
   *      文件名过滤器
   * @throws exception
   */
  public static void convert(string filename, string fromcharsetname,
      string tocharsetname, filenamefilter filter) throws exception {
    convert(new file(filename), fromcharsetname, tocharsetname, filter);
  }
 
  /**
   * 把指定文件或目录转换成指定的编码
   *
   * @param file
   *      要转换的文件或目录
   * @param fromcharsetname
   *      源文件的编码
   * @param tocharsetname
   *      要转换的编码
   * @param filter
   *      文件名过滤器
   * @throws exception
   */
  public static void convert(file file, string fromcharsetname,
      string tocharsetname, filenamefilter filter) throws exception {
    if (file.isdirectory()) {
      file[] filelist = null;
      if (filter == null) {
        filelist = file.listfiles();
      } else {
        filelist = file.listfiles(filter);
      }
      for (file f : filelist) {
        convert(f, fromcharsetname, tocharsetname, filter);
      }
    } else {
      if (filter == null
          || filter.accept(file.getparentfile(), file.getname())) {
        string filecontent = getfilecontentfromcharset(file,
            fromcharsetname);
        savefile2charset(file, tocharsetname, filecontent);
      }
    }
  }
 
  /**
   * 以指定编码方式读取文件,返回文件内容
   *
   * @param file
   *      要转换的文件
   * @param fromcharsetname
   *      源文件的编码
   * @return
   * @throws exception
   */
  public static string getfilecontentfromcharset(file file,
      string fromcharsetname) throws exception {
    if (!charset.issupported(fromcharsetname)) {
      throw new unsupportedcharsetexception(fromcharsetname);
    }
    inputstream inputstream = new fileinputstream(file);
    inputstreamreader reader = new inputstreamreader(inputstream,
        fromcharsetname);
    char[] chs = new char[(int) file.length()];
    reader.read(chs);
    string str = new string(chs).trim();
    reader.close();
    return str;
  }
 
  /**
   * 以指定编码方式写文本文件,存在会覆盖
   *
   * @param file
   *      要写入的文件
   * @param tocharsetname
   *      要转换的编码
   * @param content
   *      文件内容
   * @throws exception
   */
  public static void savefile2charset(file file, string tocharsetname,
      string content) throws exception {
    if (!charset.issupported(tocharsetname)) {
      throw new unsupportedcharsetexception(tocharsetname);
    }
    outputstream outputstream = new fileoutputstream(file);
    outputstreamwriter outwrite = new outputstreamwriter(outputstream,
        tocharsetname);
    outwrite.write(content);
    outwrite.close();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/mhmyqn/article/details/37917947

延伸 · 阅读

精彩推荐