java 关于中文字的输出 输入 和文件操作(对象序列化)

通过修改文件的属性编码如图  属性-》编码-》GBK即可 直接用Scanner输入,System.out输出

 

1.中文文件的读写用字符流PrintWriter 、FileReader 、FileWriter、 BufferedReader、 BufferedWriter 等,读取文件一定要用字符流。(BufferedReader、 BufferedWriter比FileReader 、FileWriter在方法兼容的基础上,提供的方法更多,PrintWriter提供的方法比BufferedWriter方便但是仅限于文件的写入)

2.如果是文件操作对象是类对象的话 可用对象序列化 ObjectInputStream、ObjectOutputStream等。

3.以上 流的具体用法参考http://download.csdn.net/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();
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值