[英]Writing specific number of bytes in file
I have the array listed below: 我有下面列出的数组:
I want to write these bytes in a file until the file reaches a size of 1029 bytes . 我想将这些字节写入文件,直到文件达到1029字节。 Any ideas?
有任何想法吗?
I tried this code but it does not seem to work : 我尝试了这段代码,但它似乎不起作用:
public static byte[] aSnouty = {97, 98, 99, 100, 101, 102, 103, 104, 105, 49, 45, 50, 51, 52, 53};
File file = new File(filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
do {
fos.write(aSnouty);
} while(file.length()!=1029);
} catch(Exception ex) {
ex.printStackTrace();
}
Here's a solution starting from the code you provided: 这是从您提供的代码开始的解决方案:
byte[] aSnouty = {97, 98, 99, 100, 101, 102, 103, 104, 105, 49, 45, 50, 51, 52, 53};
File file = new File("C:/users/BXM0121/testTenTwentyNine.txt");
FileOutputStream fos = new FileOutputStream(file);
int fileSizeInBytes = (int) file.length();
final int maxFileSize = 1029;
try {
do {
if ((maxFileSize - fileSizeInBytes) < aSnouty.length)
{
fos.write(Arrays.copyOfRange(aSnouty, 0, maxFileSize-fileSizeInBytes));
break;
}
fos.write(aSnouty);
fileSizeInBytes += aSnouty.length;
} while (file.length() != maxFileSize);
} catch(Exception ex) {
ex.printStackTrace();
} finally{
fos.close();
}
Edit: I see your comment about normal distribution and that's a whole different beast. 编辑:我看到你对正态分布的评论,那是完全不同的野兽。 I didn't code for that.
我没有为此编写代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.