日期:2014-05-20  浏览次数:20897 次

JAVA打印TXT
谁能给一段JAVA打印TXT文件的代码,要求中文不乱码。。。。。感激不尽!

------解决方案--------------------
package com.test;

import java.io.*;

public class TestPrinter {

final int BUFFER_LENGTH=1024;

public void formatFlie(String fileName) {
char[] c=new char[BUFFER_LENGTH];//buffered area
String inStr="";//read String
try {
BufferedReader readBuff=new BufferedReader(new FileReader(fileName)); 
while(readBuff.read(c,0,BUFFER_LENGTH)!=-1) {
//System.out.println("pos:"+read_pos);
for(int i=0;i<BUFFER_LENGTH;i++)
inStr+=c[i];
}
System.out.println(inStr);
readBuff.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
}

------解决方案--------------------
Java code

public void printFile(String fileName){
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            while(reader.ready()){
                buffer.append(reader.readLine());
            }
            System.out.println("输出文件内容的结果为:" + buffer.toString());
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }