I want convert InputStream to FileStream on Android
Process process = Runtime.getRuntime().exec(cmd);
InputStream stdout = process.getInputStream();
FileInputStream fis = (FileInputStream)stdout;
FileDescriptor fd = fis.getFD();
"cmd" is stream command.
Is it impossible?
If possible, how can I fix it?
Use ClassLoader#getResource() instead.
URL resource = classLoader.getResource("Resource_Name");
File file = new File(resource.toURI());
FileInputStream input = new FileInputStream(file);
That said, I really don't see any benefit of doing so, or it must be required by a poor helper class/method which requires FileInputStream instead of InputStream. If you can, just use InputStream instead.
Related
How can I convert a MultipartFile to FileInputStream in memory?
I have tried to below , but i am facing the error as
org.springframework.web.multipart.commons.CommonsMultipartFile cannot
be cast to java.io.File
My Code is
FileInputStream fis = new FileInputStream((File)file);
where file is a multipart file
You can't create an instance of FileInputStream unless your file is not on file system.
You have to either first save the multipart file in temporary location on server using
file.transferTo(tempFile);
InputStream stream = new FileInputStream(tempFile);
But multipart file can also be read simply via basic streams methods such as
InputStream inputStream = new BufferedInputStream(file.getInputStream());
Try using:
MultipartFile uploadedFile = ((MultipartHttpServletRequest)request).getFile('file_name')
InputStream inputStream = new ByteArrayInputStream(uploadedFile?.getBytes())
Take look at MultipartFile
In that you can go with :
void transferTo(File dest)
This method transfer the received file to the given destination file.
MultipartFile file;
InputStream inputStream = file.getInputStream();
To convert Multipart file to Input Stream
MultipartFile file;
InputStream inputStream = new InputStream(file.getInputStream());
This worked for me.
We may just cast and use like below
FileInputStream file = (FileInputStream) multipartFile.getInputStream();
For a multipart file eg:
FileMultipartData part = new FileMultipartData();
InputStream inputStream = part.getFileMultipart().get(0).getByteStream();
This worked for me in my code. Please try it
The program runs well with getResource, but after made it into the jar file, it has FileNotFoundException. It cannot find out test.conf.
My code is
URL url = getClass().getResource("test.conf");
File fin = new File(url.getPath());
FileInputStream fis = null;
try {
fis = new FileInputStream(fin);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
I think I can fix the problem by using getResourceAsStream. But I'm not sure how to change getResource to getResourceAsStream.
Your example suggests that your reading code already works on a FileInputStream. You should be able to make it work with the more general InputStream. This will allow you to just pass the result of getResourceStream() to your reading code, without having to worry about Files.
So:
URL url = getClass().getResource("test.conf");
File fin = new File(url.getPath());
FileInputStream fis = new FileInputStream(fin);
ReadResourceFromStream(fis);
Changes to:
InputStream is = getClass().getResourceAsStream("test.conf");
ReadResourceFromStream(is);
i have a code that takes a file, zip it, and stores it in a database
looks like this
// STEP 1 - Create a ZIP file
byte[] buffer = new byte[1024];// 18024
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream("file.zip"));
outZip.setLevel(Deflater.DEFAULT_COMPRESSION);
FileInputStream in = new FileInputStream(c:\file.hex);
outZip.putNextEntry(new ZipEntry("file.hex"));
int len;
while (( len = in.read(buffer)) > 0)
outZip.write(buffer, 0, len);
outZip.closeEntry();
in.close();
outZip.close();
// STEP 2 - Insert zip file to DB
file = new File("file.zip");
FileInputStream fis = new FileInputStream( file );
the fis object i store in the DB
but i would like to avoid the filesystem completely , and not create the file.zip.
i think i need to convert the ZipOutputStream into FileInputStream directly
but i could not find a way to do it.
is there an easy way to accomplish this ?
i also have the same problem when i extract the file, in order to read it i must create 2 different temporary files - file.zip and file.hex
You just do not have to create FileOutputStream at all. Use ByteArrayOutputStream instead:
ByteArrayOutputStream zipBytes = new ByteArrayOutputStream()
ZipOutputStream outZip = new ZipOutputStream(zipBytes);
// run your code that adds zip entries....
zipBytes.getBytes() // returns array of bytes that contain your zipped information.
How to Typecast File object into InputStream.
File file=new File("c:\\abc.txt");
Thanks
File file=new File("c:\\abc.txt");
InputStream is = new FileInputStream(file);
or
InputStream is = new FileInputStream("c:\\abc.txt");
You don't typecast the file to Input stream, you create an InputStream object using the file as parameter. You can use FileInputStream:
FileInputStream fis = new FileInputStream(file);
Use file as a parameter in a FileInputStream Object.
Like this,
FileInputStream fis = new FileInputStream(file);
Creates a FileInputStream by opening a connection to an actual file,
the file named by the File object file in the file system. A new
FileDescriptor object is created to represent this file connection.
I made an android app which writes to a file in an activity.
The writing to file, it works like a charm:
FileOutputStream fOut = openFileOutput("myfeeds.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(file);
osw.flush();
osw.close();
But when I want to read it back from another acivity it can't find the file...the file exists I checked with DDMS file explorer.
Reading file contents:
FileInputStream fis = new FileInputStream("myfeeds.txt"); // cant find file
InputSource input = new InputSource(fis);
xr.setContentHandler(this);
xr.parse(input);
What is the correct location to my file?
Use openFileInput to get FileInputStream object for those files which are written using openFileOutputStream
use the following code
FileInputStream fiss = openFileInput("myfeeds.txt");
InputSource input = new InputSource(fis);
xr.setContentHandler(this);
xr.parse(input);
You should use
openFileInput( String name )
to read your file.
Regards,
STéphane