4

i need some help with creating file

Im trying in the last hours to work with RandomAccessFile and try to achieve the next logic:

  1. getting a file object
  2. creating a temporary file with similar name (how do i make sure the temp file will be created in same place as the given original one?)
  3. write to this file
  4. replace the original file on the disk with the temporary one (should be in original filename).

I look for a simple code who does that preferring with RandomAccessFile I just don't how to solve these few steps right..

edited: Okay so ive attachted this part of code my problem is that i can't understand what should be the right steps.. the file isn't being created and i don't know how to do that "switch"

        File tempFile = null;
    String[] fileArray = null;
    RandomAccessFile rafTemp = null;
    try {
        fileArray = FileTools.splitFileNameAndExtension(this.file);
        tempFile = File.createTempFile(fileArray[0], "." + fileArray[1],
                this.file); // also tried in the 3rd parameter this.file.getParentFile() still not working.
        rafTemp = new RandomAccessFile(tempFile, "rw");
        rafTemp.writeBytes("temp file content");
        tempFile.renameTo(this.file);
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        rafTemp.close();
    }
Popokoko
  • 6,413
  • 16
  • 47
  • 58

2 Answers2

8
    try {
  // Create temp file.
  File temp = File.createTempFile("TempFileName", ".tmp", new File("/"));
  // Delete temp file when program exits.
  temp.deleteOnExit();
  // Write to temp file
  BufferedWriter out = new BufferedWriter(new FileWriter(temp));
  out.write("Some temp file content");
  out.close();
  // Original file
  File orig = new File("/orig.txt");
  // Copy the contents from temp to original file  
  FileChannel src = new FileInputStream(temp).getChannel();
  FileChannel dest = new FileOutputStream(orig).getChannel();
  dest.transferFrom(src, 0, src.size());

  } catch (IOException e) { // Handle exceptions here}
jags
  • 176
  • 5
  • Thanks but im not so sure it fills what i need, this temp file created is not created where the original is located (which is dynamic.. i can't write "/" as root) and second that transferFrom method of copying method is kinda problemtic since i have a lot of content to "move" this is why renaming in some working way might be better idea.. – Popokoko Mar 27 '12 at 10:48
  • 1
    This should help resolve your problem. File tempFile = null; RandomAccessFile rafTemp = null; try { tempFile = File.createTempFile("TempFile", ".tmp"); rafTemp = new RandomAccessFile(tempFile, "rw"); rafTemp.writeBytes("temp file content"); tempFile.renameTo(new File("/destinationpath/dest.txt")); } catch (IOException ex) { ex.printStackTrace(); } finally { rafTemp.close(); } – jags Mar 27 '12 at 11:19
  • erm, I have tried that, still not working :/ either im missing something or the logic isn't right. btw i still need to support the fact a given file is synamid and ur code implements static paths.. – Popokoko Mar 27 '12 at 11:28
  • The path can still be dynamic. I have added in the snippet just for illustrative purpose. And it works fine. What is not working for you. – jags Mar 27 '12 at 11:36
  • Is that working for you? I'm trying to create the temp file in the same place my received file is located, i tried using your code to set in that location but the createTempFile doesn't recieve this path at all and then im stuck at the same point i was. plus, when renaming to that file name it's not actually renaming where it should. as u can see in my code i used "this.file" since it has dynamic location and name. – Popokoko Mar 27 '12 at 11:50
  • Yes. It works for me. Even when I have file as a member variable it works fine. I see that you are using 'this.file' to create your temp file and renaming the temp file again using 'this.file'. Is it intentional? If so it looks wrong. In the createTempFile() method, third parameter is the directory where you want your file to be created. During rename, you are trying to rename the temp file to a file instance pointing to a directory than a valid file itself. And that could be the reason why you are not able to see the file created. – jags Mar 27 '12 at 13:36
1

you can direct overwrite file. or do following

  1. create file in same directory with diff name

  2. delete old file

  3. rename new file

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46