java字符串写入读取

2019年3月18日01:19:13 发表评论 1,135 views
/**
 * Created by xabcd on 2019/3/18.
 */
import java.io.*;
public class writeandreadfile {
    public static void main(String args[]) {
        File f = new File("c:\\xxx.txt");
        Writer out = null;
        try {
            out = new FileWriter(f);

        } catch (IOException e) {
            e.printStackTrace();
        }
        String s = new String("Hello World!!!");
        try {
            out.write(s);
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e9) {
            e9.printStackTrace();
        }


        Reader in = null;
        try {
            in = new FileReader(f);

        } catch (IOException e3) {
            e3.printStackTrace();
        }
//        String ss = new String ss[1024];
        char c1[] = new char[1024];
        int i = 0;
        try {
            i = in.read(c1);
//            in.close();
        }
        catch (IOException e4) {
            e4.printStackTrace();
        }

        try {
            in.close();
        }
        catch(IOException e8)
        {
            e8.printStackTrace();
        }
        System.out.println(new String(c1, 0, i));
        System.out.println(i);
//        System.out.println(c1);

    }
}

  • A+
所属分类:java

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: