Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Java
/*
* Lázarus Surazal
* 07 de Agosto de 2014
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class PlantillaLecturaEscritura {
private static BufferedReader reader;
private static BufferedWriter writer;
private static final String separador = File.separator;
private static final String userRoute = System.getProperty("user.dir").concat(separador);
private static final String inputName = "entrada.txt";
private static final String outputName = "output.txt";
private static boolean console = false;
// private static String inFileString = null;
private static String inFileString = userRoute.concat(inputName);
// private static String outFileString = null;
private static String outFileString = userRoute.concat(outputName);
public static void main(String[] args){
try {
System.out.println("PROGRAMA INICIALIZADO CORRECTAMENTE");
System.out.println("================================");
// Crear el buffer de entrada. Si es NULO, entonces se usa la consola
if (inFileString != null) {
File inFile = new File(inFileString);
reader = new BufferedReader(new FileReader(inFile));
System.out.println("ENTRADA: ".concat(inFileString));
} else {
reader = new BufferedReader(new InputStreamReader(System.in));
console = true;
System.out.println("ENTRADA: CONSOLA");
}
// Crear el buffer de salida. Si es NULO, entonces se usa la consola
if (outFileString != null) {
File outFile = new File(outFileString);
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
System.out.println("SALIDA: ".concat(outFileString));
} else {
writer = new BufferedWriter(new OutputStreamWriter(System.out, "utf-8"));
System.out.println("SALIDA: CONSOLA");
}
System.out.println("================================");
int i = 0;
if (!console) {
System.out.println("LEYENDO FICHERO DE ENTRADA...");
String line = reader.readLine();
while (line != null) {
if (!"".equals(line)) {
StringBuilder cleanLine = new StringBuilder("");
//CÓDIGO DE USUARIO AQUÍ
writer.write(cleanLine.toString());
writer.newLine();
} else {
writer.newLine();
}
i++;
line = reader.readLine();
}
} else {
System.out.println("LEYENDO ENTRADA DESDE CONSOLA. INTRODUZCA CADENA VACÍA PARA TERMINAR.");
System.out.println("================================");
String line = reader.readLine();
while (!"".equals(line)) {
StringBuilder cleanLine = new StringBuilder("");
//CÓDIGO DE USUARIO AQUÍ
writer.write(cleanLine.toString());
writer.newLine();
i++;
line = reader.readLine();
}
}
System.out.println("================================");
System.out.println(i + " LÍNEAS LEÍDAS");
reader.close();
System.out.println("================================");
if (outFileString != null) {
System.out.println("ESCRIBIENDO EN FICHERO DE SALIDA...");
}
writer.flush();
System.out.println("================================");
System.out.println("PROGRAMA CERRADO CORRECTAMENTE");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment