File is overwritten in java - user open error opening

The next function in the java program is written with the intention of reading from a file and overwriting back to the same file after.

public static void readOverWrite(File dir) throws IOException { for (File f : dir.listFiles()) { String[] data = readFile(f).split("\n"); try (BufferedWriter writer = new BufferedWriter(new FileWriter(f))) { for (int i = 0; i < data.length; i++) { writer.write((data[i]+"\n")); } writer.close(); } } } 

Error message when trying to start the program:

 Exception in thread "main" java.io.FileNotFoundException: ..\..\data\AQtxt\APW19980807.0261.tml (The requested operation cannot be performed on a file with a user-mapped section open) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileWriter.<init>(Unknown Source) at General.SplitCreationDate.splitLine(SplitCreationDate.java:37) at General.SplitCreationDate.main(SplitCreationDate.java:53) 

Request help resolving the error.


Code for readFile

 protected static String readFile(File fullPath) throws IOException { try(FileInputStream stream = new FileInputStream(fullPath)) { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); stream.close(); return Charset.defaultCharset().decode(bb).toString(); } } 

Read in another thread that this is a windows problem, and therefore the MappedByteBuffer in the readFile method caused the problem. Rewrote the readFile method as shown below. He works!

 protected static String readFile(File fullPath) throws IOException { String string = ""; try (BufferedReader in = new BufferedReader(new FileReader(fullPath))) { String str; while ((str = in.readLine()) != null) { string += str + "\n"; } } return string; } 
+4
source share
2 answers

You need to close your file streams after they open, or they will still be on your system. This can lead to corruption and such errors. Check out the Java tutorials for file input / output . This tutorial also shows a way.

 import java.io.*; public class ReadWriteTextFile { /** * Fetch the entire contents of a text file, and return it in a String. * This style of implementation does not throw Exceptions to the caller. * * @param aFile is a file which already exists and can be read. */ static public String getContents(File aFile) { //...checks on aFile are elided StringBuilder contents = new StringBuilder(); try { //use buffering, reading one line at a time //FileReader always assumes default encoding is OK! BufferedReader input = new BufferedReader(new FileReader(aFile)); try { String line = null; //not declared within while loop /* * readLine is a bit quirky : * it returns the content of a line MINUS the newline. * it returns null only for the END of the stream. * it returns an empty String if two newlines appear in a row. */ while (( line = input.readLine()) != null){ contents.append(line); contents.append(System.getProperty("line.separator")); } } finally { input.close(); } } catch (IOException ex){ ex.printStackTrace(); } return contents.toString(); } 

Pay attention to the finally block. This ensures that the thread will be closed if an exception occurs or not. You must use it to close open threads.

0
source

Memory mappings do not close when the main file is closed. Use another method to read the file into memory, if necessary. It is usually preferable to process files at a time.

0
source

Source: https://habr.com/ru/post/1432823/


All Articles