/** 读文件
* @param str 路径
* @param res 要写入文件的字符串
*/
public static void readFileByBuffer(String filename){
// File readFile = new File(filename);
BufferedReader br = null;
try{
// br = new BufferedReader(new FileReader(readFile));
br = new BufferedReader(new FileReader(filename));
StringBuffer stb = new StringBuffer();
String rline = null;
while((rline=br.readLine())!=null){
//byte[] bb = rline.getBytes("utf-8");
//utf8Togb2312(rline);
mywrite("E:/vblcfgsc/20181205/18120501_VehicleBLList.db",rline);
/*String gbk = new String(bb,"gbk");
byte[] cc = gbk.getBytes("gbk");
String gb2312 = new String(cc,"gb2312");*/
}
br.close();
}catch(IOException e){
e.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
/** 写文件
* @param str 路径
* @param res 要写入文件的字符串
*/
public static void mywrite(String str,String res){
File file = new File(str); //1、建立连接
OutputStream os = null;
try {
//2、选择输出流,以追加形式(在原有内容上追加) 写出文件 必须为true 否则为覆盖
os = new FileOutputStream(file,true);
// //和上一句功能一样,BufferedInputStream是增强流,加上之后能提高输出效率,建议
// os = new BufferedOutputStream(new FileOutputStream(file,true));
String string = res;
byte[] data = string.getBytes(); //将字符串转换为字节数组,方便下面写入
os.write(data, 0, data.length); //3、写入文件
os.flush(); //将存储在管道中的数据强制刷新出去
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件没有找到!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("写入文件失败!");
}finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭输出流失败!");
}
}
}
}