LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Error when i create a file text with java (https://www.linuxquestions.org/questions/programming-9/error-when-i-create-a-file-text-with-java-4175634063/)

squall852 07-15-2018 12:44 AM

Error when i create a file text with java
 
Hi all,

I'm getting some errors when I want to create a txt file with Java NetBeans. This is the error.

java.io.FileNotFoundException: /home/tony/Documents/Java (Is a directory)

Here is the code.


package manejoarchivos;

import static utileria.Archivos.*;

public class ManejoArchivos {


//Nota: ya debe estar creada la carpeta sobre la cual se va a trabar
//Y en caso necesario se deben asignar permisos de escritura a la carpeta
private static final String NOMBRE_ARCHIVO = "/home/tony/Documents/Java/";


public static void main(String[] args) {

//Crear archivo
crearArchivo(NOMBRE_ARCHIVO);
}

class.::::::::::::::::::::::::::::

package utileria;

import java.io.*;

public class Archivos {

public static void crearArchivo(String nombreArchivo)
{
File archivo = new File(nombreArchivo);
try
{
PrintWriter salida = new PrintWriter(new FileWriter(archivo));
salida.close();
System.out.println("El archivo se ha creado correctamente");
}
catch(IOException io)
{
io.printStackTrace();
}
}

I know to the exception is giving me that error but i don't know how to change the folder as not a directory.

Thank :)

pan64 07-15-2018 02:44 AM

you might want to check the value of NOMBRE_ARCHIVO

keefaz 07-15-2018 07:58 AM

Assign a filename to the file you want to create
Code:

crearArchivo(NOMBRE_ARCHIVO + "filename.txt");


All times are GMT -5. The time now is 10:46 PM.