Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/08/2013, 02:47
lopezcanov
 
Fecha de Ingreso: agosto-2013
Mensajes: 2
Antigüedad: 10 años, 8 meses
Puntos: 0
Pregunta Entrada de texto y exportación a fichero

Buenos días,

Os agradecería enormemente un cable.
El siguiente código crea un fichero txt a partir de datos introducidos:
- pide al usuario que determine el nombre del fichero
- comprueba si existe
- si no existe, lo crea y si existe, pregunta si desea sobreescribir.
- lo cierra

Sin embargo, el programa se termina en el momento que se escribe el nombre del fichero y no sé por qué.

Por otro lado, tengo dudas:
- qué estamos diciendo con lo que se encuentra entre paréntesis?



nombrefichero=new String(buffer,0,nbytes-2);


ESTE ES EL CÓDIGO DEL PROGRAMA
GRACIAS POR ADELANTADO

package escribirdatos;
import java.io.*;

public class EscribirDatos {


public static void main(String[] args) {
FileWriter fs=null;
byte[] buffer=new byte[81];
int nbytes;
String nombrefichero=null;
File fichero=null;

try
{
System.out.print("Escriba el nombre del fichero: ");
nbytes=System.in.read(buffer);
nombrefichero=new String(buffer,0,nbytes-2);
fichero=new File(nombrefichero);

char resp= 's';
if (fichero.exists())
{
System.out.print("El fichero existe, desea sobreescribirlo? s/n ");
resp=(char)System.in.read();
System.in.skip(System.in.available());
}
if (resp=='s')
{
System.out.print("Escribe el texto que desea almacnar en el fichero");
nbytes=System.in.read(buffer);
String str=new String(buffer,0, nbytes);
fs=new FileWriter(fichero);
fs.write(str,0,str.length());
}
}
catch (IOException e)
{
System.out.println("Error: "+e.toString());
}
finally
{
try
{
if (fs!=null) fs.close();
}
catch (IOException e)
{
System.out.println("Error :"+e.toString());

}
}
}