readfile.java

来自「这套JSP学习资料」· Java 代码 · 共 87 行

JAVA
87
字号
import java.io.*;

public class ReadFile
{
	String filePath="c:/",fileName="";
	
	public void setFilePath(String s)
	{
		filePath=s;
		try
		{
			byte a[]=filePath.getBytes("ISO-8859-1");
			filePath=new String(a);
		}catch(Exception e){}
	}

	public String getFilePath(){return filePath;}

	public void setFileName(String s)
	{
		fileName=s;
		try
		{
			byte a[]=fileName.getBytes("ISO-8859-1");
			fileName=new String(a);
		}catch(Exception e){}
	}

	public String getFileName(){return fileName;}
	
	public String[] listFile()
	{
		File dir=new File(filePath);
		String file_name[]=dir.list();
		return file_name;
	}

	public StringBuffer readFile()
	{
		try
		{
			File file=new File(filePath,fileName);
			FileReader in=new FileReader(file);
			PushbackReader push=new PushbackReader(in);
			StringBuffer stringbuffer=new StringBuffer();
			int c;
			char b[]=new char[1];
			while((c=push.read(b,0,1))!=-1)
			{
				String s=new String(b);
				if(s.equals("<"))
				{
					push.unread('&');
					push.read(b,0,1);
					stringbuffer.append(new String(b));
					push.unread('L');
					push.read(b,0,1);
					stringbuffer.append(new String(b));
					push.unread('T');
					push.read(b,0,1);
					stringbuffer.append(new String(b));
				}
				else if(s.equals(">"))
				{
					push.unread('&');
					push.read(b,0,1);
					stringbuffer.append(new String(b));
					push.unread('G');
					push.read(b,0,1);
					stringbuffer.append(new String(b));
					push.unread('T');
					push.read(b,0,1);
					stringbuffer.append(new String(b));
				}
				else if(s.equals("\n"))
					stringbuffer.append("<BR>");
				else
					stringbuffer.append(s);
			}
			push.close();
			in.close();
			return stringbuffer;
		}catch(IOException e)
		{return new StringBuffer("不能读取文件");}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?