javaio-WriteStringToFile


package cn.yu.test.io;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
/**
* WriteStringToFile
* @version 1.0
* @date 2010/5/10
**/
public class WriteStringToFile {
public static void main(String[] args) {
try {
String aString = "Hello你好";

FileWriter fw = new FileWriter("c:/out.fw.txt");
fw.write(aString);
fw.close(); //默认gbk编码9字节

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("c:/out.osw.txt"),"UTF-8");
osw.write(aString);
osw.close(); //utf-8编码 11字节

///
DataOutputStream dos = new DataOutputStream(new FileOutputStream("c:/out.dos.txt"));
dos.writeChars(aString);
dos.close(); //14个字节, 每个字符用该字符2字节的unicode表示
//
PrintStream ps = new PrintStream(new FileOutputStream("c:/out.ps.txt"));
ps.print(aString);
ps.close(); //默认gbk编码9字节
//
PrintWriter pw = new PrintWriter(new FileOutputStream("c:/out.pw.txt"));
pw.print(aString);
pw.close(); //默认gbk编码9字节
///
RandomAccessFile ra = new RandomAccessFile("c:/out.fa.txt", "rw");
ra.writeUTF(aString); //
ra.close(); //13个字节 ,前两个直接表示一个无符号整数,值是这个字符串的字符所需要的字节数

RandomAccessFile ra2 = new RandomAccessFile("c:/out.fa2.txt", "rw");
ra2.writeChars(aString);
ra2.close(); //14个字节, 每个字符用该字符2字节的unicode表示
} catch (Exception e) {
e.printStackTrace();
}
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值