| @ -0,0 +1,10 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <classpath> | |||
| <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"> | |||
| <attributes> | |||
| <attribute name="module" value="true"/> | |||
| </attributes> | |||
| </classpathentry> | |||
| <classpathentry kind="src" path="src"/> | |||
| <classpathentry kind="output" path="bin"/> | |||
| </classpath> | |||
| @ -0,0 +1,17 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <projectDescription> | |||
| <name>EditorTxtEnDirecto</name> | |||
| <comment></comment> | |||
| <projects> | |||
| </projects> | |||
| <buildSpec> | |||
| <buildCommand> | |||
| <name>org.eclipse.jdt.core.javabuilder</name> | |||
| <arguments> | |||
| </arguments> | |||
| </buildCommand> | |||
| </buildSpec> | |||
| <natures> | |||
| <nature>org.eclipse.jdt.core.javanature</nature> | |||
| </natures> | |||
| </projectDescription> | |||
| @ -0,0 +1,2 @@ | |||
| eclipse.preferences.version=1 | |||
| encoding/<project>=UTF-8 | |||
| @ -0,0 +1,14 @@ | |||
| eclipse.preferences.version=1 | |||
| org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | |||
| org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 | |||
| org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | |||
| org.eclipse.jdt.core.compiler.compliance=17 | |||
| org.eclipse.jdt.core.compiler.debug.lineNumber=generate | |||
| org.eclipse.jdt.core.compiler.debug.localVariable=generate | |||
| org.eclipse.jdt.core.compiler.debug.sourceFile=generate | |||
| org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | |||
| org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled | |||
| org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | |||
| org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning | |||
| org.eclipse.jdt.core.compiler.release=enabled | |||
| org.eclipse.jdt.core.compiler.source=17 | |||
| @ -0,0 +1,42 @@ | |||
| # README.md | |||
| ## Titulo | |||
| Editor Txt en Directo | |||
| ## Autor | |||
| Alejandro Javier Albus 2CFGS DAM | |||
| ## Enunciado | |||
| ACTIVIDAD 05 : Crea un programa que vaya escribiendo en un fichero que se le pasará por la línea de comandos. | |||
| Irá escribiendo los caracteres hasta que lea el carácter \. Antes de dicho char inclusive. | |||
| OJO: Si el fichero ya existe, al escribir borrará todo su contenido anterior. | |||
| Para que esto no ocurra al instanciar un objeto de la clase FileWriter pondremos como segundo parámetro true. | |||
| ## Uso | |||
| Debe introducir cadenas de caracteres y cuando crea que ya ha terminado introduzca la contrabarra ('\'). | |||
| El programa creará un archivo ya rellenado en la ruta pasada. | |||
| ## Ejemplo de ejecución | |||
| <pre> | |||
| Task: EditorTxtEnDirecto.main() | |||
| Escriba lo que desee. | |||
| 1a frase: En un lugar de La Mancha ... | |||
| 2a frase: De cuyo nombre no quiero ... | |||
| Final e introducimo en sig. linea la contrabarra ... | |||
| \ | |||
| - Caracter stopper. | |||
| + Se ha terminado la ejecución. | |||
| </pre> | |||
| ## Dependencias | |||
| Uso de librerias externas: | |||
| * java.io.BufferedWriter | |||
| * java.io.File | |||
| * java.io.FileNotFoundException | |||
| * java.io.FileWriter | |||
| * java.io.IOException | |||
| * java.util.Scanner | |||
| ## Licencia | |||
| Uso libre | |||
| @ -0,0 +1,104 @@ | |||
| package EditorTxtEnDirecto; | |||
| import java.io.BufferedWriter; | |||
| import java.io.File; | |||
| import java.io.FileNotFoundException; | |||
| import java.io.FileWriter; | |||
| import java.io.IOException; | |||
| import java.util.Scanner; | |||
| public class EditorTxtEnDirecto { | |||
| private static final Scanner lector = new Scanner(System.in); | |||
| public static void main(String[] args) { | |||
| final char STOPPER = '\\'; | |||
| String pathContainer = "C:\\Users\\jandr\\Desktop\\Acceso a datos\\Container Ficheros"; | |||
| String fileName = "Ejercicio5_0310.txt"; | |||
| //Creamos el archivo | |||
| File f = makeFile(pathContainer, fileName); | |||
| //Rellenamos el archivo | |||
| String finalInput = ""; | |||
| System.out.println("\n>Escriba lo que desee."); | |||
| boolean validate = false; | |||
| while (!validate) { | |||
| String userInput = readString(); | |||
| //Iteramos lo introducido CHAR por CHAR por si aparece la '\' | |||
| for(int i = 0; i < userInput.length(); i++) { | |||
| if(userInput.charAt(i) == STOPPER) { | |||
| System.out.println("\n- Caracter stopper."); | |||
| userInput = ""; | |||
| validate = true; | |||
| } | |||
| } | |||
| finalInput += userInput; | |||
| }; | |||
| //Rellenamos el archivo | |||
| fillFile(f, finalInput); | |||
| System.out.println("\n+ Se ha terminado la ejecución."); | |||
| } | |||
| /** | |||
| * Lee y devuelve un String | |||
| * @return | |||
| */ | |||
| public static String readString() { | |||
| return lector.nextLine(); | |||
| } | |||
| /** | |||
| * Creará un archivo con el nombre archivo | |||
| * @param directory | |||
| * @param file | |||
| * @return | |||
| */ | |||
| public static File makeFile(String directory, String file) { | |||
| File f = new File(directory + File.separator + file); | |||
| if (f != null) { | |||
| try { | |||
| if (f.createNewFile()) | |||
| return f; | |||
| } catch (IOException e) { | |||
| System.out.println("- Ha surgido un problema... \n(i) It exists: " + f.exists() + "\tLength: " + f.length()); | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| /** | |||
| * Rellena el File/Archivo con el String pasado | |||
| * Los saltos de linea -\n- si que los cuenta. | |||
| * @param f | |||
| * @param content | |||
| */ | |||
| public static void fillFile(File f, String content) { | |||
| if (f.exists() && f.isFile()) { | |||
| try (BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath()))) { | |||
| bw.write(content); | |||
| } catch (FileNotFoundException fnf) { | |||
| System.out.println("\n- Ha saltado una excepción FNF ... " + fnf.getMessage()); | |||
| } catch (IOException io) { | |||
| System.out.println("\n- Ha saltado una excepción I/O ... " + io.getMessage()); | |||
| } | |||
| } else { | |||
| System.out.println("\n- El archivo no existe ... "); | |||
| } | |||
| } | |||
| } | |||