| @ -0,0 +1,104 @@ | |||
| package holaFichero; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.util.Iterator; | |||
| import java.util.Scanner; | |||
| public class LecturaFichero { | |||
| public static void main(String[] args) throws IOException { | |||
| //QUEREMOS SABER QUE OS ESTAMOS UTILIZANDO | |||
| char os = sistemaOperativo(); | |||
| if(os == 'l'){ | |||
| crearFicheroLinux(solicitarDatos()); | |||
| }else { | |||
| crearFicheroWindows(solicitarDatos()); | |||
| } | |||
| } | |||
| //* El nombre del fichero | |||
| public static StringBuilder solicitarDatos() throws IOException{ | |||
| //Stringbuilder para no crear muchos objetos tipos de String a la hora de juntarlos. | |||
| StringBuilder extensionSb = new StringBuilder(".txt"); | |||
| //Leemos lo que el usario nos introduce | |||
| Scanner sc = new Scanner(System.in); | |||
| System.out.println("Introduce el nombre del fichero"); | |||
| String fileName = sc.nextLine(); | |||
| //convertir string to stb | |||
| StringBuilder fileNameSb = new StringBuilder(); | |||
| fileNameSb.append(fileName); | |||
| //2 StringBuilder = extensionSB y fileNameSb; | |||
| StringBuilder file = new StringBuilder(); | |||
| file = fileNameSb.append(extensionSb); | |||
| return file; | |||
| } | |||
| //**Return os | |||
| public static char sistemaOperativo() { | |||
| char sis='?'; | |||
| char windows = 'w'; | |||
| char linux= 'l'; | |||
| String os = System.getProperty("os.name"); | |||
| if(os.charAt(0)==windows) { | |||
| sis = windows; | |||
| return sis; | |||
| }else { | |||
| sis = linux; | |||
| return sis; | |||
| } | |||
| } | |||
| //** True si creamos el fichero | |||
| public static boolean crearFicheroLinux(StringBuilder name) throws IOException{ | |||
| //Pasar el StringBuilder to String | |||
| System.out.println("Creando un fichero en linux"); | |||
| StringBuilder path = new StringBuilder("/home/minumichaelubuntu/Desktop/"); | |||
| //Creamos el fichero | |||
| path.append(name); | |||
| File filePath = new File(path.toString()); | |||
| if(filePath.exists()== true) { | |||
| System.out.println("Fichero ya esta creado"); | |||
| return true; | |||
| } | |||
| System.out.println("Fichero creado"); | |||
| filePath.createNewFile(); | |||
| return true; | |||
| } | |||
| //** True si creamos el fichero | |||
| public static boolean crearFicheroWindows(StringBuilder name) throws IOException { | |||
| System.out.println("Creando un fichero en Windows"); | |||
| //**Hay que cambiar a nuetro path | |||
| StringBuilder path = new StringBuilder("G:\\ADA\\T1.Ficheros\\"); | |||
| path.append(name); | |||
| File filePath = new File(path.toString()); | |||
| if(filePath.exists()== true) { | |||
| System.out.println("Fichero ya esta creado"); | |||
| return true; | |||
| } | |||
| System.out.println("Fichero creado"); | |||
| filePath.createNewFile(); | |||
| return true; | |||
| } | |||
| } | |||