Escrito en lenguaje JAVA, utiliza un objeto de clase aleatorio para generar un conjunto de números aleatorios de bytes y almacenarlos en el archivo de datos data1.dat, luego leerlos y mostrarlos.
importar java.io.FileInputStream;
importar java.io.FileOutputStream;
importar java.io.IOException.
importar java.util.Random;public class RandomFile { /**
* @param args
* @throws IOException
* /
public static void main(String[] args) throws IOException {
// TODO Código auxiliar de método generado automáticamente
RandomFile rf = new RandomFile() ;
System.out.println(rf) = nuevo RandomFile();
System.out.println(rf) = nuevo RandomFile(); System.out.println(rf) = new RandomFile()out .println(rf.readFile(rf.writeFile(rf.genRandomByte()))); byte público[] genRandomByte() {
Aleatorio aleatorio = nuevo Aleatorio();
int i = aleatorio.nextInt(1024);
byte[] b = nuevo byte[i];
random.nextBytes(b);
return b;
} Archivo público writeFile(byte[] b) lanza IOException {
Archivo archivo = nuevo Archivo( "D:/data1.bat");
/*if (file.exists() || file.isDirectory())
Lanza una nueva FileNotFoundException();*/
FileOutputStream fos = new FileOutputStream(archivo);
fos.write(b);
fos.close();
devolver archivo;
} cadena pública readFile(archivo de archivo) lanza IOException {
FileInputStream fis = nuevo FileInputStream(archivo);
byte[ ] b = nuevo byte[1024];
StringBuffer sb = new StringBuffer();
while (fis.read(b) ! = -1) {
sb.append(new String(b));
b = nuevo byte[1024];
}
devolver sb.toStr
ing().trim();
}
}
}