[英]Get content of every filetype in Java
What I want to do is first of all a kind of copy programm for some files: 我想做的首先是对某些文件的复制程序:
You type in a dictory, and somewhere it should be pasted. 您输入一个字典,然后将其粘贴到某处。
Everything works fine, except every filetype that is not .txt ! 一切正常,除了不是.txt的所有文件类型! All the word data can not be opened... all the png, jpg and gif datas can't be opened after the filetransfering!?
无法打开所有单词数据...文件传输后无法打开所有png,jpg和gif数据!
So here is what I got: 所以这就是我得到的:
@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();
}
}
}
This 这个
out.write(dis.readLine());
out.newLine();
is your problem. 是你的问题。 Not every file type is line-separated.
并非每种文件类型都是行分隔的。 I would simply perform a byte-by-byte copy in order to preserve the contents.
我将只执行一个字节一个字节的复制以保留内容。 Perhaps you could check out the many IOUtils
copy()
methods available ? 也许您可以检查出许多可用的IOUtils
copy()
方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.