这是我的源代码。我得到了阅读部分,但我的写作部分需要一个简单的逻辑,而我没有得到。在我当前的逻辑中,数据被覆盖,我总是能够在我的书面文件中看到最后一个读取数据块。
import java.io.File;
import java.io.FileInputStream;
java.io.FileNotFoundException;
java.io.FileOutputStream;
import java.io.IOException;
public class LoadTest
{
public void readFiles(File file) throws FileNotFoundException
{
int fsize = (int) file.length();
int part = (fsize/4)+(fsize%4);
byte[] block = new byte[part];
FileInputStream fin = new FileInputStream(file);
try
{
int val=-1;
do
{
int bytesread =0;
while(bytesread<part)
{
val = fin.read(block, bytesread, part-bytesread);
if (val<0)
break;
bytesread += val;
}
writeFiles(block,bytesread);
}
while(val>=0);
fin.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void writeFiles(byte[] block, int len) throws IOException
{
int byteswritten = 0;
FileOutputStream fout = new FileOutputStream("input.csv");
fout.write(block, byteswritten, len+byteswritten);
byteswritten +=len;
fout.close();
}
public static void main(String[] args) throws FileNotFoundException
{
LoadTest testobj = new LoadTest();
String folder = "/Users/NiranjanSubramanian/Desktop/TestFiles";
File dir = new File(folder);
File[] files = dir.listFiles();
System.out.println("Started");
for( File entry: files)
{
testobj.readFiles(entry);
}
System.out.println("Ended");
}
}