Java:从.txt文件LINE BY LINE中读取字节 [英] Java: Reading bytes from a .txt file LINE BY LINE
问题描述
请查看以下代码
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文件。我不知道怎么做最后一件事。我怎样才能做到这一点?请帮忙!
In there, first I get the bytes of a file, and write them to a text file. Then I read that text file, read line by line, and generate a separate .txt file for each line. Now the original program is split into thousands of files. Now I need to read all the .txt files and re generate the .txt file. I don't know how to do the last thing. How can I do that? Please help!
推荐答案
如果你想操纵任何类型的文件,不要认为它们包含文本数据,并将它们视为二进制文件文件,包含字节。使用输入和输出流读取和写入二进制文件。
If you want to manipulate any kind of file, never consider they contain textual data, and consider them as binary files, containing bytes. Binary files are read and written using input and output streams.
这是一个读取文件并将其拆分为写入N个输出文件的1024个字节的方法的示例:
Here's an example of a method that reads a file and splits it into pieces of 1024 bytes written to N output files :
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:从.txt文件LINE BY LINE中读取字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!