How to read a file and treat it as standard input

It was troublesome to enter it for the test, so I was wondering if it could be done. As a result of investigation, by specifying ** FileInputStream ** that implements InputStream as the standard input It seems that it can be realized, so I wrote the code.

code

try {
    InputStream stream = new FileInputStream("filename");
    System.setIn(new FileInputStream(stream);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    /* 
     *Description of other data processing, etc.
     */
} catch (IOException e) {
    e.printStackTrace();
}

Impressions

I felt that it wasn't so when asked if there was another opportunity to use it. Generally, the file is read and processed normally.

Recommended Posts

How to read a file and treat it as standard input
[Java] How to get and output standard input
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
How to ZIP a JAVA CSV file and manage it in a Byte array
How to request a CSV file as JSON with jMeter
How to load a Spring upload file and view its contents
[Xcode] How to add a README.md file
How to display error messages and success messages when registering as a user
How to record JFR (Java Flight Recorder) and output a dump file
Scraping and writing specific elements to a file
To manually deploy Struts2 as a war file
[ruby] How to receive values from standard input?
How to convert a value of a different type and assign it to another variable
How to jump from Eclipse Java to a SQL file
Java memory management and how to read GC Viewer
How to download a file (Servlet, HTML, Apache, Tomcat)
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
21 Load the script from a file and execute it
A solution that makes it easy to input Procon and Web tests to verify results
How to add another project as Maven library with CircleCI and use it for build
How to read a library from a JAR file with VS Code << How to not use Maven / Gradle >>
How to read your own YAML file (*****. Yml) in Java
[Ruby] How to split each GraphQL query into a file
Java Language Feature and How it Produced a Subtle Bug
How to develop and register a Sota app in Java
How to join a table without using DBFlute and sql
How to install GNOME as a desktop environment on CentOS 7
How to batch run JUnit and get coverage as well
How to create and launch a Dockerfile for Payara Micro
How to register as a customer with Square using Tomcat
How to leave a comment
How to read rails routes
Read standard input in Java
[iOS] How to read Carthage
How to insert a video
How to create a method
[Swift5] How to create a .gitignore file and the code that should be written by default
Code to specify the image name as a character string and paste it into ImageView (android)
R3 Corda node high availability (HA) and how to configure it
How to create a jar file or war file using the jar command
How to make a jar file with no dependencies in Maven
[Rails 6] How to create a dynamic form input screen using cocoon
How to display 0 on the left side of the standard input value
Sample to read and write LibreOffice Calc fods file in JRuby 2021
How to output a list of strings in JSF as comma-separated strings
How to open a script file from Ubuntu with VS code
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
Sample to read and write LibreOffice Calc fods file in Java 2021
[chown] How to change the owner of a file or directory
[Swift5] How to communicate from ViewController to Model and pass a value
Learning Ruby with AtCoder 11 How to receive frequently used standard input
I want to download a file on the Internet using Ruby and save it locally (with caution)
How to use StringBurrer and Arrays.toString.
How to add columns to a table
How to use EventBus3 and ThreadMode
Ruby standard input and various methods
How to sign a Minecraft MOD
How to make a JDBC driver
How to convert erb file to haml
How to call classes and methods