关闭

ReadTxtFile this.getClass().getResourceAsStream("/ming.txt");

标签: stringexceptionnullclassbufferimport
558人阅读 评论(0) 收藏 举报
分类:
import java.io.*;
class ReadTxt
{
	public void ReadTxtFile()
	{
		String temp="";
		BufferedReader br=null;
		InputStream in = this.getClass().getResourceAsStream("/ming.txt");
		if(in != null)
		{
			br = new BufferedReader(new InputStreamReader(in));
			try{
				while((temp = br.readLine()) != null){
					System.out.println(temp); 
				}
				br.close();
			}catch(Exception e) {
				e.printStackTrace();
			}
			
		}
	}
}

public class TestFile2
{
	/*
getClass().getResource("/ming.txt")	
	*/
/*
Reader reader =new InputStreamReader(  
  getClass().getResourceAsStream("/ming.txt"));
*/

public static void main(String[] args) throws IOException
{
	/*
	FileReader fr = new FileReader(getClass().getResource("/ming.txt"));
	char[] buffer = new char[1024];
	int ch = 0;
	while((ch = fr.read())!=-1 )
	{
		System.out.print((char)ch); 
	}  
	InputStreamReader isr = new InputStreamReader(new FileInputStream(getClass().getResource("/ming.txt")));
	while((ch = isr.read())!=-1)
	{
		System.out.print((char)ch); 
	}  
	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(getClass().getResource("/ming.txt"))));
	String data = null;
	while((data = br.readLine())!=null)
	{
		System.out.println(data); 
	}  
	*/
	ReadTxt Tr=new ReadTxt();
	Tr.ReadTxtFile();

	FileWriter fw = new FileWriter("hello.txt");
	String s = "hello world";
	fw.write(s,0,s.length());
	fw.flush();  
	OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("hello2.txt"));
	osw.write(s,0,s.length());
	osw.flush();  
	PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("hello3.txt")),true);
	pw.println(s);
	/*
	fr.close();
	isr.close();
	br.close();
	*/
	fw.close();
	osw.close();
	pw.close();
} 
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2987700次
    • 积分:40243
    • 等级:
    • 排名:第93名
    • 原创:399篇
    • 转载:4555篇
    • 译文:5篇
    • 评论:609条
    文章分类
    最新评论