123

I have a function that accepts File as an argument. I don't want to create/write a new File (I don't have write access to filesystem) in order to pass my string data to the function. I should add that the String data don't exist in a file (so I cannot read my data from a file).

Can I use Streams and "cast" them to File objects?

2
  • 2
    Can you link/post the code that takes a File object? As the answers have stated, a File object is like a reference to a location in the filesystem, and so would be hard to simulate without an actual file. If, however, your function took a FileReader or a FileInputStream, you could easily modify the code to accept non-file input. Commented Aug 16, 2011 at 19:21
  • Memory mapped files can be your choice. These are special files in Java which allows Java program to access contents directly from memory. For details please go through this link : javarevisited.blogspot.com/2012/01/… Commented Mar 26, 2015 at 19:57

5 Answers 5

82

Usually when a method accepts a file, there's another method nearby that accepts a stream. If this isn't the case, the API is badly coded. Otherwise, you can use temporary files, where permission is usually granted in many cases. If it's applet, you can request write permission.

An example:

try {
    // Create temp file.
    File temp = File.createTempFile("pattern", ".suffix");

    // Delete temp file when program exits.
    temp.deleteOnExit();

    // Write to temp file
    BufferedWriter out = new BufferedWriter(new FileWriter(temp));
    out.write("aString");
    out.close();
} catch (IOException e) {
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cannot request write permission (it's Google AppEngine)
52

No; instances of class File represent a path in a filesystem. Therefore, you can use that function only with a file. But perhaps there is an overload that takes an InputStream instead?

Comments

12

A File object in Java is a representation of a path to a directory or file, not the file itself. You don't need to have write access to the filesystem to create a File object, you only need it if you intend to actually write to the file (using a FileOutputStream for example)

1 Comment

If you need to pass a file to an external API, you need an actual file - passing an "empty" File object won't work.
8

The File class represents the "idea" of a file, not an actual handle to use for I/O. This is why the File class has a .exists() method, to tell you if the file exists or not. (How can you have a File object that doesn't exist?)

By contrast, constructing a new FileInputStream(new File("/my/file")) gives you an actual stream to read bytes from.

Comments

0
FileReader r = new FileReader(file);

Use a file reader load the file and then write its contents to a string buffer.

example

The link above shows you an example of how to accomplish this. As other post to this answer say to load a file into memory you do not need write access as long as you do not plan on making changes to the actual file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.