获取Java中每个文件类型的内容

时间:2012-11-13 16:53:12

标签: image input copy bufferedreader file-type

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

你输入了一个胜利,它应该粘贴在某个地方。

一切正常,除了每个不是.txt的文件类型! 无法打开所有单词数据...文件传输后无法打开所有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();
            }

     }



 }

1 个答案:

答案 0 :(得分:0)

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

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