在java中读取文件并打印出文件内容,但是只读取了文件内容的一半,急急急

代码部分:
File file = new File("F:/public.txt");
BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(file));
        String tmpStriing = null;
        int line = 1;
        while((tmpStriing = reader.readLine()) != null){
            System.out.println(tmpStriing);
            line++;
        }
        reader.close();

    } catch (Exception e) {
        // TODO: handle exception

        e.printStackTrace();
    }

现在是我去读取这个文件,然后发现打印出来的时候,是从这个文件内容大概一半的位置开始读取的,上面部分没打印出来,请问是什么原因呢?,

1.如果你在控制台输出,建议使用分段打印,手动操作的方式;
如:在每打印100条休息一下,你既可以看,也可以清除;
休息的代码可以是:Thread.sleep(3000);//休息3秒,也可以这样:java.util.concurrent.TimeUnit.SECONDS.sleep(3);//休息3秒
休息都有抛出中断异常

2.你如果是使用jdk1.7以上版本,建议这样写你的代码:
File file = new File("F:/public.txt");

try (BufferedReader reader = new BufferedReader(new FileReader(file))){
    String tmpStriing = null;
    int line = 0;
    while((tmpStriing = reader.readLine()) != null){
                System.out.println(tmpStriing);
        line++;
                    if(line % 200 == 0) {
                            java.util.concurrent.TimeUnit.SECONDS.sleep(3);
                    }
    }

} catch (Exception e) {
    // TODO: handle exception

    e.printStackTrace();
}


    希望有帮助....

经过测试不存在你说的问题。我用的是数字测试的。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Test {
public static void main(String[] arg){
File file = new File("E:/test/public.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tmpStriing = null;
int line = 1;
while((tmpStriing = reader.readLine()) != null){
System.out.println(tmpStriing);
line++;
}
reader.close();

        } catch (Exception e) {
            // TODO: handle exception

            e.printStackTrace();
        }
}

}

可以完全将内容打印出来

所以不一定是代码的问题,你试着将public.txt复制到public2.txt中然后对比两个文件的结果看看,参考代码如下

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
public static void main(String[] arg) {
File file = new File("E:/test/public.txt");
File file2 = new File("E:/test/public2.txt");
BufferedReader reader = null;
FileWriter write=null;
try {
reader = new BufferedReader(new FileReader(file));
write=new FileWriter(file2);
String tmpStriing = null;
int line = 1;
while ((tmpStriing = reader.readLine()) != null) {
System.out.println(tmpStriing);
line++;
write.write(tmpStriing);
write.write("\n");
write.flush();
}

    } catch (Exception e) {
        // TODO: handle exception

        e.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
            if (write != null) {
                write.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

您好 建议使用GBK格式的 用我的代码试试 希望能解决
// 按行解析歌词
try {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(lyricsFile),"GBK"));
       String line  = buffer.readLine();

        while (line!=null){

            List<Lyric> lineList = parserLine(line);

            lyricsList.addAll(lineList);
            line = buffer.readLine();
        }






    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html

public static String readFile(String filePath[]){
File files=null;
String line="";
String temp="";
String textContent="";
BufferedReader br =null;
InputStreamReader read =null;
try{
for (int i = 0; i < filePath.length; i++) {
files = new File(filePath[i]);
read = new InputStreamReader(new FileInputStream(files),"gb2312");
br = new BufferedReader(read);
//读取文件中的内容
while((line=br.readLine())!=null){
temp = br.readLine();
textContent+=""+temp+"\r\n";
}
}
}catch(Exception e){e.printStackTrace();
}finally{
try{
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
return textContent;
}

测试后可以吧文件都读取出来

代码肯定是可以读取整个文件的,问题是你怎么确定的只读取部分的呢?在eclipse里面看的输出吗?

如果文件太大,eclipse的console装不下完整的输出,前面的输出就被覆盖了,看不到了。。。。

建议使用log日志空间,打印到文件中,此类情况就不会发生了