通过修改文件的属性编码如图 属性-》编码-》GBK即可 直接用Scanner输入,System.out输出
1.中文文件的读写用字符流PrintWriter 、FileReader 、FileWriter、 BufferedReader、 BufferedWriter 等,读取文件一定要用字符流。(BufferedReader、 BufferedWriter比FileReader 、FileWriter在方法兼容的基础上,提供的方法更多,PrintWriter提供的方法比BufferedWriter方便但是仅限于文件的写入)
2.如果是文件操作对象是类对象的话 可用对象序列化 ObjectInputStream、ObjectOutputStream等。
3.以上 流的具体用法参考http://download.youkuaiyun.com/detail/l631068264/6478617或者看相关参考书
JDK1.7的我暂时找不到中文的
4.亲,最后一定要有**.close();不要忘了,漏了这个不会提示有错,文件内容为空
5.最后贴上一个小应用帮助理解。
/* * 中文 输入输出流 * 作者:冷秋月 */ package stu; import java.io.*; import java.util.Scanner; class Student implements Serializable { static final long serialVersionUID = 12345L;//呢个东东有咩用,听老师说是用于升级软件,那个大神能详解一下。 String name; String tel; String birthday; public Student(String n, String t, String b) { name = n; tel = t; birthday = b; } } public class stu { public static void main(String[] args) { // TODO code application logic here int n = 0, i; Boolean finished = true; Student stu[] = new Student[300]; try { while (finished) { Scanner cin = new Scanner(System.in); //数据输入 String name; String tel; String birthday; System.out.println("输入学生姓名"); name = "学生姓名 " + cin.nextLine(); System.out.println("输入学生联系方式"); tel = "学生联系方式 " + cin.nextLine(); System.out.println("输入学生生日(如1992/01/01)"); birthday = "学生生日 " + cin.nextLine(); Student s = new Student(name, tel, birthday); //对象放进类数组中 stu[n] = s; n++; //判断是否继续 System.out.println("继续录入按1,停止按0"); int c = cin.nextInt(); if (c == 0) { finished = false; } } //另外建一个新txt 共查看数据因为data 为乱码 PrintWriter pf = new PrintWriter("学生数据.txt"); for (i = 0; i < n; i++) { pf.println(stu[i].name); pf.println(stu[i].tel); pf.println(stu[i].birthday); } pf.close(); /*遇上面所写的PrintWriter的代码功能一样 BufferedWriter bwf=new BufferedWriter(new FileWriter("学生数据.txt")); for (i = 0; i < n; i++) { bwf.write(stu[i].name); bwf.newLine(); bwf.write(stu[i].tel); bwf.newLine(); bwf.write(stu[i].birthday); bwf.newLine(); } bwf.close(); */ //写入对象 ObjectOutputStream inf = new ObjectOutputStream(new FileOutputStream("data.txt")); inf.writeObject(stu); inf.close(); //读出对象 ObjectInputStream outf = new ObjectInputStream(new FileInputStream("data.txt")); stu = (Student[]) (outf.readObject()); for (i = 0; i < n; i++) { System.out.println(stu[i].name); System.out.println(stu[i].tel); System.out.println(stu[i].birthday); } outf.close(); } catch (Exception e) { System.out.println("发生异常" + e); e.printStackTrace(); } } }