给你一个例子,可以读出中文的。你参考参考。
package com;
import java.io.*;
import java.util.StringTokenizer;public class ReadFile
{private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token; //创建文件对象
public ReadFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
} public ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
} //设置文件路径
public void setPath(String filePath)
{
path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
} //得到文件路径
public String getPath() {
return path;
} //关闭文件
public void fileClose() throws IOException
{
file.close();
} //读取下一行记录,若没有则返回-1
public int nextRecord()
{
int returnInt = -1;
try
{
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("readLine problem, terminating.");
} if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
} //以字符串的形式返回整个记录
public String returnRecord()
{
return currentRecord;
}
}
read.jsp
<%@page contentType="test/html;charset=gb2312"%>
<html>
<head>
<title>JSP读取文件</title>
</head>
<body>
<%--调用javabean --%>
<jsp:useBean id="reader" class="com.ReadFile" scope="request">
<jsp:setProperty name="reader" property="path" value="c:\\tomcat\\webapps\\yourjsp\\datafile.txt" />(或者随便输入一个中文文件路径)
</jsp:useBean>
<h3>文件内容:</h3>
<p>
<% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
<% count++; %>
<b>第<%=count %>行:</b>
<% out.print(reader.returnRecord()); %><br>
<% } %>
</p>
</body>
</html>
datafile
当字符串数据以URL的形式传送到Web服务器时,
在字符串中不允许出现空格,
也不允许出现特殊字符,
因此需要在传送字符串之前进行URL编码,
以保证数据传输的正确性。
URLEncoder方法可以将字符串进行Escape编码,
例如空格转换为%20,
字母不转换,
中文则要转换为两个代码,
形如%EF%CD(16进制)。
package com;
import java.io.*;
import java.util.StringTokenizer;public class ReadFile
{private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token; //创建文件对象
public ReadFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
} public ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
} //设置文件路径
public void setPath(String filePath)
{
path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
} //得到文件路径
public String getPath() {
return path;
} //关闭文件
public void fileClose() throws IOException
{
file.close();
} //读取下一行记录,若没有则返回-1
public int nextRecord()
{
int returnInt = -1;
try
{
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("readLine problem, terminating.");
} if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
} //以字符串的形式返回整个记录
public String returnRecord()
{
return currentRecord;
}
}
read.jsp
<%@page contentType="test/html;charset=gb2312"%>
<html>
<head>
<title>JSP读取文件</title>
</head>
<body>
<%--调用javabean --%>
<jsp:useBean id="reader" class="com.ReadFile" scope="request">
<jsp:setProperty name="reader" property="path" value="c:\\tomcat\\webapps\\yourjsp\\datafile.txt" />(或者随便输入一个中文文件路径)
</jsp:useBean>
<h3>文件内容:</h3>
<p>
<% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
<% count++; %>
<b>第<%=count %>行:</b>
<% out.print(reader.returnRecord()); %><br>
<% } %>
</p>
</body>
</html>
datafile
当字符串数据以URL的形式传送到Web服务器时,
在字符串中不允许出现空格,
也不允许出现特殊字符,
因此需要在传送字符串之前进行URL编码,
以保证数据传输的正确性。
URLEncoder方法可以将字符串进行Escape编码,
例如空格转换为%20,
字母不转换,
中文则要转换为两个代码,
形如%EF%CD(16进制)。
FileInputStream in=new FileInputStream(file_in);
InputStreamReader reader=new InputStreamReader(in,"gb2312");