Java:逐行从.txt文件读取字节
请看一下下面的代码Java:逐行从.txt文件读取字节,java,file,io,bytearray,byte,Java,File,Io,Bytearray,Byte,请看一下下面的代码 import java.io.*; import java.util.ArrayList; import java.util.List; public class FileCopy2 { public static void main(String[]args) { try { //First in here, we are trying to get the bytes
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileCopy2
{ public static void main(String[]args)
{
try
{
//First in here, we are trying to get the bytes from the file
File file = new File("D:/burn/preview.mp3"); //The File you need to get
byte bufferFile[] = new byte[(int)file.length()]; //Creating a byte array, which has the exact size of the file
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(file));// Creating Buffers for IO
bi.read(bufferFile, 0, bufferFile.length);//Reading the file
bi.close();//Closing Buffer
//Then in here, we are writing those bytes to a text file
BufferedOutputStream bu = new BufferedOutputStream(new FileOutputStream("C:/Users/Yohan/Desktop/test.txt")); //The output Location
bu.write(bufferFile, 0, bufferFile.length);//Writing the file
bu.flush();//Flushing the buffer
bu.close();//Closing the Buffer
System.out.println("Done Copieng");
//Here we are trying to WRITE number of .txt files containing the byte details..In other words, I am breaking the file into peaces
BufferedReader br = new BufferedReader(new FileReader("C:/Users/Yohan/Desktop/test.txt"));
BufferedWriter bw = null;
String content = "";
int count = 1;
while((content = br.readLine())!=null)
{
bw = new BufferedWriter(new FileWriter("C:/Users/Yohan/Desktop/output4/file"+count+".txt"));
bw.write(content);
bw.flush();
bw.close();
count++;
System.out.println("Running");
}
System.out.println("-----------------------Done-----------------------------");
//NOW I NEED TO read all the generated .txt file and create the original file back. How to do it? //
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
在那里,首先我得到一个文件的字节,并将它们写入一个文本文件。然后我读取该文本文件,逐行读取,并为每行生成一个单独的.txt文件现在,原始程序被分割成数千个文件。现在我需要读取所有的.txt文件并重新生成.txt文件。最后一件事我不知道怎么做。我该怎么做?请帮忙如果您想操纵任何类型的文件,不要考虑它们包含文本数据,并将它们视为包含字节的二进制文件。二进制文件使用输入和输出流进行读写 下面是一个方法示例,该方法读取文件并将其拆分为写入N个输出文件的1024字节片段:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileSplit {
public static void main(String[] args) throws IOException {
new FileSplit().splitFile(new File(args[0]));
}
private void splitFile(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
try {
byte[] buffer = new byte[1024];
// remaining is the number of bytes to read to fill the buffer
int remaining = buffer.length;
// block number is incremented each time a block of 1024 bytes is read
//and written
int blockNumber = 1;
while (true) {
int read = fis.read(buffer, buffer.length - remaining, remaining);
if (read >= 0) { // some bytes were read
remaining -= read;
if (remaining == 0) { // the buffer is full
writeBlock(blockNumber, buffer, buffer.length - remaining);
blockNumber++;
remaining = buffer.length;
}
}
else {
// the end of the file was reached. If some bytes are in the buffer
// they are written to the last output file
if (remaining < buffer.length) {
writeBlock(blockNumber, buffer, buffer.length - remaining);
}
break;
}
}
}
finally {
fis.close();
}
}
private void writeBlock(int blockNumber, byte[] buffer, int length) throws IOException {
FileOutputStream fos = new FileOutputStream("output_" + blockNumber + ".dat");
try {
fos.write(buffer, 0, length);
}
finally {
fos.close();
}
}
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
公共类文件碎片{
公共静态void main(字符串[]args)引发IOException{
新的FileSplit().splitFile(新文件(args[0]);
}
私有void splitFile(文件文件)引发IOException{
FileInputStream fis=新的FileInputStream(文件);
试一试{
字节[]缓冲区=新字节[1024];
//剩余是要读取以填充缓冲区的字节数
int剩余=buffer.length;
//每次读取1024字节的块时,块编号都会递增
//写
int blockNumber=1;
while(true){
int read=fis.read(buffer,buffer.length-剩余,剩余);
如果(读取>=0){//读取了一些字节
剩余-=读取;
如果(剩余==0){//,则缓冲区已满
writeBlock(blockNumber、buffer、buffer.length-剩余);
blockNumber++;
剩余=缓冲区长度;
}
}
否则{
//已到达文件结尾。如果缓冲区中有一些字节
//它们被写入最后一个输出文件
if(剩余<缓冲区长度){
writeBlock(blockNumber、buffer、buffer.length-剩余);
}
打破
}
}
}
最后{
fis.close();
}
}
私有void writeBlock(int blockNumber,byte[]buffer,int length)引发IOException{
FileOutputStream fos=新的FileOutputStream(“output_“+blockNumber+”.dat”);
试一试{
fos.写入(缓冲区,0,长度);
}
最后{
fos.close();
}
}
}
这个程序没有意义。mp3文件包含二进制数据,而不是文本数据。把它当作文本来写和读是没有意义的。此外,您绝对不能保证读取所有文件,因为您忽略了read()调用的结果,并且在读取所有文件之前不会循环。最后,您不会在finally块中关闭流。谷歌搜索“Java IO教程”。@Polygenme:你的答案是什么?@JBNizet:我知道它包含二进制数据,这就是我得到字节的原因。如果更改文件参数(这意味着将“C:/Users/Yohan/Desktop/test.txt”指定给第一个块,将“D:/burn/preview.mp3”指定给第二个块),首先运行此代码后。它将重新生成原始文件,我已经对其进行了测试,并且一直有效。如果您知道,请提供答案。仅仅因为您可以将任何编码字符序列转换为字节序列并不意味着您可以将任何字节序列转换为有效的编码字符序列。您的程序毫无意义,您的问题也没有有效的答案。假设您的编码是ASCII,它将128到255之间的所有字节视为无效字符。您试图将包含这些字节的mp3文件转换为ASCII,因此您将丢失mp3文件的一半字节。如果您将规范更改为文本仅文件,对于输入和输出,人们都可以提供答案。即使如此,您所做的也没有什么意义。为什么要将文件分解到每一行中,只是为了重新组装它?通过所有这些努力,您试图提供什么程序功能?首先,我请求您对延迟表示歉意。我很高兴看到一个代码示例!请给出请给我一天时间来检查这是否是我正在寻找的文件。我现在无法这样做,因为现在是凌晨1点:)哇..好..这就是答案。非常感谢。我真的很感激:)在这个程序中,如果文件只有1000字节,在第一次迭代中它将读取1000字节,而剩余的是1024字节。因此剩余-=读取将使剩余为24字节。因此,如果条件(剩余==0)为假,它将转到下一次迭代而不写入。下一次迭代读取将为-1,因此循环中断。因此它将关闭输入流而不写入已读取的内容。这是我的疑问,如果我错了,请纠正我(以柔和的音调plss)不。第二次迭代将读取-1,因此else块将被执行,else块将写入已读取的内容,然后只有循环将结束。测试它。太好了!这是一个很好的答案,代码显示了问题所需的所有内容,我也在搜索类似的内容,现在找到了。。