参考 /** convert strings */import java.io.*;public class inputtest {
public static void main(String[] args) { String outfile = null; args = new String[2]; args[0] = "c:/windows/temp/sn.html"; args[1] = "c:/windows/temp/sn1.html"; try { convert(args[0], args[1], "GB2312", "Big5"); } // or "BIG5" catch (Exception e) { System.out.print(e.getMessage()); System.exit(1); } } public static void convert(String infile, String outfile, String from, String to) throws IOException, UnsupportedEncodingException { // set up byte streams InputStream in; if (infile != null) in = new FileInputStream(infile); else in = System.in; OutputStream out; if (outfile != null) out = new FileOutputStream(outfile); else out = System.out; // Use default encoding if no encoding is specified. if (from == null) from = System.getProperty("file.encoding"); if (to == null) to = System.getProperty("file.encoding"); // Set up character stream Reader r = new BufferedReader(new InputStreamReader(in, from)); Writer w = new BufferedWriter(new OutputStreamWriter(out, to)); // Copy characters from input to output. The InputStreamReader // converts from the input encoding to Unicode,, and the OutputStreamWriter // converts from Unicode to the output encoding. Characters that cannot be // represented in the output encoding are output as '?' char[] buffer = new char[4096]; int len; while((len = r.read(buffer)) != -1) w.write(buffer, 0, len); r.close(); w.flush(); w.close(); }}
我就是要用filereader这个类,这是规定
BufferedReader in = new BufferedReader(new FileReader("xiaoniu.txt")); String readstr = ""; while (readstr != null) { readstr = in.readLine(); System.out.print(readstr); }
BufferedReader in = new BufferedReader(new FileReader("xiaoniu.txt")); String readstr = ""; while (readstr != null) { readstr = in.readLine(); System.out.print(new String(readstr.getBytes("ISO8859_1"),"GB2312")); }
leolee 这个办法跑不通。中文还是?
leolee 这个办法跑不通。中文还是问号
这样可以: try{ BufferedReader in = new BufferedReader(new FileReader("d://a.txt")); String readstr = ""; while (readstr != null) { readstr = in.readLine(); System.out.println(readstr); } }catch(Exception ex){ System.out.println(ex.toString()); } }
package gw;
import java.io.*;
public class readtxt
{
public String[] getfiletxt(String getfilepath)
{
try
{
if (getfilepath==null) return new String[]{""};
File fn=new File(getfilepath);
String[] gettxt=new String[255];
int i=0;
if(fn.isFile()&&fn.canRead())
{
RandomAccessFile file=new RandomAccessFile(fn,"r");
while(file.getFilePointer()<file.length())
{
gettxt[i]=file.readLine();
i=i+1;
}
file.close();
}
String[] str1=new String[i+1];
System.arraycopy(gettxt,0,str1,0,str1.length);
return str1;
}
catch(Exception e)
{
e.printStackTrace();
return new String[]{""};
}
} }
下面的我调用的servlet:
/* $Id: HelloWorldExample.java,v 1.2 1999/03/17 02:21:36 duncan Exp $
*
*/
package gw;
import gw.readtxt;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class readtxtservlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String expr=request.getParameter("line");
out.println("<html>");
out.println("<head>");
out.println("<title>example</title>");
out.println("</head>");
out.println("<body>");
readtxt myread=new readtxt();
String[] toUse=myread.getfiletxt("d:\\a.txt");
out.println(toUse.length);
for(int i=0;i<toUse.length;i++)
{
if (toUse[i]==null)
{
break;
}
out.println(toUse[i]+"<br>");
} //String my=new String[255];
//my[1]=myread.getfiletxt("d:\\a.txt");
out.println("以上是读取a.txt的所有内容");
out.println("</body>");
out.println("</html>");
}
}
编译上面这个servlet:javac -encoding ISO8859_1 readtxtservlet.java
/**
convert strings
*/import java.io.*;public class inputtest {
public static void main(String[] args) {
String outfile = null;
args = new String[2];
args[0] = "c:/windows/temp/sn.html";
args[1] = "c:/windows/temp/sn1.html";
try { convert(args[0], args[1], "GB2312", "Big5"); } // or "BIG5"
catch (Exception e) {
System.out.print(e.getMessage());
System.exit(1);
}
} public static void convert(String infile, String outfile, String from, String to)
throws IOException, UnsupportedEncodingException
{
// set up byte streams
InputStream in;
if (infile != null) in = new FileInputStream(infile);
else in = System.in;
OutputStream out;
if (outfile != null) out = new FileOutputStream(outfile);
else out = System.out; // Use default encoding if no encoding is specified.
if (from == null) from = System.getProperty("file.encoding");
if (to == null) to = System.getProperty("file.encoding"); // Set up character stream
Reader r = new BufferedReader(new InputStreamReader(in, from));
Writer w = new BufferedWriter(new OutputStreamWriter(out, to)); // Copy characters from input to output. The InputStreamReader
// converts from the input encoding to Unicode,, and the OutputStreamWriter
// converts from Unicode to the output encoding. Characters that cannot be
// represented in the output encoding are output as '?'
char[] buffer = new char[4096];
int len;
while((len = r.read(buffer)) != -1)
w.write(buffer, 0, len);
r.close();
w.flush();
w.close();
}}
= new BufferedReader(new FileReader("xiaoniu.txt"));
String readstr = "";
while (readstr != null) {
readstr = in.readLine();
System.out.print(readstr);
}
= new BufferedReader(new FileReader("xiaoniu.txt"));
String readstr = "";
while (readstr != null) {
readstr = in.readLine();
System.out.print(new String(readstr.getBytes("ISO8859_1"),"GB2312"));
}
这个办法跑不通。中文还是?
这个办法跑不通。中文还是问号
try{
BufferedReader in
= new BufferedReader(new FileReader("d://a.txt"));
String readstr = "";
while (readstr != null) {
readstr = in.readLine();
System.out.println(readstr);
}
}catch(Exception ex){
System.out.println(ex.toString());
}
}
int readInt = 0;
char readChar;StringBuffer tmpData=new StringBuffer();
String data="";while (readInt != -1) {
readInt = headerFile.read();
if(readInt == -1) break;
readChar = (char) readInt; tmp=tmp.append(readChar);
out.print(readChar);
}
data=tmp.toString();
data=new String(data.getBytes("ISO8859_1"));//或者data=new String(data.getBytes("ISO8859_1"),"GBK"));
out.print(data);
看这样改可以不?