在linux下可以读写中文

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*; public class TestFile {
//写文件,支持中文字符,在linux redhad下测试过
public static void writeLog(String str)
{
try
{
String path="/root/test/testfilelog.log";
File file=new File(path);
if(!file.exists())
file.createNewFile();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
FileOutputStream out=new FileOutputStream(file,false); //如果追加方式用true
StringBuffer sb=new StringBuffer();
sb.append("-----------"+sdf.format(new Date())+"------------\n");
sb.append(str+"\n");
out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集
out.close();
}
catch(IOException ex)
{
System.out.println(ex.getStackTrace());
}
}
public static String readLog()
{
StringBuffer sb=new StringBuffer();
String tempstr=null;
try
{
String path="/root/test/testfilelog.log";
File file=new File(path);
if(!file.exists())
throw new FileNotFoundException();
// BufferedReader br=new BufferedReader(new FileReader(file));
// while((tempstr=br.readLine())!=null)
// sb.append(tempstr);
//另一种读取方式
FileInputStream fis=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
while((tempstr=br.readLine())!=null)
sb.append(tempstr);
}
catch(IOException ex)
{
System.out.println(ex.getStackTrace());
}
return sb.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
writeLog("this is a test log");
writeLog("中文测试看看");
System.out.println(readLog());
} }

最新文章

  1. bat 延时删除指定文件夹中的文件经验分享
  2. js 控制表单提交
  3. oracle job的写法
  4. 知方可补不足~sqlserver中的几把锁~续
  5. 虚拟机VirtualBox 5.1.0|VBOX
  6. Java 动态代理
  7. Debian 基本设置
  8. web.config的奇淫巧技
  9. 关于线程池ThreadPoolExecutor使用总结
  10. easyui扩展-日期范围选择.
  11. PopupWindow 问题集锦
  12. 完全用LINUX工作
  13. Swift - 23 - 选择结构
  14. C# lesson2
  15. join on用法
  16. easyui时间框只选择年月
  17. 【机器学习】--线性回归中L1正则和L2正则
  18. dubbo 在不同协议下携带上下文区别
  19. flask之信号和mateclass元类
  20. Angel - 模拟Kafka数据流调试FTRL的方法

热门文章

  1. Mac使用git/github小结
  2. Python之collections.defaultdict
  3. (笔记)Mysql命令insert into:向表中插入数据(记录)
  4. js 形如 "1,2,3"的操作
  5. C# Image与Base64编码互转函数
  6. 软件设计模式之适配器模式(JAVA)
  7. thinkphp 3.2 多表查询 group
  8. MongoDB 之 幽灵操作避免
  9. php解析mpp文件中的多级任务
  10. C#获取当前时区转换方法