java - Byte Array not printing to file correctly -


i'm creating program aims take in n number of splits , split file amount of sub-files. in splitfile.java, i'm reading file, passing array of substrings show text that's supposed go in each split file. convert string byte array , write byte array split file, each file i'm creating outputting just different.

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 = 1; <= 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 + " " + + ".txt");                 newfile.createnewfile();                 out = new fileoutputstream(newfile);                 out.write(b, 0, temp); // writes 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();         }                  {             br.close();         }     } } 

if pass in 2 amount of splits want, not splitting right @ middle, file 1 being first half , file 2 being second half, , instead giving end of text file both files. problem seems here:

while((temp = bis.read(b)) > 0); {     file newfile = new file(name + " " + + ".txt");     newfile.createnewfile();     out = new fileoutputstream(newfile);     out.write(b, 0, temp); // writes file     out.close();     temp = 0; } 

an example file i'll use on here file:

myfile.txt

abcdefghijklmnopqrstuvwxyz

it splits 2 files go follows:

myfile.txt 1

nopqrstuvqxyz

myfile.txt 2

opqrstuvqxyz

any idea on problem is?

  1. in code, define file newfile = new file(name + " " + + ".txt"); , out = new fileoutputstream(newfile); in whilte loop, it's not correct.
  2. while((temp = bis.read(b)) > 0); not semicolon here =.="
  3. many mistake in code change code like:

            file newfile = new file(name + " " + + ".txt");         newfile.createnewfile();         out = new fileoutputstream(newfile);         out.write(b); // writes file         out.flush();         out.close(); 

if need code run want, here are

        (int = 1; <= 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 + " " + + ".txt");             newfile.createnewfile();             fileoutputstream out = new fileoutputstream(newfile);                                 while ((temp = bis.read()) > 0)             {                 out.write(temp); // writes file             }             out.flush();             out.close();         } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -