\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{victorjfg} \pdfinfo{ /Title (ficheros-en-java.pdf) /Creator (Cheatography) /Author (victorjfg) /Subject (Ficheros en java Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{C486C2} \definecolor{LightBackground}{HTML}{F7EFF7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Ficheros en java Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{victorjfg} via \textcolor{DarkBackground}{\uline{cheatography.com/35914/cs/11925/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}victorjfg \\ \uline{cheatography.com/victorjfg} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 28th May, 2017.\\ Updated 28th May, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Escribir texto}} \tn % Row 0 \SetRowColor{LightBackground} `PrintWriter fOut = new PrintWriter(new BufferedWriter(new \seqsplit{FileWriter("nombre\_fichero")));`} & Apertura del PrintWriter como Stream handler utilizando un buffer de salida para acelerar las operaciones. \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} `PrintWriter fOut = new PrintWriter((new \seqsplit{FileWriter("nombre\_fichero"));`} & Apertura sin buffer \tn % Row Count 10 (+ 4) % Row 2 \SetRowColor{LightBackground} `PrintWriter fOut = new \seqsplit{PrintWriter("nombre\_fichero");`} & Similar al anterior \tn % Row Count 13 (+ 3) % Row 3 \SetRowColor{white} `fOut.print(dato);` & Imprimir un dato. \tn % Row Count 14 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{`fOut.println(dato);`} & Imprimir un dato y cambio de línea \tn % Row Count 16 (+ 2) % Row 5 \SetRowColor{white} `fOut.println();` & Imprimir un cambio de línea en el fichero \tn % Row Count 19 (+ 3) % Row 6 \SetRowColor{LightBackground} \seqsplit{`fOut.format("cadena\_de\_formato"}, dato, dato...);` & Imprimir un dato con formato \tn % Row Count 22 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{`fOut.format(Locale.ROOT}, \seqsplit{"cadena\_de\_formato"}, dato, dato...);` & Imprimir un dato con formato en cultura neutra \tn % Row Count 26 (+ 4) % Row 8 \SetRowColor{LightBackground} \seqsplit{`fOut.printf("cadena\_de\_formato"}, dato, dato...);` & printf es equivalente a format \tn % Row Count 29 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Si el fichero existe, se machaca, creándose de nuevo. \newline Puede abrirse para añadir al final mediante el constructor del FileWriter: `PrintWriter fOut = new PrintWriter(new BufferedWriter(new \seqsplit{FileWriter("nombre\_fichero"}, true)));`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Leer texto}} \tn % Row 0 \SetRowColor{LightBackground} `Scanner fIn = new Scanner(new BufferedReader(new \seqsplit{FileReader("nombre\_fichero"))):`} & Apertura del stream, con buffer de entrada. \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} `Scanner fIn = new Scanner(new \seqsplit{FileReader("nombre\_fichero")):`} & Sin buffer \tn % Row Count 9 (+ 4) % Row 2 \SetRowColor{LightBackground} `fIn.nextLine();` & Lee una línea de la entrada. \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} `fIn.hasNextLine()` & Devuelve `true`si hay una línea preparada en la entrada. \tn % Row Count 14 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Una vez leída una línea de la entrada se puede tokenizar o realizar cualquier otra acción.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operaciones con el sistema de ficheros}} \tn % Row 0 \SetRowColor{LightBackground} `File f= new File("nombre");` & Un objeto de tipo File representa a un fichero o una carpeta. Se puede especificar un nombre con trayectoria absoluta o relativa. \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} `f.delete()` \{\{fa-hand-o-right\}\} boolean & Borrar el fichero. True si éxito. \tn % Row Count 9 (+ 2) % Row 2 \SetRowColor{LightBackground} `f.exists()` \{\{fa-hand-o-right\}\} boolean & True si el fichero existe. \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`f.getAbsolutePath()`} \{\{fa-hand-o-right\}\} String & La trayectoria absoluta \tn % Row Count 14 (+ 3) % Row 4 \SetRowColor{LightBackground} `f.getParent()` \{\{fa-hand-o-right\}\} String & El directorio de nivel superior \tn % Row Count 17 (+ 3) % Row 5 \SetRowColor{white} `f.isDirectory()` \{\{fa-hand-o-right\}\} boolean & True si el nombre representa a un directorio. False si es un fichero u otra cosa. \tn % Row Count 22 (+ 5) % Row 6 \SetRowColor{LightBackground} `f.mkdir()` \{\{fa-hand-o-right\}\} boolean & Crea un directorio con el nombre especificado. True si éxito \tn % Row Count 26 (+ 4) % Row 7 \SetRowColor{white} `f.renameTo(new \seqsplit{File("nuevo\_nombre"))`} \{\{fa-hand-o-right\}\} boolean & Cambiar nombre. True si éxito \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operaciones con el sistema de ficheros (cont)}} \tn % Row 8 \SetRowColor{LightBackground} `f.list()` \{\{fa-hand-o-right\}\} String{[}{]} & Si f representa a un directorio, lista los nombres de los elementos que contiene. \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Escribir binario secuencial}} \tn % Row 0 \SetRowColor{LightBackground} `DataOutputStream fOut = new \seqsplit{DataOutputStream(new} \seqsplit{FileWriter(nombre));`} & Abrir stream handler \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \seqsplit{`fOut.writeInt(dato)`} & Escribe un entero \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{`fOut.writeDouble(dato)`} & Escribe un double \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`fOut.writeUTF(dato)`} & Escribe una cadena en la codificación UTF de Unicode. \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Dispone de métodos para grabar todos los tipos primitivos, cada uno con una longitud fija y cadenas de longitud variable. Para las cadenas graba primero su longitud en dos bytes, y a continuación el contenido de la cadena. \newline Dispone de un método para escribir un número arbitrario de bytes.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.36 cm} x{2.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Leer binario secuencial}} \tn % Row 0 \SetRowColor{LightBackground} `DataInputStream fIn = new DataInputStream(new \seqsplit{FileInputStream("nombre"));`} & Abrir stream handler \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `int n = fIn.readInt();` & Lee un entero \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} `double d = fIn.readDouble();` & Lee un double \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `String s = fIn.readUTF();` & Leer una cadena \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Dispone de métodos para leer todos los tipos primitivos. \newline Dispone de un método para leer un número arbitrario de bytes.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Escribir / leer binario aleatorio}} \tn % Row 0 \SetRowColor{LightBackground} `RandomAccessFile f = new \seqsplit{RandomAccessFile("nombre"}, "rw");` & Abre un fichero de acceso aleatorio que permite leer y escribir simultáneamente. "rw"= read/write \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} `f.writeInt(n)` & Escribe un entero \tn % Row Count 6 (+ 1) % Row 2 \SetRowColor{LightBackground} `f.writeDouble(d)` & Escribe un double \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} `f.write(buff)` & Escribe un array de bytes. \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} `f.seek(pos)` & Establece el desplazamiento del puntero del fichero al byte especificado, para que la siguiente operación de lectura o escritura se produzca a partir de ese byte. \tn % Row Count 18 (+ 9) % Row 5 \SetRowColor{white} `int n = f.readInt()` & leer un entero \tn % Row Count 20 (+ 2) % Row 6 \SetRowColor{LightBackground} `double d = f.readDouble()` & Leer un double \tn % Row Count 22 (+ 2) % Row 7 \SetRowColor{white} `f.read(buff)` & Lee una cierta cantidad de bytes suficiente para llenar el array buff \tn % Row Count 26 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{RandomAccessFile permite grabar y leer del fichero. También permite saltar con seek(..) y establecer la posición por la que el S.O. lee o escribe. \newline Dispone de los mismos métodos que el DataOutputStream y el DataInputStream.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Leer serialización nativa}} \tn % Row 0 \SetRowColor{LightBackground} `ObjectInputStream ois = new \seqsplit{ObjectInputStream(new} \seqsplit{FileInputStream("nombre"));`} & Abrir stream handler \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} `ois.readObject()` \{\{fa-hand-o-right\}\} Object & Lee un objeto y lo devuelve con una referencia a Object. Para engancharlo con una referencia más específica se puede hacer un typecast \tn % Row Count 11 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{ObjectInputStream dispone de los mismos métodos que DataInputStream.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Escribir Serialización nativa}} \tn % Row 0 \SetRowColor{LightBackground} `ObjectOutputStream oos = new \seqsplit{ObjectOutputStream(new} \seqsplit{FileOutputStream("nombre"));`} & Apertura del Stream handler \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \seqsplit{`oos.writeObject(o)`} & Escribe el objeto apuntado por la referencia. Si el objeto contiene referencias a otros objetos, se serializan todos si es posible \tn % Row Count 12 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Los objetos serializables deben implementar la interface java.io.Serializable, que no conlleva obligaciones. \newline Puede guardarse más de un objeto en un stream, que deben recuperarse luego en el mismo orden. \newline ObjectOutputStream también tiene los mismos métodos que DataOutputStream.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}