Created
December 14, 2023 03:10
-
-
Save zendar426/ea6954927fd842c0f54ded82ab4eb650 to your computer and use it in GitHub Desktop.
Archivos Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Scanner; | |
public class Files { | |
public static void crearArchivo() { | |
try { | |
File myObj = new File("filename.txt"); | |
if (myObj.createNewFile()) { | |
System.out.println("File created: " + myObj.getName()); | |
} else { | |
System.out.println("File already exists."); | |
} | |
} catch (IOException e) { | |
System.out.println("An error occurred."); | |
} | |
} | |
public static void escribirEnArchivo(String texto) { | |
try { | |
FileWriter myWriter = new FileWriter("filename.txt"); | |
myWriter.write(texto); | |
myWriter.close(); | |
System.out.println("Successfully wrote to the file."); | |
} catch (IOException e) { | |
System.out.println("An error occurred."); | |
} | |
} | |
public static void leerArchivo() { | |
try { | |
File myObj = new File("filename.txt"); | |
Scanner myReader = new Scanner(myObj); | |
while (myReader.hasNextLine()) { | |
String data = myReader.nextLine(); | |
System.out.println(data); | |
} | |
myReader.close(); | |
} catch (FileNotFoundException e) { | |
System.out.println("An error occurred."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment