Чтение текстового файла в кодировке UTF-8
//Чтение текстового файла в кодировке UTF-8 //в кодировке UTF-8 package readwrite;
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException;
public class readTextUTF8 { static String readText(String filename) throws IOException{ StringBuffer sb = new StringBuffer(2048); String res=""; String str; try { BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(filename), "UTF8")); while ((str = in.readLine()) != null) { sb.append("\n"+in.readLine()); } } catch (UnsupportedEncodingException e) { } catch (IOException e) { }
res=sb.toString(); return res; } public static void main(String[] args) throws IOException { String out=readText("/MyDir/myfile.txt"); System.out.println("text "+out); }
} 04.01.2009
|