黑马程序员技术交流社区

标题: 复制全是文本文件的文件夹并转换编码出现问题? [打印本页]

作者: 够了没有    时间: 2015-10-30 13:43
标题: 复制全是文本文件的文件夹并转换编码出现问题?
本帖最后由 够了没有 于 2015-10-30 16:21 编辑

我的项目刚开始是GBK格式编码的,现在想把src文件夹里所有文本文件转成UTF-8编码格式,但是运行却报错了。
错误提示:
java.io.FileNotFoundException: H:\exam\src (拒绝访问。)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at com.itheima.CopyFold_My.copyFile(CopyFold_My.java:56)
        at com.itheima.CopyFold_My.copyFolder(CopyFold_My.java:33)
        at com.itheima.CopyFold_My.copyFolder(CopyFold_My.java:36)
        at com.itheima.CopyFold_My.copyFolder(CopyFold_My.java:36)
        at com.itheima.CopyFold_My.main(CopyFold_My.java:19)
为什么会出现这种错误呢?

  1. package com.itheima;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;

  8. public class CopyFold_My
  9. {
  10.         static String charset;
  11.         static String newFold;
  12.         static String destinationFold = "H:/exam/src/copy";
  13.         static File fileSrc = new File("H:/exam/src");

  14.         public static void main(String[] args)
  15.         {
  16.                 copyFolder(fileSrc);
  17.         }
  18.         public static void copyFolder(File fileSrc)
  19.         {
  20.                 if (fileSrc.isFile())
  21.                 {
  22.                         copyFile(fileSrc, new File(destinationFold + File.separator + fileSrc.getName()));
  23.                 } else
  24.                 {
  25.                         File[] listFiles = fileSrc.listFiles();
  26.                         for (File file : listFiles)
  27.                         {
  28.                                 if (file.isFile())
  29.                                 {
  30.                                         copyFile(file, new File(destinationFold + File.separator + fileSrc.getName() + File.separator + file.getName()));
  31.                                 } else
  32.                                 {
  33.                                         copyFolder(file);
  34.                                 }
  35.                         }
  36.                 }
  37.         }
  38.         public static void copyFile(File src, File destination)
  39.         {
  40.                 try
  41.                 {
  42.                         InputStream in = new java.io.FileInputStream(src);
  43.                         byte[] b = new byte[3];
  44.                         in.read(b);
  45.                         in.close();
  46.                         if (b[0] == -17 && b[1] == -69 && b[2] == -65)
  47.                         {
  48.                                 charset = "UTF-8";
  49.                         } else
  50.                         {
  51.                                 charset = "GBK";
  52.                         }
  53.                         InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fileSrc), charset);
  54.                         OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(destination), "UTF-8");
  55.                         char[] ch = new char[1024];
  56.                         while (inputStreamReader.read(ch) != -1)
  57.                         {
  58.                                 outputStreamWriter.write(ch);
  59.                         }
  60.                         outputStreamWriter.flush();
  61.                         outputStreamWriter.close();
  62.                         inputStreamReader.close();
  63.                 } catch (Exception e)
  64.                 {
  65.                         e.printStackTrace();
  66.                 }
  67.         }
  68. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2