Java文件读写方式——以字符串的方式读写

来源:互联网 发布:怎么看设备端口号 编辑:程序博客网 时间:2024/05/22 04:59

以字符串的方式读写

import  java.io.*;

public class T{

publc static void main(String []arg)throw IOException

{

   FileWriter fw = new FileWriter("d:\\Test.txt",true);

   fw.write("gulugulu");

   fw.close();



   FileReader   fr = new FileReader("d:\\Test.txt");

   char d[] = new char[1024];

   fr.read(d);

   System.out.println(d);

   fr.close();


}

}

import  java.io.*;

public class T{

publc static void main(String []arg)throw IOException

{

   FileWriter fw = new FileWriter("d:\\Test.txt",true);

   // FileWriter fw = new FileWriter("d:\\Test.txt");

   //这两条语句相比,仅为true的区别,若有true,则为从已有的Test.txt文件结尾处续写;若无true,则重新写入。

  //若无Test.txt文件,则新建后写入。

   fw.write("gulugulu");

   fw.close();



   FileReader   fr = new FileReader("d:\\Test.txt");

   char d[] = new char[1024];//申请分配1k的内存。

   int  n = fr.read(d);//读d的长度。

   String s = new String(d,0,n);//d数组中,从零开始的n个字符写入字符串s中。

   System.out.println(s);

   fr.close();


}

}


0 0