Java inputstream get filename

Java get file from input stream code example

As File on Windows is case-insensitive, one might have Test.properties working on Windows using File, and not as resource or other platforms (Linux, MacOSX). From : These -methods are for searching the probably bundled java application (e.g. as jar file) and not for searching files beside your application, which seems, what you want to do in this case.

Reading a java properties file using input Stream

Make sure that you kept your test.properties file in the classpath : i.e in Src folder of your application

package com.example; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadProperties < public static void main(String[] args) < ReadProperties r = new ReadProperties(); String result = r.readProperties(); System.out.println("Result : " + result); >public String readProperties() < String result = ""; Properties prop = new Properties(); String file = "test.properties"; InputStream fins = getClass().getClassLoader().getResourceAsStream(file); try < //prop.load(new FileInputStream(file)); if(fins!=null) prop.load(fins); >catch (IOException e) < e.printStackTrace(); >catch (Exception e) < e.printStackTrace(); >String nation = prop.getProperty("Nation"); String city = prop.getProperty("City"); String state = prop.getProperty("State"); result = "I live in "+city+" in "+state+" in "+nation; return result; > > 

The getResourceAsStream -method will in your case search in the package of your class.

This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.

These ClassLoader -methods are for searching the probably bundled java application (e.g. as jar file) and not for searching files beside your application, which seems, what you want to do in this case. See ClassLoader JavaDoc.

Читайте также:  События в javascript это

If the ClassLoader is unable to find the resource, the getResource* -methods will return null and hence your code will fail ( NullPointerException -> the stream is null ).

Update: If the properties-file is in the root of your project, you might try it with a / at the beginning of the path, when using the ClassLoader. See this thread for further information.

The code could be like this:

String file = "/test.properties"; InputStream fins = getClass().getResourceAsStream(file); InputStream fins = MyClass.class.getResourceAsStream(file); 

The resource is sought relative to the class of getClass() , but is made absolute with a starting / . Using getClass() however means that the actual class may be from another jar, and thus it might be better to use the actual class name.

In contrast to using File, a resource might be taken from a jar (a zip format). As File on Windows is case-insensitive, one might have Test.properties working on Windows using File, and not as resource or other platforms (Linux, MacOSX).

Open the jar (zip) and check that test.properties is there.

For completeness sake: you can also use a ClassLoader to fetch the resource. This is nice across jar. The path then however has to be absolute, not starting with / .

Java Program to Load File as InputStream, In the above example, we have a file named input.txt. The content of the file is. This is a content of the file input.txt. we have used the FileInputStream class to …

How to get input stream from input stream file?

Assuming that your code compiles:

If you are already picking up a zip file, there is no need to pass it through ZipInputStream again.

something like this http://www.avajava.com/tutorials/lessons/how-do-i-serve-up-a-pdf-from-a-servlet.html

If you want to get the whole ZIP file downloaded, you don’t have to use ZipInputStream . That is for accessing the contents of the ZIP file.

Instead of zis.getInputStream() use is.getInputStream() , and remove the code related to the ZipInputStream :

@RequestMapping("download") public String Download(HttpServletResponse response) < //ZipInputStream zis = null; no need for this try < InputStream is = new FileInputStream("C:\\apache-tomcat-6.0.36\\webapps\\xml\\XML.zip"); //zis = new ZipInputStream(is); //no need for this response.setHeader("Content-Disposition", "inline;filename=\"" + "XML.zip" + "\""); OutputStream out = response.getOutputStream(); response.setContentType("application/zip"); IOUtils.copy(is, out); //no zis here, and "is" is already an InputStream instance out.flush(); out.close(); >catch (IOException e) < e.printStackTrace(); >return null; > 

Also, I’d revise the .close() calls: they are almost always best fit for finally blocks to ensure everzthing gets closed properly. (that, or try-with-resource blocks are to be used.)

Java — How to get the file name of the one being used by, Even if you are certain you have a FileInputStream, you will still be unable to get the underlying file.FileInputStream does not retain its File …

Get filename from an inputstream (Java)

An input stream can be created to read from a file or from any other source of data. Therefore it makes no sense to have a filename attached to an input stream. Look in assetInfo to see if that class exposes that data (you can even look inside the class using reflection). Note that the creator or assetInfo made a design mistake not exposing this information, OR you are trying to make one now.

Java FileInputStream Class, Java FileInputStream Class for beginners and professionals with examples on Java IO or Input Output in Java with input stream, output stream, Java …

How to get the file name of the one being used by a inputStream? [duplicate]

I guess you cannot, because the input stream might not belong to a file. It can be SocketInputStream , or ByteArrayInputStream for example. The input stream is just an abstraction

An input stream can be created to read from a file or from any other source of data. Therefore it makes no sense to have a filename attached to an input stream.

InputStream input= assetInfo.openStream(); File t = new File(""); OutputStream out = new FileOutputStream(t); int read=0; byte[] bytes = new byte[1024]; while((read = input.read(bytes))!= -1)

Look in assetInfo to see if that class exposes that data (you can even look inside the class using reflection). Note that the creator or assetInfo made a design mistake not exposing this information, OR you are trying to make one now.

Even if you are certain you have a FileInputStream , you will still be unable to get the underlying file. FileInputStream does not retain its File argument, but immediately opens the file and retains just the FileDescriptor , a wrapper around the native int handle to the OS resource.

As of OpenJDK version 7, Update 40, a String path variable has been introduced, so with some luck you may try to reflectively get it.

Of course, this can be nothing more than heurstics. There is no official way through the public API.

Java InputStreamReader class, Java InputStreamReader. An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a …

Источник

Java inputstream get filename

Note: Before running the code, a text file named as «testout.txt» is required to be created. In this file, we are having following content:

After executing the above program, you will get a single character from the file which is 87 (in byte form). To see the text, you need to convert it into character.

Java FileInputStream example 2: read all characters

Youtube

For Videos Join Our Youtube Channel: Join Now

Feedback

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью