|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Sep 2005
Città: Torino
Messaggi: 606
|
[JAVA] scrittura su file
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 Codice:
copy 1.txt+2.txt+...etc temp.txt Codice:
fc temp.txt originale.txt Codice:
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);
}
}
se invece di usare RandomAccessFile uso FileInputStream e leggo e scrivo 1 carattere alla volta va tutto bene...perchè?
__________________
"Se proprio dovete piratare un prodotto, preferiamo che sia il nostro piuttosto che quello di qualcun altro." [Jeff Raikes] "Pirating software? Choose Microsoft!" |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Jul 2005
Messaggi: 291
|
Ho provato questa mia versione e sembra funzionare, vedi un po se è quello che ti serve
Codice:
import 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();
}
}
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Sep 2005
Città: Torino
Messaggi: 606
|
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!!
__________________
"Se proprio dovete piratare un prodotto, preferiamo che sia il nostro piuttosto che quello di qualcun altro." [Jeff Raikes] "Pirating software? Choose Microsoft!" |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:38.



















