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!