Java编码

Java编码
Java编码
Java编码
Java编码
Java编码
Java编码
Java编码
Java编码

java关于txt文件读写

package com.zyb.pric;

import java.io.*;

public class FileOperate {

    public static void main(String[] args) {
    writeFile();
    readFile();
    }
    public static void writeFile(){
        FileOutputStream fos=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        String charSet="utf-8";//写入的字符编码

        try {
            fos=new FileOutputStream("d:/test.txt");//节点类
            osw= new OutputStreamWriter(fos,charSet);//转化类
            bw=new BufferedWriter(osw);//装饰类
            bw.write("我是");
            bw.newLine();
            bw.write("hututu");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    public static void readFile(){
        FileInputStream fis=null;
        InputStreamReader isr=null;
        BufferedReader br=null;
        String charSet="GB2312";//写入的字符编码

        try {
            fis=new FileInputStream("d:/test.txt");//节点类
            isr=new InputStreamReader(fis,charSet);//转化类
            //isr=new InputStreamReader(fis);采用系统默认编码
            br=new BufferedReader(isr);//装饰类
            String line;
            while((line=br.readLine())!=null){
                System.out.println(line);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

上一篇:Java知识点汇总--网络编程


下一篇:java课程设计---个人博客 彩票抽奖程序 201821123098 钟海清