I'm writing a program that transfers files around a network using UDP. One of the things I am trying to do is break down files into small byte arrays that can be put on packets and transferred.

But it doesn't work... It doesn't seem to be reading the bytes correctly, or adding them to the LinkedList properly. Basically what I am trying to get it do is take say an N KB file of text, break it up into 1024 byte chunks (the byte[]) and then put an array onto a LinkedList to later be called when needed.

package miniftpserver2;

import java.io.*;
import java.util.*;

public class FileIOTest
{
    public static void main(String [] args) throws Exception
    {
        // Input/Read
        File inFile = new File("C:\\MiniFTP\\data\\lorem_ipsum.txt");
        File outFile = new File("C:\\MiniFTP\\data\\lorem_ipsum_out.txt");
        FileInputStream fis = new FileInputStream(inFile);
        FileOutputStream fos = new FileOutputStream(outFile);

        byte[] temp = new byte[1024];
        LinkedList<byte[]> byteList = new LinkedList<byte[]>();

        while(fis.read() != -1)
        {
            fis.read(temp, 0, 1024);
            byteList.addLast(temp);
        }

        // test the byte array.
        while(byteList.isEmpty() == false)
        {
            temp = byteList.removeFirst();
            String str2 = new String(temp);
            System.out.println(str2);
        }
    }
}

Any help is greatly appreciated!

line:19 one byte missed each loop !

public int read()
throws IOException
Reads a byte of data from this input stream.

line:16 single and always the same instance of byte array !
Result of run:

run:
100
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA*
BUILD SUCCESSFUL (total time: 0 seconds)

public class FileIOTest {

    public static void main(String[] args) throws Exception {
        // Input/Read
        File inFile = new File("C:\\lorem_ipsum100.txt");
        File outFile = new File("C:\\lorem_ipsum_out.txt");
        FileInputStream fis = new FileInputStream(inFile);
        FileOutputStream fos = new FileOutputStream(outFile);
        System.out.println(fis.available());
        int K = 16;
        LinkedList<byte[]> byteList = new LinkedList<byte[]>();
       
        byte[] temp = new byte[K];
        while (fis.read() != -1) {
        fis.read(temp, 0, K);
        byteList.addLast(temp);
        }
        //magician change byteList : fill byteList with letter A
        for (int j = 0; j < temp.length; j++) {
        temp[j] = (byte) 65;
        }
         
/*
        int i = -1;
        do {
            byte[] temp = new byte[K];
            i = fis.read(temp, 0, K);
            byteList.add(temp);

        } while (i != -1);
*/

        // test the byte array.

        for (byte[] tem : byteList) {
            System.out.print(new String(tem));
        }
        System.out.println("*");
    }
}

tested with lorem_ipsum100.txt (100 Bytes)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.