Java 如何使用BufferedInputStream读取法语字符
我试图从文件中读取一些法语字符,但如果字母中包含字母Aéè,则会出现一些符号。 谁能指导我如何获得文件的实际字符。 这是我的主要方法Java 如何使用BufferedInputStream读取法语字符,java,Java,我试图从文件中读取一些法语字符,但如果字母中包含字母Aéè,则会出现一些符号。 谁能指导我如何获得文件的实际字符。 这是我的主要方法 public static void main(String args[]) throws IOException { char current,org; //String strPath = "C:/Documents and Settings/tidh/Desktop/BB/hhItem01_2.txt"; String
public static void main(String args[]) throws IOException
{
char current,org;
//String strPath = "C:/Documents and Settings/tidh/Desktop/BB/hhItem01_2.txt";
String strPath = "C:/Documents and Settings/tidh/Desktop/hhItem01_1.txt";
InputStream fis;
fis = new BufferedInputStream(new FileInputStream(strPath));
while (fis.available() > 0) {
current= (char) fis.read(); // to read character
// from file
int ascii = (int) current; // to get ascii for the
// character
org = (char) (ascii);
System.out.println(org);
}
您正在尝试实际使用ASCII读取UTF-8字符。以下是如何实现您的功能的示例:
public class Test {
private static final FILE_PATH = "c:\\temp\\test.txt";
public static void main(String[] args){
try {
File fileDir = new File(FILE_PATH);
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileDir), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
参考:以下假设文本为Windows Latin-1,但我添加了UTF-8
private static final String FILE_PATH = "c:\\temp\\test.txt";
Path path = Paths.get(FILE_PATH);
//Charset charset = StandardCharset.ISO_8859_1;
//Charset charset = StandardCharset.UTF_8;
Charset charset = Charset.forName("Windows-1252");
try (BufferedReader in = Files.newBufferedReader(path, charset)) {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
字符串行
将包含Unicode格式的文本。现在取决于System.out是否可以使用Unicode的转换在系统编码中表示该Unicode
System.out.println("My encoding is: " + System.getProperty("file.encoding"));
但是,如果选择了正确的编码,则每个特殊字符最多只能有一个?
。如果你觉得每个特殊字符都更合适,那么就使用UTF-8——一种多字节编码
为控制台选择支持Unicode的字体
检查是否已获得é
:
String e = "\u00e9";
String s = new String(Files.readAllBytes(path), charset);
System.out.println("Contains e´ : " + s.contains(e));
评论后: 最好使用Files.newbuffereder(我在上面更正了它),因为它可以执行以下操作
try (BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(file), charset))) {
这种缓冲区用于更快的读取,InputStreamReader使用二进制数据InputStream加上字符集将其转换为读卡器的(Unicode)。IBM提供的法语特定编码是CP1252(首选,因为它在所有操作系统上运行) 问候,
一个法国人你可以为ApacheCommons IO下载一个jar文件,并尝试通过读取每一行而不是逐字符读取来实现它
List<String> lines = IOUtils.readLines(fis, "UTF8");
for (String line: lines) {
dbhelper.addDataRecord(line + ",'" + strCompCode + "'");
}
List lines=IOUtils.readLines(fis,“UTF8”);
用于(字符串行:行){
dbhelper.addDataRecord(行+”,“+strCompCode+”);
}
我尝试了你告诉我的方式,但没有解决我的问题。我得到的文件(français)输出中的单词(fran?ais)Java编码为UTF-8;控制台解码为IBM850。java-Dfile.encoding=UTF-8有关更多信息,请参阅输出问题是输入问题的镜像:使用正确的编码。请参见以下答案:@VikashKumar如果您使用命令行查看结果,您不可避免地会看到“fran?ais”,因为它通常不支持UTF-8。尝试使用IDE(如Eclipse)或将其写入文件中进行测试:谢谢Joop。非常感谢你所说的。你能帮我实现我所附代码中的编码内容吗?这意味着我如何将这些内容与BufferedInputStream一起使用。我更正了我的原始答案,有一个复制错误,文件。NewBuffereder
是有意的。类文件有很多好处,比如读取一系列行:list
。