在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());
} }

java 读写文件例子的更多相关文章

  1. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  2. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

  3. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  4. 【java】java 读写文件

    场景:JDK8  将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...

  5. 转:Java读写文件各种方法及性能比较

    干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...

  6. Java读写文件常用方法

    一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...

  7. 161012、JAVA读写文件,如何避免中文乱码

    1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...

  8. java 读写文件乱码问题

    这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...

  9. java读写文件小心缓存数组

    一般我们读写文件的时候都是这么写的,看着没问题哈.   public static void main(String[] args) throws Exception {   FileInputStr ...

随机推荐

  1. Ubuntu下Ruby的下载和编译源码安装

    1.Ruby的下载 Ruby可以在Ruby 官网上下载,如果想获取更多的Ruby版本,可以到淘宝镜像网站下载. 2.Ruby的编译源码安装 解压 首先把下载下来的源码压缩包解压到自己指定的目录 编译安 ...

  2. IDEA MAVEN Failed to create a Maven project 'C:/gitProjects/mayProj/pom.xml' already exists in VFS

    When adding the module to an existing module that already has a POM, it is necessary to manually spe ...

  3. (转)PCM数据格式

        1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 4410 ...

  4. Ubuntu -- 安装、卸载程序的命令

    通过sudo apt-get install xxxx 安装软件后,总是无法卸载干净,这里以Apache 为例,提供方法:首先sudo apt-get remove apache2再sudo apt- ...

  5. openssl创建自己的CA certificate

    Create a Certificate Authority private key (this is your most important key): $ openssl req -new -ne ...

  6. [原创] Keil uVision5 下载程序 add flash programming algorithm选项缺少需要的算法解决办法

    MDK开发环境从V4升级到V5后,支持包不再是集成到开发环境当中,而是封装在PACK中,需要自行安装,比较麻烦. 搭建MDK开发环境以及破解的方法,在前面的文章中有详细说明,这里不再赘述,有兴趣的可以 ...

  7. MySQL中mysqldump导出数据的使用

    mysqldump常用参数说明: 帮助使用:mysqldump --help -A, --all-databases 导出全部数据库 mysqldump -uroot -p –A > /tmp/ ...

  8. ★Wireshark基本介绍和学习TCP三次握手

    之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括http,TCP,UDP,等网络协议包. 记得大学的 ...

  9. JSON.stringify报cyclic object value错误

    这是一个典型的循环引用的错误,一个对象里引用自己就会立刻得到这个错误: obj = { x:555, y: "hi" }; obj.myself = obj; try{ json ...

  10. android LinearLayout添加分隔线

    方法一: 可以放置一个ImageView组件,然后将其设为分隔线的颜色或图形. 分隔线View的定义代码如下: [html] view plaincopy   <ImageView androi ...