您的位置:首页 > Web前端

java 使用FileInputStream,FileReader,BufferedStream读写记事本文件的例子

2011-05-11 09:36 861 查看
view plaincopy to clipboardprint?
//在当前文件夹下建立一个文本文件(使用Windows 记事本程序)
//在文件中输入几行文本;
//使用文件输入流类FileInputStream和FileReader类,
//读取上边的文本文件中的内容,并输出到屏幕上。
import java.util.Arrays;
import java.io.*;
class Test
{
public static void main(String []args) throws Exception
{
File myFile=new File("temp.txt");
if(!myFile.exists())
{
System.out.println("can't fine that file"+myFile.getName());
}
//
//使用BufferedReader类读入
//
//      FileReader fis=new FileReader(myFile);
//      BufferedReader br=new BufferedReader(fis);
//      String line;
//      while((line=br.readLine())!=null)
//      {
//          System.out.println(line);
//      }
//      br.close();
//      fis.close();

//
//使用fileInputStream读入
//      FileInputStream fis=new FileInputStream(myFile);
//      byte[] b=new byte[(int)myFile.length()];
//      fis.read(b);
//      String str=new String(b);
//      System.out.print(str);
//      fis.close();
//
//使用FileReader
//
FileReader fr=new FileReader(myFile);
char[] contents=new char[(int)myFile.length()];
fr.read(contents,0,(int)myFile.length()-1);
String result=new String(contents);
System.out.println(result);
fr.close();
}
}; 

http://blog.csdn.net/chenzhehui/archive/2009/04/12/4067482.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐