0

I was wondering if there was a way to determine if any streams are open in a program?

I am using some of my code and some of another, and my goal is to be able to write to the same file multiple times, erasing it and rewriting every time. However, I think somewhere, the code belonging to this other group may have forgotten to close a stream, or that Java cannot handle it, maybe? It always writes at the end of the file, instead of at the beginning of a blank file. It will not delete and I cannot rename it if it has already been opened by the program.

If it is an open stream problem, I want to close the stream (which I have gone through the code, and cannot seem to find the open stream). Or if Java cannot handle it, is there a good way (besides making destroy methods) for me to be able to reset/kill objects to be reinstanciated?

Or is there a way to maybe...set the file to null and that erases it? Or should I try to open the file, erase it and set the offset to 0?

any tips would be nice

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sean
  • 1
  • 1
  • As a start let me offer this. File writing is not append mode by default, it is something you have to explicitly set. Therefore the part where you mention "It always writes at the end of the file" makes be believe this other code, or you, are telling it to append. Source code and error messages welcome. – demongolem Jul 05 '11 at 14:09
  • Let me also share this SO link about checking if a file is already open so you can assess the situation http://stackoverflow.com/questions/1390592/java-check-if-file-is-already-open – demongolem Jul 05 '11 at 14:12
  • The other code is telling it to append, yes. Although my goal is to not modify their code (since they release updates, and I would have to always change their code) I tried to set it to false, and the way they write the file fails to write it effectively unless it is set as true. I am looking into a way to erase the file, maybe with the write method that writes a certain number of byes from a specified location to a file. BUT, I am not sure what I should write it as? nulls or blank spaces? @demongolem I am not quite sure how that link is very helpful, except telling me It may not be possible? – Sean Jul 05 '11 at 15:23

2 Answers2

0

Heres some good code that might be usefull to you:

public void writeToNewFile(String filePath, String data)
{
    PrintWriter writer;
    File file;
    try
    {
        file = new File(filePath);
        file.createNewFile();
        writer = new PrintWriter(new FileWriter(file));
        writer.println(data);
        writer.flush();
        writer.close();
     }catch(Exception e){e.printStackTrace();}
     writer = null;
     file = null;
     \\setting file & writer to null releases all the system resources and allows the files to be accessed again later
}

//this will write to end of file
public void writeToExistingFile(String filePath, String data)
{
    PrintWriter writer;
    File file;
    try
    {
        file = new File(filePath);
        if(!file.exists())
            file.createNewFile();
        writer = new PrintWriter(new FileWriter(file,true));
        writer.println(data);
        writer.flush();
        writer.close();
     }catch(Exception e){e.printStackTrace();}
     writer = null;
     file = null;
     \\setting file & writer to null releases all the system resources and allows the files to be accessed again later
}

public String[] readFile(String filePath)
{
    String data[];
    Iterator<String> it;
    ArrayList<String> dataHolder = new ArrayList<String>();
    BufferedReader reader;
    File file;
    try
    {
        file = new File(filePath);
        reader = new BufferedReader(new FileReader(file));

        int lines = 0;
        while(reader.ready())
        {
            lines++;
            dataHolder.add(reader.readLine());
        }

        data = new String[lines];
        it = dataHolder.iterator();
        for(int x=0;it.hasNext();x++)
            data[x] = it.next();

        reader.close();
     }catch(Exception e){e.printStackTrace();}
     reader = null;
     file = null;
     \\setting file & reader to null releases all the system resources and allows the files to be accessed again later
    return data;
}

public void deleteFile(String filePath)
{
    File file;
    try
    {
         file = new File(filePath);
         file.delete();
    }catch(Exception e){e.printStackTrace();}
    file = null;
}

public void createDirectory(String directory)
{
    File directory;
    try
    {
        directory = new File(directory);
        directoyr.mkDir();
    }catch(Exception e){e.printStackTrace();}
    directory = null;
}

Hope this helped!

gsfd
  • 1,070
  • 2
  • 12
  • 17
0

@John Detter, i had already tried a significant portion of that, though that is some good/helpful code.

I solved it by opening the file in a separate thread (when I knew I was not reading/writing from/to it) as a RandomAccessFile. I got the length of the file, then called raf.skipBytes(length) and it erased the file. There are some other weird things that go along with it, but it works for me.

Sean
  • 1
  • 1
  • 1
    John won't get your message. You have it properly formatted but it needs to be a comment not an Answer. – Keng Jul 07 '11 at 15:32