java文件操作的一个摘录,还没看先存在这了

1. /**
2. * 给定一个源文件路径或者File和目标路径,把源文件路径下的的所有txt文件和文件夹拷贝到目标路径下
3. * 并且可以按照给定的编码输出txt文件
4. * @author myyate
5. *
6. */
7. public class Converter {
8. public Converter(String inPath, String outPath) throws Exception {
9. this(inPath, outPath, DEFAULT_ENCODING);
10. }
11. public Converter(String inPath, String outPath, String encoding)
12. throws Exception {
13. this(new File(inPath), new File(outPath), encoding);
14. }
15. public Converter(File inFile, File outFile) throws Exception {
16. this(inFile, outFile, DEFAULT_ENCODING);
17. }
18. public Converter(File inFile, File outFile, String encoding) throws Exception {
19. this.inFile = inFile;
20. this.outFile = outFile;
21. if(!outFile.exists()) {
22. outFile.mkdir();
23. }
24. this.encoding = encoding;
25. }
26.
27. public void convert() throws Exception {
28. copyFiles(inFile, outFile);
29. }
30.
31. private void copyFiles(File inDirectory, File outDirectory) throws Exception {
32. for(File file : inDirectory.listFiles(new TxtFileFilter())) {
33. File outFile = new File(outDirectory.getAbsolutePath(), file.getName());
34. if(file.isFile()) {
35. //如果是文件,就把文件拷贝到目标文件夹中
36. copySingleFile(file, outFile);
37. } else if(file.isDirectory()) {
38. if(!outFile.exists()) {
39. outFile.mkdir();
40. }
41. copyFiles(file, outFile);
42. }
43. }
44. }
45.
46. private void copySingleFile(File src, File des) throws Exception {
47. if(!des.exists()) {
48. des.createNewFile();
49. }
50. String encodingUse = ((encoding == null) ? DEFAULT_ENCODING : encoding);
51. Reader in =
52. new BufferedReader(new InputStreamReader(new FileInputStream(src)));
53. //设定输出文件的编码
54. Writer out =
55. new BufferedWriter(new OutputStreamWriter(new FileOutputStream (des), encodingUse));
56.
57. char[] buffer = new char[4096];
58. int readBytes = -1;
59. while((readBytes = in.read(buffer)) != -1) {
60. out.write(buffer, 0, readBytes);
61. }
62. out.flush();
63.
64. out.close();
65. in.close();
66. }
67.
68.
69. class TxtFileFilter implements FileFilter {
70.
71. public boolean accept(File pathname) {
72. if(pathname.isDirectory()) {
73. return true;
74. }
75.
76. String absolutePath = pathname.getAbsolutePath();
77. String suffix = absolutePath.substring(absolutePath.length() - 3);
78. //只拷贝txt文件
79. if("txt".equalsIgnoreCase(suffix)) {
80. return true;
81. }
82.
83. return false;
84. }
85. }
86.
87. private File inFile; //输入路径
88. private File outFile; //输出路径
89. private String encoding; //输出文件编码
90. private static String DEFAULT_ENCODING = "GBK";
91. }

测试代码如下:
java 代码

1. public class Test {
2.
3. static String IN_PATH = "F:/Novel";
4. static String OUT_PATH = "F:/NovelConvert";
5.
6. public static void main(String[] args) throws Exception {
7. Converter convert = new Converter(IN_PATH, OUT_PATH, "Unicode");
8. convert.convert();
9. }
10. }

程序是可以正常执行了,并且也转码了,不过不幸的是转成后的文件格式不是unicode,而是unicode big endian,拷贝到手机里还是乱码,nnd,现在也不管手机可否看了,我想问大家一下,如何修改才可以把txt文件改成unicode格式呢?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值