Java说非空文件是空的? [英] Java says a nonempty file is empty?

查看:136
本文介绍了Java说非空文件是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定文件,说Java是空的......

I have a particular file that Java says is empty...

源代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MinimumWorkingExample
{
    public static void main(String[] args) throws FileNotFoundException
    {
        String filename = "/home/tyson/Data/English-French_test/test/test.f";
        Scanner fileIn = new Scanner(new File(filename));
        System.out.println("***START***");
        while(fileIn.hasNextLine())
        {
            System.out.println(fileIn.nextLine());
        }
        System.out.println("***FINISH***");
    }
}

输出

***START***
***FINISH***

...但该文件不为空:

...but the file is not empty:

控制台

tyson@tyson-desktop:~$ head /home/tyson/Data/English-French_test/test/test.f
<s snum=0001> 2 .  </s>
<s snum=0002> 2 .  </s>
<s snum=0003> oh , oh !  </s>
<s snum=0004> oh , oh !  </s>
<s snum=0005> oh , oh !  </s>
<s snum=0006> souvenons - nous , monsieur le Orateur , que ce sont ces secteurs de notre soci�t� qui servent de �pine dorsale � notre �conomie .  </s>
<s snum=0007> bravo !  </s>
<s snum=0008> bravo !  </s>
<s snum=0009> monsieur le Orateur , ma question se adresse � le ministre charg� de les transports .  </s>
<s snum=0010> tous deux poss�dent de nombreuses ann�es de exp�rience dans la fabrication et la distribution de les produits forestiers .  </s>
tyson@tyson-desktop:~$ 

问题

为什么会发生这种情况?

Why is this happening???

推荐答案

也可以使用Scanner fileIn = new Scanner(新文件(filename),Cp1252);因为这是法语的编码,你的系统似乎是UTF-8。
如果扫描程序认为读取UTF-8多字节,则扫描程序可能存在编码问题。

Also do Scanner fileIn = new Scanner(new File(filename), "Cp1252"); as this is the encoding for French, and your system seems to be UTF-8. The Scanner might have encoding problems if it thinks to read UTF-8 multibytes.

这篇关于Java说非空文件是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆