java - 字节数组未正确打印到文件

标签 java file split arrays

我正在创建一个程序,旨在接受 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

知道问题出在哪里吗?

最佳答案

  1. 在代码中,定义 File newFile = new File(name + ""+ i + ".txt");out = new FileOutputStream(newFile); 在 while 循环中,这是不正确的。
  2. while((temp = bis.read(b)) > 0); 这里不是分号 =.="
  3. 您的代码中有很多错误 我将更改您的代码,例如:

            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();
        }

关于java - 字节数组未正确打印到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812713/

相关文章:

java - 在Java Web应用程序中,尽管AWS SDK内部使用1.3,如何添加common-codec 1.6

如果没有 seekg 和 seekp,c++ fstream write 不适用于二进制文件

python - 使用 dict 键作为列名写入 CSV 文件

java 拆分不能与 ^ 一起正常工作

javascript - 将字符串拆分为单词数组以查找最长的

java - 未找到 HanderMapping - Spring 3.x - Controller

java regex - 读取包含小数点的数字

使用 COMODO 证书签名的 JavaFx dmg 包

file - 强制 : passing file extensions in a "diff" command

csv - 根据列值拆分大型csv文本文件