FileNotFoundException on opening FileOutputStream on same file read using java nio

StackOverflow https://stackoverflow.com/questions/2146656

  •  23-09-2019
  •  | 
  •  

문제

I have a text editor that uses code almost identical to the below for reading and writing. When I open small files for editting, I always get this error when I try to save them again. The really interesting thing though is that my text editor works fine for files with about 600 bytes or more (seems to vary each time), behaviour that I haven't been able to replicate here.

My current workaround is to check for file size < 1024 bytes, and use java.io if that's the case.

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;

public class Test {
    public static void main(String[] args) {
        String text = null;
        try {
            FileInputStream fis = new FileInputStream("test.txt");
            FileChannel ifc = fis.getChannel();
            CharBuffer cb = Charset.forName("ISO-8859-15").newDecoder().
                    decode(ifc.map(FileChannel.MapMode.READ_ONLY, 0,
                    (int) ifc.size()));
            text = cb.toString();
            ifc.close();
            fis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        try {
            FileOutputStream fos = new FileOutputStream("test.txt");
            OutputStreamWriter osw = new OutputStreamWriter(
                    fos, "ISO-8859-15");
            osw.write(text);
            osw.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

For the above code, I get this error for various sizes of test.txt from 4 bytes to 10kb:

java.io.FileNotFoundException: test.txt (The requested operation cannot be perfo
rmed 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 Test.main(Test.java:23)

Can anyone explain what I'm doing wrong and how I can get this to work while still reading with nio?

도움이 되었습니까?

해결책

Stephen C told you to unmap the buffer.

However, here is a link to STILL OPEN ( from 2002 ) request for MappedBybeBuffer.unmap method.

It has a few workarounds suggested, so you may try them.

다른 팁

2010 년 + Visual WebPart를 만들 때 자동으로!

프로젝트의 Visual Studios 내에서 ASCX 파일과 해당 ASCX.CS 파일 ...을보아야합니다. 그런 다음 사용자 컨트롤 (ASCX)을 호스팅하는 WebPart가 있으며 .cs

솔루션 탐색기에서는 다음과 같이 보일 것입니다 :

 mywebpart.cs
 myVisualcontrol.ascx
  --myVisualcontrol.ascx.cs
.

.cs ASCX 파일을 참조하고 컨트롤 트리에 추가해야합니다.

    private const string _ascxPath = @"~/_CONTROLTEMPLATES/VisualWebPart1/myVisualcontrol.ascx";

    protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);
    }
.

2007 년에 당신은 자신을 자신을 만드는 데 필요합니다.) 자신을 직접 만드는 것을 포함하여!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top