字节数组未正确打印到文件
Byte Array not printing to file correctly
我正在创建一个程序,旨在接收 n 次拆分并将文件拆分为该数量的子文件。在我的 SplitFile.java 中,我正在读取一个文件,传递一个子字符串数组,这些子字符串显示每个拆分文件中应该包含的文本。然后我将字符串转换为字节数组并将字节数组写入拆分文件,但我创建的每个文件输出的内容 只是 略有不同。
SplitFile.java
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class SplitFile
{
int numberOfSplits = 1;
File file;
String[] parts = new String[5];
public SplitFile(File file,int numberOfSplits)
{
this.file = file;
this.numberOfSplits = numberOfSplits;
}
public void FileSplitter() throws IOException
{
FileInputStream fis = new FileInputStream(file);
String fileText = readFile();
if(numberOfSplits == 2)
{
int mid = fileText.length() / 2;
parts[0] = fileText.substring(0, mid);
parts[1] = fileText.substring(mid);
}
else if(numberOfSplits == 3)
{
int third = fileText.length() / 3;
int secondThird = third + third;
parts[0] = fileText.substring(0, third);
parts[1] = fileText.substring(third, secondThird);
parts[2] = fileText.substring(secondThird);
}
for(int i = 1; i <= numberOfSplits; i++)
{
BufferedInputStream bis = new BufferedInputStream(new fileInputStream(file));
FileOutputStream out;
String name = file.getName();
byte[] b = parts[i - 1].getBytes(Charset.forName("UTF-8"));
int temp = 0;
while((temp = bis.read(b)) > 0);
{
File newFile = new File(name + " " + i + ".txt");
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(b, 0, temp); // Writes to the file
out.close();
temp = 0;
}
}
}
public String readFile() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(file));
try
{
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
}
finally
{
br.close();
}
}
}
如果我传入 2 作为我想要的拆分量,它不会在中间拆分它,文件 1 是前半部分,文件 2 是后半部分,而是给出结尾两个文件的文本文件。我的问题似乎出在这里:
while((temp = bis.read(b)) > 0);
{
File newFile = new File(name + " " + i + ".txt");
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(b, 0, temp); // Writes to the file
out.close();
temp = 0;
}
我将在此处使用的示例文件是此文件:
myFile.txt
abcdefghijklmnopqrstuvwxyz
它分为两个文件,如下所示:
myFile.txt 1
nopqrstuvqxyz
myFile.txt 2
opqrstuvqxyz
知道问题出在哪里吗?
- 在您的代码中,您在 whilte 循环中定义了
File newFile = new File(name + " " + i + ".txt");
和 out = new FileOutputStream(newFile);
,这是不正确的。
while((temp = bis.read(b)) > 0);
这里不是分号 =.="
你的代码有很多错误
我会像这样更改您的代码:
File newFile = new File(name + " " + i + ".txt");
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(b); // Writes to the file
out.flush();
out.close();
如果您需要代码 运行,这里是
for (int i = 1; i <= numberOfSplits; i++) {
String name = file.getName();
byte[] b = parts[i - 1].getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream bis = new ByteArrayInputStream(b);
int temp = 0;
File newFile = new File(name + " " + i + ".txt");
newFile.createNewFile();
FileOutputStream out = new FileOutputStream(newFile);
while ((temp = bis.read()) > 0)
{
out.write(temp); // Writes to the file
}
out.flush();
out.close();
}
我正在创建一个程序,旨在接收 n 次拆分并将文件拆分为该数量的子文件。在我的 SplitFile.java 中,我正在读取一个文件,传递一个子字符串数组,这些子字符串显示每个拆分文件中应该包含的文本。然后我将字符串转换为字节数组并将字节数组写入拆分文件,但我创建的每个文件输出的内容 只是 略有不同。
SplitFile.java
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class SplitFile
{
int numberOfSplits = 1;
File file;
String[] parts = new String[5];
public SplitFile(File file,int numberOfSplits)
{
this.file = file;
this.numberOfSplits = numberOfSplits;
}
public void FileSplitter() throws IOException
{
FileInputStream fis = new FileInputStream(file);
String fileText = readFile();
if(numberOfSplits == 2)
{
int mid = fileText.length() / 2;
parts[0] = fileText.substring(0, mid);
parts[1] = fileText.substring(mid);
}
else if(numberOfSplits == 3)
{
int third = fileText.length() / 3;
int secondThird = third + third;
parts[0] = fileText.substring(0, third);
parts[1] = fileText.substring(third, secondThird);
parts[2] = fileText.substring(secondThird);
}
for(int i = 1; i <= numberOfSplits; i++)
{
BufferedInputStream bis = new BufferedInputStream(new fileInputStream(file));
FileOutputStream out;
String name = file.getName();
byte[] b = parts[i - 1].getBytes(Charset.forName("UTF-8"));
int temp = 0;
while((temp = bis.read(b)) > 0);
{
File newFile = new File(name + " " + i + ".txt");
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(b, 0, temp); // Writes to the file
out.close();
temp = 0;
}
}
}
public String readFile() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(file));
try
{
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
}
finally
{
br.close();
}
}
}
如果我传入 2 作为我想要的拆分量,它不会在中间拆分它,文件 1 是前半部分,文件 2 是后半部分,而是给出结尾两个文件的文本文件。我的问题似乎出在这里:
while((temp = bis.read(b)) > 0);
{
File newFile = new File(name + " " + i + ".txt");
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(b, 0, temp); // Writes to the file
out.close();
temp = 0;
}
我将在此处使用的示例文件是此文件:
myFile.txt
abcdefghijklmnopqrstuvwxyz
它分为两个文件,如下所示:
myFile.txt 1
nopqrstuvqxyz
myFile.txt 2
opqrstuvqxyz
知道问题出在哪里吗?
- 在您的代码中,您在 whilte 循环中定义了
File newFile = new File(name + " " + i + ".txt");
和out = new FileOutputStream(newFile);
,这是不正确的。 while((temp = bis.read(b)) > 0);
这里不是分号 =.="你的代码有很多错误 我会像这样更改您的代码:
File newFile = new File(name + " " + i + ".txt"); newFile.createNewFile(); out = new FileOutputStream(newFile); out.write(b); // Writes to the file out.flush(); out.close();
如果您需要代码 运行,这里是
for (int i = 1; i <= numberOfSplits; i++) {
String name = file.getName();
byte[] b = parts[i - 1].getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream bis = new ByteArrayInputStream(b);
int temp = 0;
File newFile = new File(name + " " + i + ".txt");
newFile.createNewFile();
FileOutputStream out = new FileOutputStream(newFile);
while ((temp = bis.read()) > 0)
{
out.write(temp); // Writes to the file
}
out.flush();
out.close();
}