当前位置:编程学习 > JAVA >>

用java编写一个应用程序,读取一个文本的内容。

用java编写一个应用程序,读取一个文本的内容。提示:FileInputStream fin=null; fin =new FileInputStream(“D:/…..”); //要读取数据的文件路径 while((int i=fin.read())!=-1) { System.out.print((char)i); i=fin.read(); } fin.close();
答案:import java.io.*;
public class Input 
{
	public static void main(String[] args) throws Exception
	{
		FileInputStream fis = new FileInputStream("D:/abc.txt");
                                //把字符输入流转换成字节流,并且套上缓冲流管子
		BufferedReader buf = new BufferedReader(new InputStreamReader(fis));
		String s = null;
		while((s=buf.readLine())!=null)
			System.out.println(s);
		buf.close();
	}
}
其他:编写一个Java应用程序,开启一个文本文件(以本程序源文件为例读取),一次import java.io.*; import java.util.LinkedList; public class public String readData(){
		//数据库文件位置
		String url ="D:/data.txt"; 
		String strs = ""; 
		try{
			//读取文件
			FileReader read = new FileReader(new File(url)); 
			StringBuffer sb = new StringBuffer(); 
			char ch[] = new char[1024]; 
			int d = read.read(ch);
			while(d!=-1){ 
				String str = new String(ch,0,d); 
				sb.append(str); 
				d = read.read(ch); 
			} 
			String a = sb.toString();//.replaceAll("@@@@@", ",");
			strs = a;//.substring(0,a.length()-1);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return strs;
	} 

上一个:关于java的一个疑问 高手解答下啊?
下一个:java 线程

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,