写入指定长度的字节到文件
程序员文章站
2024-02-26 14:22:58
...
使用java ,如何把指定长度的字节写入文件呢,或者说如何从inputStream 中读取指定长度的字节写入outputStream中呢?
/***
* write inputstream into file according to specified length.
*
* @param file
* @param ins
* : not closed
* @param length2
* @throws IOException
*/
public static FileOutputStream writeInputStream2File(File file,
InputStream ins, long length2, boolean isCloseOutputStream)
throws IOException {
String parentDir = SystemHWUtil.getParentDir(file.getAbsolutePath());
File fatherFile = new File(parentDir);
if (!fatherFile.exists()) {
fatherFile.mkdirs();
}
FileOutputStream outs = new FileOutputStream(file);
int readSize;
byte[] bytes = null;
bytes = new byte[(int) length2];
long length_tmp = length2;
while ((readSize = ins.read(bytes)) != SystemHWUtil.NEGATIVE_ONE/*-1*/) {
length_tmp -= readSize;
outs.write(bytes, 0, readSize);
if (length_tmp == 0) {
break;
}
//非常重要,千万不能去掉!!!
if (length_tmp < SystemHWUtil.BUFF_SIZE/*4096*/) {
bytes = new byte[(int) length_tmp];
}
}
outs.flush();
if (isCloseOutputStream) {
outs.close();
}
return outs;
}
/***
* Not responsible for closing the output and input stream 写入指定长度的字节到输出流
*
* @param fin
* @param fout
* : The divided file
* @param length2
* @throws IOException
*/
public static void writeFromFile2File(InputStream fin, OutputStream fout,
long length2) throws IOException {
if (length2 == 0) {// want to write zero bytes
// if (fout != null) {
// fout.close();
// }
return;
}
int readSize;
byte[] bytes = null;
if (length2 >= SystemHWUtil.BUFF_SIZE) {
bytes = new byte[SystemHWUtil.BUFF_SIZE];
} else {
bytes = new byte[(int) length2];
}
long length_tmp = length2;
while ((readSize = fin.read(bytes)) != SystemHWUtil.NEGATIVE_ONE) {
length_tmp -= readSize;
fout.write(bytes, 0, readSize);
if (length_tmp == 0) {
break;
}
//非常重要,千万不能删除
if (length_tmp < SystemHWUtil.BUFF_SIZE) {
bytes = new byte[(int) length_tmp];
}
}
}
/***
* Responsible for closing the output stream
*
* @param fin
* @param outPutFile
* @param length2
* :The number of bytes to be written
* @param append
* : Whether additional
* @throws IOException
*/
public static void writeFromFile2File(FileInputStream fin, File outPutFile,
long length2, boolean append) throws IOException {
if (length2 == 0) {// want to write zero bytes
return;
}
FileOutputStream fout = null;
try {
fout = new FileOutputStream(outPutFile, append/* 追加 */);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
writeFromFile2File(fin, fout, length2);
} catch (IOException e) {
e.printStackTrace();
} finally {
fout.flush();
fout.close();// Close the stream
}
}
上述代码见附件中io0007-find_progess\src\main\java\com\io\hw\file\util\FileUtils.java
依赖的包:is_chinese-0.0.2-SNAPSHOT.jar
参考:http://hw1287789687.iteye.com/blog/2023095
写入文件:
/***
* 写入文件
* @param content
* @param charset
* @param readAndWriteResult
* @param file
* @throws IOException
*/
private static void writeStubFile(String content, String charset, File file) throws IOException {
FileWriterWithEncoding fileW = new FileWriterWithEncoding(file, charset);
fileW.write(content);
fileW.close();
}