1

我想做的首先是一些文件的一种复制程序:

你输入一个字典,它应该被粘贴到某个地方。

一切正常,除了不是 .txt 的每个文件类型!所有word数据都打不开……png、jpg、gif数据都打不开文件传输后!?

所以这就是我得到的:

@SuppressWarnings("deprecation")
static void crypt(String input, String output){
     File folder = new File(input);
     File[] listOfFiles = folder.listFiles();
     for (File file : listOfFiles){
         FileInputStream fis = null;
         BufferedInputStream bis = null;
         DataInputStream dis = null;
         System.out.println("['"+file.getName()+"' is copied]");
         try {
              fis = new FileInputStream(file);

              // Here BufferedInputStream is added for fast reading.
              bis = new BufferedInputStream(fis);
              dis = new DataInputStream(bis);
              FileWriter fstream;
              if(output.endsWith(file.separator) || output.endsWith("/")){  
                  fstream = new FileWriter(output+file.getName());
              }else{
                  fstream = new FileWriter(output+file.separator+file.getName());
              }
              BufferedWriter out = new BufferedWriter(fstream);


              //Close the output stream

              // dis.available() returns 0 if the file does not have more lines.
              while (dis.available() != 0) {

              // this statement reads the line from the file and print it to
                // the console.
                out.write(dis.readLine());
                out.newLine();
              }
              out.close();
              // dispose all the resources after using them.
              fis.close();
              bis.close();
              dis.close();

            } catch (FileNotFoundException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }

     }



 }
4

1 回答 1

0

    out.write(dis.readLine());
    out.newLine();

是你的问题。并非每种文件类型都是行分隔的。我将简单地执行逐字节复制以保留内容。也许您可以查看许多可用的IOUtils copy()方法?

于 2012-11-13T16:55:11.400 回答