如果您对java8将字节分成块感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于java8将字节分成块的详细内容,我们还将为您解答java字节拼接的相关问题,并且为您提供关于j
如果您对java 8将字节分成块感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于java 8将字节分成块的详细内容,我们还将为您解答java字节拼接的相关问题,并且为您提供关于java – 可以将字节设置为01作为值、java – 如何在仍然写入文件时将文件拆分成块?、java – 如何将浮点数转换为由字节分子和分母表示的最接近的分数?、java – 将字符串分成所有可能的4个字母的后续短语的有价值信息。
本文目录一览:- java 8将字节分成块(java字节拼接)
- java – 可以将字节设置为01作为值
- java – 如何在仍然写入文件时将文件拆分成块?
- java – 如何将浮点数转换为由字节分子和分母表示的最接近的分数?
- java – 将字符串分成所有可能的4个字母的后续短语
java 8将字节分成块(java字节拼接)
试试这个:
public List<byte[]> getFileChunks(byte[] mainFile) {
int sizeMB = 1 * 1024 * 1024;
List<byte[]> chunks = new ArrayList<>();
for (int i = 0; i < mainFile.length; ) {
byte[] chunk = new byte[Math.min(sizeMB,mainFile.length - i)];
for (int j = 0; j < chunk.length; j++,i++) {
chunk[j] = mainFile[i];
}
chunks.add(chunk);
}
return chunks;
}
或者如果你想要一个功能性的解决方案,那么试试这个:
public List<byte[]> getFileChunks(byte[] mainFile) {
final int sizeMB = 1 * 1024 * 1024;
return IntStream.iterate(0,i -> i + sizeMB)
.limit((mainFile.length + sizeMB - 1) / sizeMB)
.mapToObj(i -> Arrays.copyOfRange(mainFile,i,Math.min(i + sizeMB,mainFile.length)))
.collect(Collectors.toList());
}
java – 可以将字节设置为01作为值
public class example { private byte b1; public byte getB1() { return b1; } public void setB1(byte b1) { this.b1 = b1; } public static void main(String[] args) { final byte a = 01; example e = new example(); e.setB1(a); System.out.println(e.getB1()); } }
这个程序返回值为1而不是01,但是,我期待它为01(已设置)
如果设置为01,如何从字节获得01输出.
解决方法
我今年37岁.我也是025岁,045岁(八进制) – 他们是相同的数字,在不同的表示.数字本身没有表示的概念.
如果要以特定方式将字节转换为字符串,则应将其作为格式化的一部分 – 它不是与字节关联的数据的一部分.但是初始化它的值是相同的.
java – 如何在仍然写入文件时将文件拆分成块?
我试图从进程仍然使用该文件进行写入的文件创建字节数组块.实际上我将视频存储到文件中,我想在录制时从同一个文件创建块.
以下方法应该从文件中读取字节块:
private byte[] getBytesFromFile(File file) throws IOException{
InputStream is = new FileInputStream(file);
long length = file.length();
int numRead = 0;
byte[] bytes = new byte[(int)length - mReadOffset];
numRead = is.read(bytes, mReadOffset, bytes.length - mReadOffset);
if(numRead != (bytes.length - mReadOffset)){
throw new IOException("Could not completely read file " + file.getName());
}
mReadOffset += numRead;
is.close();
return bytes;
}
但问题是所有数组元素都设置为0,我想这是因为写入过程会锁定文件.
如果你们中的任何人在写入文件时能够显示任何其他创建文件块的方法,我将非常感激.
解决方法:
解决了这个问题:
private void getBytesFromFile(File file) throws IOException {
FileInputStream is = new FileInputStream(file); //videorecorder stores video to file
java.nio.channels.FileChannel fc = is.getChannel();
java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000);
int chunkCount = 0;
byte[] bytes;
while(fc.read(bb) >= 0){
bb.flip();
//save the part of the file into a chunk
bytes = bb.array();
storeByteArrayToFile(bytes, mRecordingFile + "." + chunkCount);//mRecordingFile is the (String)path to file
chunkCount++;
bb.clear();
}
}
private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException {
FileOutputStream fOut = new FileOutputStream(path);
try {
fOut.write(bytesToSave);
}
catch (Exception ex) {
Log.e("ERROR", ex.getMessage());
}
finally {
fOut.close();
}
}
java – 如何将浮点数转换为由字节分子和分母表示的最接近的分数?
这样做的原因是I2C设备需要分子和分母,而给它一个浮点数是有意义的.
例如,3.1415926535 …将导致245/78,而不是314/100或22/7.
在效率方面,这将在程序开始时调用三次,但之后根本不会.所以慢速算法也不算太糟糕.
解决方法
您可以在updateratio()方法中找到我的代码here,从第1161行开始.只要LGPL许可证适合您,您就可以使用它.我所做的基本上遵循了GIMP中所做的事情 – 这是其中一种只有一种有效,明智的方法.
java – 将字符串分成所有可能的4个字母的后续短语
>读一个文件;
>删除所有标点符号并将所有字母转换为小写;
>将单词转换为4个字母短语(如果单词短于4个字符,则将其作为一个整体);
例:
Input: Hello,my identification is Mister Dude.
Output: hell,ello,my,iden,dent,enti,ntif,tifi,ific,fica,icat,cati,atio,tion,is,mist,iste,ster,dude.
如果我能将每个4字短语作为数组中的单独值,那将是很好的.
现在我设法完成的事情:
public String[] OpenFile() throws IOException { FileReader fr = new FileReader(path); BufferedReader textReader = new BufferedReader(fr); int numberOfLines = readLines(); String[] textData = new String[numberOfLines]; int i; for (i = 0; i < numberOfLines; i++) { textData[i] = textReader.readLine(); textData[i] = textData[i].replaceAll("[^A-Za-ząčęėįšųūž]+"," ").toLowerCase(); } textReader.close(); return textData; }
textData [i]是我需要划分的每行文本.
我已经尝试了几种方法,例如.tochararray和2D数组,但我似乎无法管理字母排列部分.我怎样才能完成第3项任务?
解决方法
public static void main (String[] args) { String text = "Hello,my identification is Mister Dude."; String[] words = text.replaceAll("[^(\\w )]+","").toLowerCase().split(" "); for (String word : words) { if (word.length() <= 4) { System.out.println(word); } else { for (int i = 0; i <= word.length() - 4; i++) { System.out.println(word.substring(i,i + 4)); } } } }
今天的关于java 8将字节分成块和java字节拼接的分享已经结束,谢谢您的关注,如果想了解更多关于java – 可以将字节设置为01作为值、java – 如何在仍然写入文件时将文件拆分成块?、java – 如何将浮点数转换为由字节分子和分母表示的最接近的分数?、java – 将字符串分成所有可能的4个字母的后续短语的相关知识,请在本站进行查询。
本文标签: