简体   繁体   中英

I am not able to write all data to a file

I have written the Java code to read from one file and write to a new file. The file from which I am reading has 5000 lines of records, but when I am writing to a new file I am able to write only between 4700-4900 records.

I think may be I am simultaneously reading from a file and writing to a file, which might be creating a problem.

My code is as follows:

Reading from a file:

public String readFile(){
    String fileName = "/home/anand/Desktop/index.txt";
    FileReader file = null;  
    try {
        file = new FileReader(fileName);
        BufferedReader reader = new BufferedReader(file);
        String line = "";
        while ((line = reader.readLine()) != null) {
            line.replaceAll("ids", "");
            System.out.println(line);
            returnValue += line + "\n";
        }
        return returnValue;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (IOException e) {
                // Ignore issues during closing 
            }
        }
    }
}

Writing to a file:

public void writeFile(String returnValue){
    String newreturnValue = returnValue.replaceAll("[^0-9,]", "");      
    String delimiter = ",";
    String newtext ="";
    String[] temp;
    temp = newreturnValue.split(delimiter);
    FileWriter output = null;
    try {
        output = new FileWriter("/home/anand/Desktop/newinput.txt");
        BufferedWriter writer = new BufferedWriter(output);
        for(int i =0; i < temp.length ; i++){
            writer.write("["+i+"] "+temp[i]);
            writer.newLine();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                // Ignore issues during closing
            }
        }
    }
}

I need the suggestion to how to simultaneously read and write to a file.

You need to close writer instead of output . The BufferedWriter may not be writing all of the lines, and won't since you never close it.

You have to close the writer object. The last couple lines probably haven't been flushed onto the text file.

In addition, are you aware of the try-with-resource introduced in Java 7? You can condense your code to this by utilizing it:

 public String readFile(){
      String fileName = "/home/anand/Desktop/index.txt";

      try(BufferedReader reader = new BufferedReader(new FileReader(filename)) {

        String line = "";
        while ((line = reader.readLine()) != null) {
               line.replaceAll("ids", "");
               System.out.println(line);
               returnValue += line + "\n";
             }
        return returnValue;
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
    }

By doing this, Java will automatically close the reader object for you once the try block completes, regardless of whether or not an exception was thrown. This makes it easier to read your code :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM