Name

createInput()

Description

This is a function for advanced programmers to open a Java InputStream. It's useful if you want to use the facilities provided by PApplet to easily open files from the data folder or from a URL, but want an InputStream object so that you can use other parts of Java to take more control of how the stream is read.

The filename passed in can be:
- A URL, for instance openStream("http://processing.org/")
- A file in the sketch's data folder
- The full path to a file to be opened locally (when running as an application)

If the requested item doesn't exist, null is returned. If not online, this will also check to see if the user is asking for a file whose name isn't properly capitalized. If capitalization is different, an error will be printed to the console. This helps prevent issues that appear when a sketch is exported to the web, where case sensitivity matters, as opposed to running from inside the Processing Development Environment on Windows or macOS, where case sensitivity is preserved but ignored.

If the file ends with .gz, the stream will automatically be gzip decompressed. If you don't want the automatic decompression, use the related function createInputRaw().
In earlier releases, this function was called openStream().
 

Examples

  • // Load the local file 'data.txt' and initialize a new InputStream
    InputStream input = createInput("data.txt");
    
    String content = "";
    
    try {
      int data = input.read();
      while (data != -1) {
        content += data;
        data = input.read();
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        input.close();
      } 
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    println(content);
    

Syntax

  • createInput(filename)

Parameters

  • filename(String)the name of the file to use as input

Return

  • InputStream
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.