📄 leefichero.java
字号:
/*
GAEM - Graphical Adventure Engine for Mobiles (version 0.1)
Copyright (C) 2005 Victor Borrull
This file is part of GAEM.
GAEM is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
GAEM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program;
if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.io.*;
/** Esta clase se encarga de leer en fichero de texto .dat y devolverlo en forma de String.
*/
public class LeeFichero {
/** Lee el fichero segun la ruta pasada de par醡etro y lo devuelve como String.
* @param fichero Ruta al fichero.
* @return Contenido del fichero.
*/
public String leeFichero(String fichero) {
StringBuffer str = new StringBuffer();
try {
Class clase = this.getClass();
InputStream is;
is = clase.getResourceAsStream(fichero);
byte [] arrayBytes = new byte[1];
while ( is.read(arrayBytes) != -1 ) {
str.append(new String(arrayBytes)); //StringBuffer.append() es mas eficiente que String += String. Pero igualmente
} //sigo creando un String! :( InputStreamer no lee array de Chars, que append() si aceptaria sin convertir a String...
is.close();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException ficheroNoExiste) {
return "";
}
return str.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -