【问题标题】:Byte Array not printing to file correctly字节数组未正确打印到文件
【发布时间】:2015-04-23 02:47:50
【问题描述】:

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

知道问题出在哪里吗?

【问题讨论】:

    标签: java file split bytearray


    【解决方案1】:
    1. 在您的代码中,您在 whilte 循环中定义了 File newFile = new File(name + " " + i + ".txt");out = new FileOutputStream(newFile);,这是不正确的。
    2. while((temp = bis.read(b)) &gt; 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();
            }
    

    【讨论】:

    • 感谢您的编辑。但是哇......在我尝试过的事情中,我什至没有认为这两行是我问题的最大罪魁祸首。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多