1. PrintWriter()使用示例
package StreamAndFile;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.Calendar;import java.util.GregorianCalendar;public class TextFileTest { public static class Employee{ String name; double salary; int year,month,day; public Employee(String name,double salary,int year,int month,int day){ this.name=name; this.salary=salary; this.year=year; this.month=month; this.day=day; } public String getName() { return name; } public double getSalary() { return salary; } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } } //将数据写出到文件 public static void writeData(Employee[] staff, PrintWriter out){ for(int i=0;i
程序运行结果如下:
写出成功!读出的数据为:Carl Cracker|75000.0|1987|12|15Harry Hacker|50000.0|1989|10|1Tony Tester|40000.0|1990|3|15
2. 获得字符集别名及所有可用字符集
获得字符集别名:
package StreamAndFile;import java.nio.charset.Charset;import java.util.Set;//输出字符集的别名public class GetCharSet { public static void main(String[] args) { Charset cset = Charset.forName("ISO-8859-1"); Setaliases = cset.aliases(); for(String aliase : aliases){ System.out.println(aliase); } }}
程序运行结果如下:
csISOLatin1latin1IBM-819iso-ir-1008859_1ISO_8859-1:1987ISO_8859-1819l1ISO8859-1IBM819ISO_8859_1ISO8859_1cp819
获得所有可用字符集:
package StreamAndFile;import java.nio.charset.Charset;import java.util.Map;//输出字符集的别名public class GetCharSet { public static void main(String[] args) { //输出所有可用的字符集的名字 Mapcharsets = Charset.availableCharsets(); for(String name:charsets.keySet()){ System.out.println(name); } }}