PDA

View Full Version : [JAVA] scrittura su file


Oceans11
20-06-2007, 17:10
il seguente programma dovrebbe splittare un file. Il costruttore vuole il file e la dimensione dei "pezzi"...beh "quasi" funziona, nel senso che non sempre la riunione dei file che escono fuori è uguale all'originale!

nota1:
ho fatto copy 1.txt+2.txt+...etc temp.txt
e poi fc temp.txt originale.txt

dov'è il problema??? :mc:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Split
{
private File file;
private long splitSize;
private int splitNumber;
private int bufferSize;

public Split(File f, long size) {
file = f;
splitSize = size;
splitNumber = computeSplitNumber();
bufferSize = splitSize < 4096 ? (int) splitSize : 4096;
}

private void split() {
int digitNum = splitNumber / 10;
for (int i = 0; i < splitNumber; i++) {
String outName = f.getName() + (i + 1);
File outFile = new File(outName);
try {
copyFile(outFile, i * splitSize, splitSize);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}

private void copyFile(File destination, long offset, long dim)
throws IOException {
RandomAccessFile in = new RandomAccessFile(file, "r");
in.seek(offset);
FileOutputStream out = new FileOutputStream(destination);
byte[] buffer = new byte[bufferSize];
long lastPos = dim + offset;
long bytesWritten = 0;
int currentBufferSize;
while ((currentBufferSize = in.read(buffer)) > 0
&& in.getFilePointer() <= lastPos) {
out.write(buffer, 0, currentBufferSize);
bytesWritten += currentBufferSize;
}
if (in != null)
in.close();
if (out != null)
out.close();
}

private int computeSplitNumber() {
return (int) ((f.length() / splitSize) + 1);
}
}

nota2:
se invece di usare RandomAccessFile uso FileInputStream e leggo e scrivo 1 carattere alla volta va tutto bene...perchè? :muro:

morskott
20-06-2007, 18:38
Ho provato questa mia versione e sembra funzionare, vedi un po se è quello che ti serveimport java.io.*;

public class SplitFile{
private File theFile;
private long size;

public SplitFile(String path,long size){
this.theFile=new File(path);
this.size=size;
}

public void split() throws IOException{
String nome=getNome(this.theFile);
String ext=getEstensione(this.theFile);
FileInputStream fis=new FileInputStream(this.theFile);
int numeroDiPezzi=getNumeroPezzi(this.theFile.length(),this.size);
for (int i=0;i<numeroDiPezzi;i++){
FileOutputStream fos=new FileOutputStream(nome+"_"+String.valueOf(i)+"."+ext);
for (long k=0;k<this.size;k++){
int in=fis.read();
if (in==-1) break;
fos.write(in);
}
fos.close();
}
fis.close();
}

private static String getNome(File theFile){
return theFile.getName().substring(0,theFile.getName().lastIndexOf("."));
}

private static String getEstensione(File theFile){
return theFile.getName().substring(theFile.getName().lastIndexOf(".")+1,theFile.getName().length());
}

private static int getNumeroPezzi(long length,long size){
return (((int)length)/((int)size))+1;
}

public static void main(String[] args) throws IOException{
File theFile=new File("prova.txt");
theFile.createNewFile();
FileOutputStream fos=new FileOutputStream(theFile);
for (int i=0;i<10000;i++)
fos.write(i%100);
fos.close();
SplitFile sf=new SplitFile(theFile.getPath(),512);
sf.split();
}
}

Oceans11
22-06-2007, 11:15
ho provato anche la tua versione e mi dava lo stesso problema...poi ho capito che il carattere strano a fine file lo inserisce il copy di win...grazie cmq!!