• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

433
Vistas
How to create a .txt file within the java project folder and write to it

I am attempting to create a text file within my project folder (not within the src but the System library). I have already created a print to display the proper info in the correct format. I copied and pasted this and edited the "System.out.println" to "PrintWriter.println". I am not sure if this code is correct although it shows no errors.

try {
        File file = new File("SortedLists.txt");
        FileWriter w = new FileWriter("SortedLists.txt");
        writer = new PrintWriter(w);

        for(Team p: roster){
            writer.println("Team Name: "+p.gettName()+", "+p.gettAbrv());
            for(Riders r: p.getRide()) {
                writer.println(r);
            }
            writer.printf("Total Team Donations: %.2f$\n",p.getSumD());
        }
    }
    catch(IOException e) {
        System.out.println(e.getMessage());
    }
    finally {
        writer.close();
    }
about 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

If you want to know where is your created file, you simply write for example

System.out.println(file.getAbsolutePath());

Furthermore, if you want to create a file in a relative path (say in a folder for example) you can do something like this:

File folder = new File("folderName"); 
folder.mkdir(); // create a folder in your current work space
File file = new File(folder, "fileName.txt"); // put the file inside the folder
file.createNewFile(); // create the file
about 3 years ago · Santiago Trujillo Denunciar

0

To set the path for the file create the file as follows:

String fileLocation = absolutePathToFile;

File file = new File(Location);

about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda