Results 1 to 6 of 6

Thread: File read-write mode in java

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    File read-write mode in java

    Hello,
    I want to create a file that would be write-only mode. But I see no solution for doing that in java. In C / C + +, it specifies the open mode in a parameter in Java I do not know how to do that. Briefly how to perform read write mode operations in java. Thank you for your help.
    Last edited by ISAIAH; 20-01-2010 at 09:53 AM.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: File read-write mode in java

    Hello,
    Is that you can show your need? I think this what you have explained is not so clear, more explanation is needed. If not, creates a temporary file and save it under any name and then I think you have to use the java.io package to perform the needed operations on write and read permission of a file.

  3. #3
    Join Date
    Dec 2009
    Posts
    202

    Re: File read-write mode in java

    Hello,
    I am aware that there are ways such as creating a file in another directory and rename it, but it is not very clean what. All I want is to have the equivalent of C / C + + where you can create a file access mode "a" (there is also a + r, r + w, w +). In summary, I want to create a file and not make it accessible even to read, only when I finished writing this file, i.e after I closed. How can it be achieved, is it that it is not possible in java. Thanks for your answers.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: File read-write mode in java

    Hi,
    All I want is to have the equivalent of C / C + + where you can create a file access mode "a" (there is also a + r, r + w, w +).
    If I am not mistaken you speak of fopen () function which is used in C.
    has : Opens the file for writing. The file is created if it did not exist. The stream is positioned at the end of the file.
    So "a" stands for write access with the append at the end of the file, and that is what fair FileOutputStream when you use the constructor with parameter append to true.
    In summary, I want to create a file and not make it accessible even to read, only when I finished writing this file, ie after I closed.
    Accessible to whom? For other programs, know that the "a" of fopen () does not block read access to the file from other applications. If still it is what you want to do it you must use the FileLock obtained from FileChannel for your OuputStream:
    Code:
    FileOutputStream fout = new FileOutputStream(filename, true);
    FileLock lock = fout.getChannel().lock();
    try {
     
    	/ / Write file
     
     
    } finally {
    	lock.release();
    }
    But the lock depends on the operating system and can vary from one platform to another.

  5. #5
    Join Date
    Dec 2009
    Posts
    202

    Re: File read-write mode in java

    Hi,
    I speak of fopen. Indeed, the "a" describes what you said, but the description of "a +" says it also opens the file for reading, suggesting that the "a" file was not so much playback mode. But in fact, seems FileLock do what I want. And I do not understand why it is platform-dependent. Finally I say that but I doubt it's got to be a valid reason, at least I hope. The safest way seems to be good to go through a temporary file and rename it, I think it's a shame but good. Why is it not an access mode describing each situation: R, R / W, W, Read-only; Read-only/Write; Write-only, etc. .... ?

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: File read-write mode in java

    Hello,
    The philosophy of Java is different from that of C. This is something that must be understood before attempting to do anything. In Java, we use the concept of data flows for input-output. Inflow or outflow, which may be the keyboard, screen, file, the output of a servlet, a buffer, the exit where the standard input in Unix, etc. It is an advantage. You can easily schedule a method that takes a stream parameter. No matter if this flow then gives the output of a servlet, a buffer memory, or file. It's something I have been very useful for generating pdf documents in several contexts. Knowing this, write to the end of a file is to write out a stream that points to a file. So use the class FileOutputStream. The class FileLock avoids access concurrent. The API of these classes will give you all the details.

Similar Threads

  1. Need to read input text and write it to output file
    By Tramba-keshwar in forum Software Development
    Replies: 6
    Last Post: 26-06-2011, 10:38 PM
  2. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  3. Replies: 5
    Last Post: 02-02-2010, 04:20 PM
  4. How to read and write files in Java
    By BansiJ in forum Software Development
    Replies: 3
    Last Post: 02-09-2009, 08:52 PM
  5. How to create read write lock in java
    By AlienKing in forum Software Development
    Replies: 3
    Last Post: 05-05-2009, 08:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,492,403.22286 seconds with 17 queries