写新文件时数据丢失 [英] Data Loss When I Write A New File

查看:60
本文介绍了写新文件时数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题就像当我读取文件并执行某些操作并写入该文件但当我再次写入时读取50页时我只有47页请告诉我该怎么办。我的代码如下:

problem is like that when i read file and i perform some operation on that and write that file but when i read 50 page when i write again i got only 47 page please suggest what can i do. my code bellow.

public class DataSeperation {
    public static void main(String[] args) throws IOException {
        BufferedReader rd=new BufferedReader(new FileReader("D:\\Data.txt"));
        File tempFile = new File("myTempFile.txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
        String lineToRemove = "208 vaDgaava SaorI ivaQaanasaBaa matadar saMGa : dubaar matadar yaadI Ah-taa idnaaMkx : 1ó1ó2013 va pa`isawI idnaaMkx : 1ó10ó2012";
        
        String currentLine;
        
        while((currentLine =rd.readLine()) != null) {
            // trim newline when comparing with lineToRemove
            String trimmedLine = currentLine.trim();
            if(trimmedLine.equals(lineToRemove)) continue;
            writer.write(System.getProperty("line.separator"));
            writer.write(currentLine);
        }
        
    
    }
    
}

推荐答案

在您的代码中,您正在从读取数据中跳过一些时间,那么您想要什么?在调试器下运行它,看看你是否正在跳过你想要保留的东西。 :-)



-SA
In your code, you are skipping some time from read data, so what do you want? Run it under the debugger and see if you are skipping something you want to keep. :-)

—SA


这篇关于写新文件时数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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