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