问题发现:从后台获取文字信息的时候,经过抓包发现含有\r\n换行符,显示在TextView上可以正确的换行,可是经过以下方法将String写进File,再次使用的时候从File中读取的时候会发现再也不有换行效果。java
写文件方法:数组
public static void saveJson(String fileName, String info) {
try {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(info);
bw.flush();
bw.close();
osw.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
复制代码
读文件方法:缓存
public static String getJson(String fileName) {
String info = null;
try {
FileInputStream fis = context.openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String Line;
StringBuffer info2 = new StringBuffer();
while ((Line = br.readLine()) != null) {
info2.append(Line);
}
info = info2.toString();
br.close();
isr.close();
fis.close();
} catch (Exception e) {
LogSwitchUtils.logD(FileUtils2.class, "缓存的异常为:" + e);
return null;
}
return info;
}
复制代码
经过分析,是由于FileOutputStream、OutputStreamWriter、BufferedWriter等一些流在处理这些转义字符的时候没有将其写入,或者是FileInputStream、InputStreamReader、BufferedReader等没有将其读出。PS:应恶补这些知识。app
问题解决:dom
方法1:依然使用上述两个Method,不过在调用以前先对目标String进行编码/解码,如URLEncoder.encode(schoolDesc)
和URLDecoder.decode(schoolDescTemp)
;编码
方法2:将带有特殊字符的String写进SharedPreferences,使用的时候读出;spa
方法3:使用字节流进行处理,可是该方法有一个不足之处,见读文件方法的注释。方法以下;指针
写文件方法:code
public static void saveStringToFileByByte(Context context, String fileName, String info) {
try {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
byte[] buf = info.getBytes();
fos.write(buf);
fos.close();
} catch (Exception e) {
LogSwitchUtils.logE(FileUtils2.class, "fos写缓存时候的异常为:" + e);
e.printStackTrace();
}
}
复制代码
读文件方法:对象
public static String getStringFromFileByByte(Context context, String fileName) {
String result = null;
StringBuffer sb = new StringBuffer();
File file = new File(context.getFilesDir().getAbsolutePath() +"/"+ fileName);
if (!file.exists()) {
return result;
}
try {
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024 * 2];//数组的容量不容易肯定,若是给的太大,浪费内容;给的过小,在new String的时候将会发生乱码,所以该方法适用于长度固定的String的File写入及读出
while (fis.read(buf) > 0) {
sb.append(new String(buf));
}
result = sb.toString();
fis.close();
} catch (Exception e) {
LogSwitchUtils.logE(FileUtils2.class, "fis写缓存时候的异常为:" + e);
e.printStackTrace();
}
return result;
}
复制代码
方法4:使用RandomAccessFile进行读写,方法以下:
写文件方法:
public static void saveFileByRaf(Context context, String fileName, String info) {
File path = new File(context.getFilesDir().getAbsolutePath() + "/baike_detail");
if (!path.exists()) {
path.mkdir();
}
File file = new File(path, fileName);
if (file.exists()) {
file.delete();
}
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.writeUTF(info);
} catch (Exception e) {
LogSwitchUtils.logE(FileUtils2.class, "写缓存时候的异常为:" + e);
e.printStackTrace();
}
}
复制代码
读文件方法:
public static String getFileByRaf(Context context, String fileName) {
String result = null;
File path = new File(context.getFilesDir().getAbsolutePath() + "/baike_detail");
if (!path.exists()) {
return result;
};
File file = new File(path, fileName);
if (!file.exists()) {
return result;
}
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
result = raf.readUTF();
} catch (Exception e) {
LogSwitchUtils.logE(FileUtils2.class, "读缓存时候的异常为:" + e);
e.printStackTrace();
}
return result;
}
复制代码
##File
File类是java.io包下表明与平台无关的文件和目录,即无论是文件仍是目录都是使用File来操做的,File能新建、删除、重命名文件和目录,但不能访问文件内容自己。
访问文件名相关的方法:
文件检测相关的方法:
获取常规文件信息:
文件操做相关的方法:
目录操做相关方法:
##IO流的分类
访问文件的节点流:
这些节点流读和写方法中的容器参数,分别表明读到容器内,将容器内的内容写出去。
经常使用的处理流:
##RandomAccessFile
简介:能够直接跳转到文件的任意地方来读写文件,但一个最大的局限是只能读写文件,不能读写其余节点。指针的移动单位是字节。
方法: