domingo, 18 de mayo de 2008

Java de File a String

Bueno este artículo es muy interesante, muchas veces necesitamos leer el contenido de un fichero y pasarlo todo a un string de manera que podramos concatenarlo con otros o podamos hacer tratamiento de cadenas en java. Pues bien aquí veras cuatro formas de hacer esto y como de eficientes son...

Técnicajview (sec) java (sec)
Read into byte-buffer & convert to string 0.61.1
Read into byte-buffer & ByteArrayOutputStream
1.0
1.7
BufferedReader line at a time1.7
2.1
Read single byte at a time
62
77

Read into byte-buffer & convert to string

import java.io.*;

public class ReadIntoStr4 {
public static void main(String argv[]){
try {
FileInputStream file = new FileInputStream (argv[0]);
long tcalc ;
long begin = System.currentTimeMillis() ;
byte[] b = new byte[file.available ()];
file.read(b);
file.close ();
String result = new String (b);
/* */
tcalc = (System.currentTimeMillis() - begin);
System.out.println("Compute time: " + tcalc + " ms for " + result.length() + " bytes.");
//System.out.println("The string \n " + result) ;
}
catch (Exception e) {
e.printStackTrace();
}
}

}

Read into byte-buffer & ByteArrayOutputStream

import java.io.*;

public class ReadIntoStr2 {
public static void main(String argv[]){
try {
FileInputStream file = new FileInputStream (argv[0]);
long tcalc ;
long begin = System.currentTimeMillis() ;
ByteArrayOutputStream bout = new ByteArrayOutputStream() ;
int bytesread = 0;
byte [] buff = new byte[512] ;

while(true) {
bytesread = file.read(buff);
if (bytesread == -1) // if EOF
break ;
bout.write(buff,0,bytesread) ;
}

file.close ();
bout.close();
String result = bout.toString() ;
/* */
tcalc = (System.currentTimeMillis() - begin);
System.out.println("Compute time: " + tcalc + " ms for " + result.length() + " bytes.");
//System.out.println("The string \n " + result) ;
}
catch (Exception e) {
e.printStackTrace();
}
}
}

BufferedReader line at a time

import java.io.*;
public class ReadIntoStr3 {
public static void main (String args[]) {
String thisLine;
StringBuffer strb = new StringBuffer("") ;

for (int i=0; i < args.length; i++) {
try {
FileInputStream fin = new FileInputStream(args[i]);
BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
long tcalc ;
long begin = System.currentTimeMillis() ;
while ((thisLine = myInput.readLine()) != null) {
strb.append(thisLine+"\r\n");
}
String result = strb.toString();
tcalc = (System.currentTimeMillis() - begin);
System.out.println("Compute time: " + tcalc + " ms for " + result.length() + " bytes.");
//System.out.println("The string \n " + result) ;

}
catch (Exception e) {
e.printStackTrace();
}
}
}
}

Read single byte at a time

import java.io.*;

public class ReadIntoStr1 {

public static void main(String argv[]){

try {

FileInputStream file = new FileInputStream (argv[0]);

long tcalc ;

long begin = System.currentTimeMillis() ;

ByteArrayOutputStream bout = new ByteArrayOutputStream() ;


int c;

while ((c = file.read()) != -1)

bout.write(c);

file.close ();

bout.close();

String result = bout.toString() ;

/* */

tcalc = (System.currentTimeMillis() - begin);

System.out.println("Compute time: " + tcalc + " ms for " + result.length() + " bytes.");

//System.out.println("The string \n " + result)

}

catch (Exception e) {

e.printStackTrace();

}

}

}

2 comentarios:

Bonsai dijo...

gracias! Muy útil! :)

Saludos desde Barcelona.

Jorge Cantón Ferrero dijo...

Gracias Axel por tu comentario, me alegra saber que le sirve también a los demás.

Un saludo